英文:
How to add a Javadoc JAR file to a Maven project in IntelliJ IDEA?
问题
以下是您要翻译的内容:
我正在制作一个本地的Java实用库,并将其构建成一个JAR文件。我还为Javadocs构建了一个JAR文件。
这些是由Maven生成的JAR文件:
现在,我正在尝试在IntelliJ IDEA中使用Javadocs JAR文件,通过在“文件”>“项目结构”中导入它,它可以工作。我可以在我的IDE中获得有关代码引用的提示和文档。但是,一旦我重新加载我的Maven项目,Javadocs就消失了,我必须手动添加它。
我还从IntelliJ那里得到了一个警告,每次我重新添加Javadoc JAR文件时都会出现。
添加Javadoc JAR文件到Maven项目的正确方式是什么?
英文:
I am making a local Java utility library and building it into a JAR file. I am also building a JAR file for the Javadocs.
These are the JAR files generated by Maven:
Now, I am trying to use the Javadocs JAR file with IntelliJ IDEA by importing it in File > Project Structure, and it works. I get hints and documentation for code references in my IDE. But as soon as I reload my Maven project, the Javadocs are gone and I have to manually add it back.
I am also getting this warning from IntelliJ everytime I add the Javadoc JAR file back.
What is the proper way of adding a Javadoc JAR file into a Maven project?
答案1
得分: 4
Pure Maven(好的IDE应该识别这些):
mvn dependency:resolve -Dclassifier=javadoc
Ref: https://maven.apache.org/plugins/maven-dependency-plugin/resolve-mojo.html
Notes: It's rather unusual (unless explicitly ordered) to "package" (dependency) javadocs with the main artifact... In maven, "we" rather maintain (own!) artifacts for "main"/sources/and javadocs (See: https://maven.apache.org/repository/guide-central-repository-upload.html)
As mentioned, good IDEs should be capable of processing the "local repository" for javadoc (and source) artifacts of undrlying dependencies.
To "download sources" use:
mvn dependency:sources
Ref: https://maven.apache.org/plugins/maven-dependency-plugin/sources-mojo.html
When your (remote) Repository does not comply with "central requirements" (has no "source" and "javadoc" artifacts attached), also to eliminate (any) <scope>system</scope>
dependency, I would strongly recommend (to "mavenize" the dependencies and) install/deploy sources and javadocs artifacts for this:
- locally with:
mvn install:install ...
- to a "repo":
mvn deploy:...
For "mavenize" refer to:
- locally: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
- for "deployments": https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Specifically for sources and javadocs artifacts (once you have/generate these):
- deploy: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-sources-javadoc.html
- accordingly with "install-file";
The "pure IDE" way would be to link to the (external!) sources/javadocs (in file system/zip/jar archive):
... See screenshot:
com.example:my-dep:0.0.1-SNAPSHOT
is my (fake)system
scoped dependency.- After "applying" changes and confirming the warn dialog (iml vs pom, re-import...), IntelliJ should recognize javadocs and sources.
- According to the warning, our "modifications" get lost in case of a "maven re-import".
英文:
Pure Maven (good IDEs should recognize these):
mvn dependency:resolve -Dclassifier=javadoc
Ref: https://maven.apache.org/plugins/maven-dependency-plugin/resolve-mojo.html
Notes: It's rather unusual (unless explicitly ordered) to "package" (dependency) javadocs with the main artifact... In maven, "we" rather maintain (own!) artifacts for "main"/sources/and javadocs (See: https://maven.apache.org/repository/guide-central-repository-upload.html)
As mentioned, good IDEs should be capable of processing the "local repository" for javadoc (and source) artifacts of undrlying dependencies.
To "download sources" use:
mvn dependency:sources
Ref: https://maven.apache.org/plugins/maven-dependency-plugin/sources-mojo.html
When your (remote) Repository does not comply with "central requirements" (has no "source" and "javadoc" artifacts attached),
also to eliminate (any) <scope>system</scope>
dependency, I would strongly recommend (to "mavenize" the dependencies and) install/deploy sources and javadocs artifacts for this:
- locally with:
mvn install:install ...
- to a "repo":
mvn deploy:...
For "mavenize" refer to:
- locally: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
- for "deployments": https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Specifically for sources and javadocs artifacts (once you have/generate these):
- deploy: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-sources-javadoc.html
- accordingly with "install-file"
The "pure IDE" way, would be to link to the (external!) sources/javadocs (in file system/zip/jar archive):
... See screenshot:
-
com.example:my-dep:0.0.1-SNAPSHOT
is my (fake)system
scoped dependency. -
After "applying" changes and confirming the warn dialog (iml vs pom, re-import...), IntelliJ should recoginze javadocs and sources.
-
According to the warning our "modifications" get lost in case of a "maven re-import".
答案2
得分: 2
以下是翻译好的部分:
有一种更通用的 Maven 风格的方式,应该优先使用,而不是 "IDE 方式"。您可以在项目内部使用一个本地的 "伪" 仓库。将 JAR 文件添加到您的仓库中。Maven 命令的具体形式取决于 JAR 文件的类型。
JAR 文件:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo
源代码:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1-sources.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo \
-Dclassifier=sources
Javadoc 文档:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1-javadoc.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo \
-Dclassifier=javadoc
接下来的步骤是将仓库和依赖项添加到您的 POM 文件中:
<repositories>
<repository>
<id>fake-repo</id>
<name>Just a fake repository.</name>
<url>file://${project.basedir}/fake-repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>mylib</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
英文:
There is a more general maven-style way and should be preferred over the "IDE way". You can use a local "fake" repository inside your project. Add the jar files to your repo. The maven call depends on the type of the jar.
The jar:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo
The sources:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1-sources.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo \
-Dclassifier=sources
The javadoc:
mvn deploy:deploy-file \
-DgroupId=org.example \
-DartifactId=mylib\
-Dversion=1.1 \
-Dfile=/path/to/mylib-1.1-javadoc.jar \
-Dpackaging=jar \
-Durl=file://path/to/your-project/fake-repo \
-Dclassifier=javadoc
The next step is adding the repo and the dependency to your pom:
<repositories>
<repository>
<id>fake-repo</id>
<name>Just a fake repository.</name>
<url>file://${project.basedir}/fake-repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>mylib</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论