我通过放弃 jarjar-maven-plugin 并恢复到 maven-shade-plugin 找到了解决此问题的方法。这允许在您自己的命名空间中重新打包类,设置 jar 的主类,并且至关重要的是,重写生成的 pom 以不包括现在捆绑的编译时依赖项。
我的 pom.xml 中实现这一点的部分是:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>org.objectweb.asm</pattern>
<shadedPattern>${repackage.base}.org.objectweb.asm</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${package.base}.my.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
此配置的重要部分是:
shadedArtifactAttached
当设置为 false 时,意味着阴影 jar 将替换通常会生成的主要工件。这默认为 false,但值得指出。
createDependencyReducedPom
which when set to true means that when the shaded jar is deployed or installed, the pom.xml which is deployed will not include the compile-scope dependencies which have been repackaged into the jar.
relocation
these elements configure how files within the dependencies are repackaged into the shaded jar. In the above example any class whose canonical name begins with org.objectweb.asm
will be moved to ${package.base}.org.objectweb.asm
, and thus when packaged in the jar will have the equivalent file path within the jar.
With this configuration, when my project is deployed, when clients declare a compile-scope dependency on my project, it only pulls in the shaded jar, and no transitive dependencies.