【问题标题】:How to create a Maven project as jar file with comments included in the jar for each method如何将 Maven 项目创建为 jar 文件,其中每个方法的 jar 中都包含注释
【发布时间】:2021-08-05 12:25:28
【问题描述】:

我有 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 文件。

但是当我在其他项目中使用它时,带有cmets/multiline cmets的类文件下的方法jar中没有显示。请在这里帮忙。

这就是我在方法中的评论

/*
     * 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 ;
    }

【问题讨论】:

  • “jar 中包含的 cmets”到底是什么意思?您可以使用 maven 源插件来创建源工件。
  • 将所有 Java 源代码放入 src/test/java 是没有意义的。目录src/test/java 仅用于测试正在运行的构建,但如果src/main/java 中没有任何内容,则没有要测试的内容。
  • 如果给定的All the java files are located in src/test/java 是真的,那么你就完全误解了 maven 的工作原理。正如@JFabianMeier 所描述的那样......将java文件移动到适当的位置。
  • @J Fabian Meier 和 @khmarbaise,我已经将所有内容从 src/test/java 移动到 src/main/java,当我现在构建这个项目时,我也获得了包含资源的包(我没有想要)当我在其他项目中使用这个 jar 时,也无法识别方法。
  • 请在 github 或类似网站上创建一个示例项目以展示真实世界...这样更容易...

标签: java maven executable-jar


【解决方案1】:

这可以使用 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

【讨论】:

  • 感谢@xalves,当我运行 mvn clean install MavenReportException 时出现以下错误:生成 Javadoc 时出错:退出代码:1 - javadoc:警告 - 包 test.java.com.terminaldriver 没有源文件.common.logger
  • @Gireesh 我相信你可以添加属性 failOnError 。我希望至少会生成一些文档。如果没有,也许需要对该包有更多的了解。 &lt;configuration&gt;&lt;failOnError&gt;false&lt;/failOnError&gt;&lt;/configuration&gt;
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 2013-10-19
    相关资源
    最近更新 更多