如何将 Maven 项目创建为 jar 文件,并在每个方法的 jar 中包含注释

How to create a Maven project as jar file with comments included in the jar for each method

我有Maven项目

所有 java 个文件位于 src/test/java

我的pom.xml如下

 <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>manual-jar</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>prepare-package</phase>
                        <configuration>
                            <classifier>manual</classifier>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>${project.build.finalName}-manual.lib/</classpathPrefix>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

当我 运行 命令 mvn clean install -DskipTests 时,我在目标文件夹中创建了 jar 文件。

但是当我在其他项目中使用它时,class 文件下带有 comments/multiline 注释 的方法是 未在罐子中显示。请在这里帮忙。

这是我在方法中评论的方式

/*
     * Returns an Image object that can then be painted on the screen.
     * The url argument must specify an absolute <a href="#{@link}">{@link "url"}</a>. The name
     * argument is a specifier that is relative to the url argument.
     * <p>
     * This method always returns immediately, whether or not the
     * image exists. When this applet attempts to draw the image on
     * the screen, the data will be loaded. The graphics primitives
     * that draw the image will incrementally paint on the screen.
     *
     * @param  url  an absolute URL giving the base location of the image
     * @param  name the location of the image, relative to the url argument
     * @return      the image at the specified URL
     * @see   String
     */
    public String abc(String name, String url){
        String c =name+url;
        return c ;
    }

您需要将源代码和 javadoc 工件作为构建的一部分附加,Maven documentation 中描述了该过程。

这就是 IDE 能够记录第三方 API 的方式。注释不会转换为字节码,因此默认情况下它们会从已编译的 class 中剥离。

这可以使用 maven-source 插件来完成。

源的 POM 配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-sources</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

javadoc 的 POM 配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-javadoc-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-javadocs</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

https://maven.apache.org/plugin-developers/cookbook/attach-source-javadoc-artifacts.html