我有this maven multi-module project:
ModuleA
src
pom.xml
target
ModuleA-with-dependencies-shaded.jar (version 4.1 of lucene relocated)
ModuleB
src
pom.xml
target
ModuleB-with-dependencies.jar (version 7.5.0 of lucene)
ModuleDist
assembly
all.xml
pom.xml (shaded plugin for jar + assembly for Docker)
dist pom插件的配置如下:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<shadedClassifierName>all</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<!--remove models from jar see mitie -->
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>.dat</resource>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<id>make-dist</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/all.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
和程序集all.xml:
<assembly>
<id>all</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/docker</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
由于ModuleA
对lucene 4.1
具有传递依赖关系(但已重定位,因此它不会与moduleB发生冲突),并且ModuleB
对lucene 7.5.0
具有传递依赖关系,我想使用< [先前构建的和ModuleA
的maven阴影插件中的ModuleDist
的阴影罐(因为如果我在ModuleDist
中重新定位了lucene,它将重新定位所有lucene类)。
<?xml version="1.0"?>
<assembly>
...
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
查看组装插件documentation了解更多详细信息。可以找到一个工作示例here(不使用阴影)。我的个人经验是,最好只有一个最终模块来组装所有东西。这样,着色器不必一遍又一遍地拆卸和组装所有东西。
使用Jackson和Log4j2时,阴影是个大问题,因为它破坏了一些扩展查找机制,这些机制希望所有内容都位于单独的jar中。我建议不再使用它。