Java Maven JAR Plugin 3.0.2 错误:您必须使用分类器将补充工件附加到项目而不是替换它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40964500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:02:13  来源:igfitidea点击:

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

javamavenjarmaven-jar-plugin

提问by Alex Shesterov

Maven JAR plugin (version 3.0.2) keeps throwing the following error, even for a single invocation of the jargoal:

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]

[错误] 无法在项目测试中执行目标 org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default):您必须使用分类器将补充工件附加到项目而不是替换它们. -> [帮助 1]

Here's a (minimal?) pom.xmlwhich demonstrates the problem:

这是一个(最小的?)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>

The invocation is just mvn package.

调用只是mvn package.

  • It doesn't seem to matter whether there are any classes/resources at all — the above error message appears anyway.
  • The problem also appears if two goals are specified (jarand test-jar).
  • The problem does NOT appear if no goalsare specified. But this is not an option, since I really need both jarand test-jar.
  • 是否有任何类/资源似乎并不重要 - 无论如何都会出现上述错误消息。
  • 如果指定了两个目标(jartest-jar),问题也会出现。
  • 如果指定目标,则不会出现此问题。但这不是一个选择,因为我真的需要jartest-jar

According to the documentation, classifieronly needs to be specified on multiple invocations of the same goal, and there's a reasonable default for the test-jargoal which I don't intend to change.

根据文档classifier只需要在同一目标的多次调用中指定,并且test-jar我不打算更改的目标有一个合理的默认值。

Also, the problem doesn't seem to appear on the 2.x line of the JAR plugin.

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

Did I miss something? Could anybody please suggest what I am doing wrong?

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

P.S. The Maven version is 3.3.9.

PS Maven 版本是 3.3.9。

采纳答案by Tunaki

The Jar Plugin is actually getting executed twice with the configuration:

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

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

If you check the logs with such a configuration, you will have something like:

如果您使用这样的配置检查日志,您将看到如下内容:

[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] ------------------------------------------------------------------------

meaning that the plugin was in fact executed twice. What happens, is that the Jar Plugin, in a project that has a packaging of jarhas a default execution bound to the packagephase. This default execution is the one mentioned in the logs with the ID of default-jar.

这意味着该插件实际上执行了两次。发生的情况是,Jar 插件,在一个打包为 的项目中有一个jar绑定到package阶段的默认执行。此默认执行是日志中提到的 ID 为 的执行default-jar

When you configured an <execution>in the plugin, you actually configured a new execution, where the jargoal of the plugin is to be invoked. Since the jargoal binds by default to the packagephase, that execution is getting executed at that phase, after the default binding inherent to the jarpackaging. 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, because such a thing happening is very likely a mis-configuration which should be detected early.

当您<execution>在插件中配置 an时,您实际上配置了一个新的执行,其中jar将调用插件的目标。由于jar目标默认绑定到package阶段,因此该执行将在该阶段执行,在jar打包固有的默认绑定之后。而且由于插件已经运行,它失败了,因为再次运行它实际上会替换第一次运行期间已经生成的主要工件。这个错误是在MJAR-198插件的 3.0.0 版本中添加的,因为发生这种情况很可能是错误配置,应该及早检测到。

As such, the fix is simple: don't have an execution that specifies the goal jar, and let the default one (coming from the jarpackaging) do the work. The JAR will still be created, even without the explicit configuration of the jargoal, thanks to the default execution. If you want a test JAR as well, you will still need to configure the plugin to do thatwith:

因此,修复很简单:没有指定目标的执行jar,并让默认的(来自jar包装)完成工作。jar由于默认执行,即使没有明确配置目标,JAR 仍将被创建。如果你还想要一个测试 JAR,你仍然需要配置插件来做到这一点

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

But note that the goal jaris not specified.

但请注意,jar未指定目标。

回答by Derik

In my case , I have setup the execution's ID as default-jar, then the error gone. e.g.

就我而言,我已将执行的 ID 设置为 default-jar,然后错误消失了。例如

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

回答by Wolfgang Fahl

If your logs shows something like:

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

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

Adding a single useless class in src/main/java seems to solve the issue see:

在 src/main/java 中添加一个无用的类似乎解决了这个问题,见:

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

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