您的位置:首页 > 其它

Maven package错误:You have to use a classifier to attach supplemental artifacts to the project instead

2017-11-17 10:22 4259 查看
我在pom.xml文件中添加了一个maven-jar-plugin的插件,目的是想多打一个jar包,如下:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>service-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/webservice/</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
目的是将webservice目录打包成jar包,运行mvn package之后,却报错了:

...
[INFO] --- maven-jar-plugin:3.0.2:jar (make-a-jar) @ deploy ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.035 s
[INFO] Finished at: 2017-11-17T09:25:22+08:00
[INFO] Final Memory: 18M/167M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (make-a-jar) on project deploy: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException[/code] 
原来如果在plugin中添加maven-jar-plugin插件之后,maven会运行两次maven-jar-plugin插件,第一次运行是打包当前工程的jar,第二次运行是执行在plugin中设置的jar,这样的话会生成两个jar包,这里就要用到classifier这个属性了。classifier是用来标识不同的jar包的名称的一个分类符,这个属性的值会添加到工程jar包的名称后面。例如当前工程的坐标:

<groupId>deploy</groupId>
<artifactId>deploy</artifactId>
<version>0.0.1-SNAPSHOT</version>
那就是意味着打包之后的jar包名称为deploy-0.0.1-SNAPSHOT.jar,而在plugin中设置要打的jar包是额外的jar包,classifier的值设置为bak:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>service-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>bak</classifier> <!-- 生成deploy-0.0.1-SNAPSHOT-bak.jar -->
<classesDirectory>${project.build.directory}/webservice/</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>


那最后在build目录下会有两个jar包,一个是deploy-0.0.1-SNAPSHOT.jar,另外一个则是deploy-0.0.1-SNAPSHOT-bak.jar这个deploy-0.0.1-SNAPSHOT-bak.jar是根据${project.build.directory}/webservice/这个目录下的文件来生成的jar包。总之,打出的jar包名称一定要区分,maven默认打多个jar包时不能覆盖掉原来的jar包,所以classifier就做了这个事情,可以从控制台打印的信息看到,maven执行了两次maven-jar-plugin的打包操作:

[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ vbap3-webservice ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /deploy/target/deploy-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (vbap-jar) @ vbap3-webservice ---
[INFO] Building jar: /deploy/target/deploy-0.0.1-SNAPSHOT-bak.jar
[INFO]

如果是只想要使用plugin生成的jar包,那么可以直接把execution的id改为default-jar,因为默认本身会调用一个maven-jar-plugin并且执行一个default-jar的操作,这里如果命名了一个default-jar的操作,那么就会按照这个操作进行打包,但是这个时候不能添加classifier分类符,因为这时只有一个jar包。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>  <!-- 没有classifier -->
<classesDirectory>${project.build.directory}/webservice/</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐