如何在多模块Maven项目中使用以前构建的jar?

问题描述 投票:0回答:1

我有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>

由于ModuleAlucene 4.1具有传递依赖关系(但已重定位,因此它不会与moduleB发生冲突),并且ModuleBlucene 7.5.0具有传递依赖关系,我想使用< [先前构建的和ModuleA的maven阴影插件中的ModuleDist的阴影罐(因为如果我在ModuleDist中重新定位了lucene,它将重新定位所有lucene类)。

我该怎么办?还是有另一种方法?
java maven maven-shade-plugin
1个回答
0
投票
要将所有运行时依赖的jar添加到程序集中,请添加如下内容:

<?xml version="1.0"?> <assembly> ... <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> <unpack>false</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly>

查看组装插件documentation了解更多详细信息。可以找到一个工作示例here(不使用阴影)。

我的个人经验是,最好只有一个最终模块来组装所有东西。这样,着色器不必一遍又一遍地拆卸和组装所有东西。

使用Jackson和Log4j2时,阴影是个大问题,因为它破坏了一些扩展查找机制,这些机制希望所有内容都位于单独的jar中。我建议不再使用它。

© www.soinside.com 2019 - 2024. All rights reserved.