我有一个Maven多模块项目。在一个模块中,我们使用maven-assembly-plugin插件创建一个ZIP文件。这个的配置:
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<excludes>
<exclude>
com.sample.blabla:test-core-client
</exclude>
</excludes>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
这个的pom配置:
<execution>
<id>make-service-client-with-dependencies-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${service-client-with-dependencies.zip.filename}</finalName>
<appendAssemblyId>true</appendAssemblyId>
<outputDirectory>${project.build.directory}/zip</outputDirectory>
<descriptors>
<descriptor>src/main/assembly/test-service-client-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
</execution>
不幸的是,创建的ZIP包含更多的jar-s,我们希望…例如:37 X maven-XXX JAR,许多弹簧罐,马车罐,等等
但是我们不希望包含这些jar,它们只是运行时所必需的。我们怎么做呢?
Maven程序集插件只包含根据您的配置在runtime
范围内的jar文件。您可以运行mvn dependency:tree
并将输出与zip文件的内容进行比较。
您可以尝试将属性useTransitiveDependencies
设置为false
。这将从zip文件中排除所有可传递的依赖项。但这可能会产生令人不快的副作用。
您使用描述符test-service-client-with-dependencies.xml
,其中包括结果中的所有内容和厨房水槽。
使用jar-with-dependencies
代替。这将包括条目运行时依赖项(本地的和临时的)。
如果仍然太多,那么您可以通过将它们声明为<scope>provided</scope>
(如果其他人稍后将它们添加到类路径中),<scope>test</scope>
(如果依赖项仅用于运行测试)或<optional>true</optional>
(如果这是可选依赖项)来忽略依赖项。