Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

Maven JAR 插件(版本 3.0.2)不断抛出以下错误,即使是 jar 目标 的单次调用:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default) on project test: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]

这里有一个(最小的?)pom.xml 演示了这个问题:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

调用只是mvn package

根据 documentationclassifier 只需要在同一目标的多次调用中指定,test-jar 目标有一个合理的默认值,我不知道打算改变。

此外,问题似乎没有出现在 JAR 插件的 2.x 行。

我错过了什么吗? 有人可以建议我做错了什么吗?

P.S。 Maven 版本是 3.3.9。

Jar 插件实际上使用配置执行了两次:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <executions>
    <execution>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

如果你用这样的配置检查日志,你会得到类似的东西:

[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ test ---
[INFO] Building jar: ...\test\target\test-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default) @ test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

意味着插件实际上被执行了两次。会发生什么,是 Jar 插件,在一个项目中,其包装为 jar has a default execution bound to the package phase。此默认执行是日志中提到的 ID 为 default-jar.

的执行

当您在插件中配置一个 <execution> 时,您实际上配置了一个新的执行,其中将调用插件的 jar 目标。由于jar goal binds by default to the package phase, that execution is getting executed at that phase, after the default binding inherent to the jar packaging. And since the plugin ran already, it is failing because running it again would actually replace the main artifact already produced during the first run. This error was added in version 3.0.0 of the plugin in MJAR-198,因为这样的事情发生很可能是错误的配置,应该及早发现。

因此,解决方法很简单:不要执行指定目标 jar,让默认的执行(来自 jar 包装)完成工作。由于默认执行,即使没有 jar 目标的显式配置,JAR 仍将被创建。如果你也想要一个测试 JAR,you will still need to configure the plugin to do that with:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

但请注意,未指定目标 jar

在我的例子中,我将执行的 ID 设置为 default-jar,然后错误消失了。例如

<execution>
    <id>default-jar</id>
    <phase>package</phase>
    <goals>
        <goal>jar</goal>
    </goals>
</execution>

如果您的日志显示如下内容:

[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ test --
[WARNING] JAR will be empty - no content was marked for inclusion!

在 src/main/java 中添加一个无用的 class 似乎可以解决问题,请参阅:

http://maven.40175.n5.nabble.com/quot-mvn-clean-verify-deploy-quot-causes-jar-plugin-to-execute-twice-td5877166.html

由于上述 link 可能会在 2021-09 年被破坏,您可能想尝试 http://mail-archives.apache.org/mod_mbox/maven-users/201608.mbox/thread 作为替代方案