默认情况下,Maven Shade Plugin取代了构建生成的原始jar,并创建了一个副本,其前缀为,原始为。
可以通过outputDirectory
,outputFile
和finalName
配置条目来配置替换和重定位。
应用以下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${project.version}-something</finalName>
<outputDirectory>../guide</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我们:
- 首先禁用默认罐子的产生,通过您的要求的要求,并指定由this dedicated SO Q/A
- 然后配置阴影插件将其输出重定位到上面的
guide
文件夹(通过相对路径,更好的方法也可以通过@Tunaki建议)
- 还配置
finalName
元素以禁用替换(这也会影响重定位,也就是(前缀)原始jar将被重新定位)。按照official documentation的finalName是
The name of the shaded artifactId. If you like to change the name of the native artifact, you may use the <build><finalName>
setting. If this is set to something different than <build><finalName>
, no file replacement will be performed, even if shadedArtifactAttached
is being used.
这样,Maven将产生仅在配置的位置的阴影罐子。
另一种方法,是使用了outputFile
配置项,其中规定:
The path to the output file for the shaded artifact. When this parameter is set, the created archive will neither replace the project's main artifact nor will it be attached. Hence, this parameter causes the parameters finalName
, shadedArtifactAttached, shadedClassifierName
and createDependencyReducedPom
to be ignored when used.
因此,你可以改变上面的配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>../guide/${project.artifactId}-${project.version}-shaded.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
而且具有完全相同的行为。
附注:你真的在改变这里的构建行为。如果某人从模块文件夹本身构建了一个模块,他/她就不会在target
文件夹中找到预期内容,而是在父文件夹中找到预期内容(有点意外)。
更新
应用上述配置和从命令行调用只有阴影插件
mvn shade:shade
不过,您将有以下问题:
[INFO] --- maven-shade-plugin:2.4.3:shade (default-cli) @ test-addjar ---
[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR] supported. Please add the goal to the default lifecycle via an
[ERROR] <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR] remove this binding from your POM such that the goal will be run in
[ERROR] the proper phase.
[ERROR] - You removed the configuration of the maven-jar-plugin that produces the main artifact.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
你配置Jar插件或Shade插件的'outputDirectory'条目? –
你应该改变[outputDirectory'](https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#outputDirectory)。附注:硬编码值是脆弱的。为什么不使用'../ guide'? – Tunaki
是的,输出目录是在阴影插件@ A.DiMatteo – Max