maven shaded jar:改变输出位置

Max*_*Max 4 jar pom.xml maven-3 maven maven-shade-plugin

我在使用Maven Shade插件时遇到了困难,因为我希望将我的阴影jar安装到与父pom相同的文件夹中(而不是本地src/target目录).

布局:maven_project

guide/
   parent_pom.xml
projA/
   pom.xml
projB/
   pom.xml
   /target
      original-projB-0.0.3.jar
      projB-0.0.3.jar (shaded jar) 
Run Code Online (Sandbox Code Playgroud)

我必须导出项目并使其他人更容易运行可执行jar我想将着色jar重定位到该guide文件夹.

不幸的是,我尝试过使用

<outputDirectory>/home/name/Desktop/maven_project/guide/</outputDirectory>    
Run Code Online (Sandbox Code Playgroud)

但这只会将原始jar移动到目录中.

问题:关于如何在那里移动阴影罐的任何想法(甚至删除过程中的原始jar)?

A_D*_*teo 5

Maven的阴影插件默认替换由编译生成的原始罐子,并创建前缀它的一个副本.

更换和重新定位可以通过被配置outputDirectory,outputFilefinalName配置条目.

应用以下配置:

<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>
Run Code Online (Sandbox Code Playgroud)

我们是:

  • 首先根据您的要求禁用默认jar的生成,并由此专用SO Q/A指定
  • 然后配置Shade插件以将其输出重定位到上层guide文件夹(通过相对路径,更好的方法,也由@Tunaki建议)
  • 还要配置finalName元素以禁用替换(这也会影响重定位,在某种意义上也是(前缀的)原始jar将被重定位).根据官方文档,finalName是

    着色artifactId的名称.如果您想更改本机工件的名称,可以使用该<build><finalName>设置.如果将其设置为不同<build><finalName>,则即使shadedArtifactAttached正在使用,也不会执行文件替换.

因此,Maven将仅在配置的位置生成阴影jar.


另一种方法是使用outputFile配置条目,它指定:

着色工件的输出文件的路径.设置此参数后,创建的存档既不会替换项目的主工件,也不会附加.因此,该参数将导致参数finalName,shadedArtifactAttached,shadedClassifierName并且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>
Run Code Online (Sandbox Code Playgroud)

并且具有完全相同的行为.


旁注:您实际上是在改变构建的行为.如果有人只从模块文件夹本身构建一个模块,他/她就不会在target文件夹上找到预期的内容,而是在父文件夹上(有点意外).


更新
应用上面的配置并从命令行仅调用Shade插件

mvn shade:shade
Run Code Online (Sandbox Code Playgroud)

但是,您会遇到以下问题:

[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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

  • 是的,我使用“mvn clean package”,不,它没有修复它,作为副产品还可以,原始 jar 文件不是什么大问题 (2认同)