The Maven 阴影插件默认情况下替换构建生成的原始 jar 并创建其前缀为的副本original.
更换和搬迁可以通过配置outputDirectory
, outputFile
and 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>
We are:
- 首先根据您的要求和指定禁用默认 jar 的生成这个专用的 SO Q/A
- 然后配置Shade Plugin将其输出重新定位到上层
guide
文件夹(通过相对路径,更好的方法也建议@Tunaki)
- Also configuring the
finalName
element in order to disable replacement (which also affects relocation, in the sense that also the (prefixed) original jar will be relocated). As per official documentation the finalName is
着色的artifactId 的名称。如果您想更改本机工件的名称,您可以使用<build><finalName>
环境。如果这设置为不同于<build><finalName>
,不会执行文件替换,即使shadedArtifactAttached
正在使用中。
因此,Maven 将仅在配置的位置生成阴影 jar。
另一种方法是使用outputFile
配置条目,指定:
着色工件的输出文件的路径。设置此参数后,创建的存档既不会替换项目的主要工件,也不会附加它。因此,该参数导致参数finalName
, 附有阴影Artifact,shadedClassifierName
and createDependencyReducedPom
使用时可以忽略。
因此,您可以将上面的配置更改为:
<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
文件夹,该文件夹将位于父文件夹上(有点令人惊讶)。
Update
应用上面的配置并仅从命令行调用 Shade 插件
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] ------------------------------------------------------------------------