【发布时间】:2015-03-30 06:36:44
【问题描述】:
我们需要使用 Maven 构建一个 jar,以包含其所有依赖项的方式,而且所有这些依赖项都被重命名(重新定位)。
假设我们自己的包都以com.mycompagny.projectx.*"开头。我们希望项目依赖项的包重命名为以"embedded"开头,但不是我们自己的类。
以maven-shade-plugin 为例,我无法做到这一点:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<artifactSet>
<includes>
<include>*.*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>*</pattern>
<shadedPattern>embedded.</shadedPattern>
<excludes>
<exclude>com.mycompagny.projectx.*</exclude>
</excludes>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
这里<pattern>*</pattern> 无效。另外,如果我使用<pattern></pattern>(空字符串),那么所有内容都会重新定位到“嵌入式”包,甚至是资源(也是“META-INF”目录)!当然,我们希望资源保留在 jar 的根目录中。
我想我们可以创建多个 <relocation> 元素,每个依赖包一个,但这将是很多工作:<relocation>com</relocation>、<relocation>net</relocation>、<relocation>javax</relocation> 等。
知道如何在不触及我们自己的类、资源和“META-INF”目录的情况下轻松地重新定位 uber jar 中的所有依赖项吗?
【问题讨论】:
标签: java maven jar maven-shade-plugin