简体   繁体   English

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

[英]How can I use previously built jars in a multi-module maven project?

I have this maven multi-module project :我有这个 maven 多模块项目

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)

The dist pom plugins are configured like this : 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>

And the assembly all.xml :和程序集 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>

As the ModuleA is having a transitive dependency on lucene 4.1 (but relocated so it won't clash with moduleB) and ModuleB is having a transitive dependency on lucene 7.5.0 , I'd like to use the previously built and shaded jar of ModuleA in the maven shaded plugin of ModuleDist (because if I relocate lucene in ModuleDist it is relocating all lucene classes).由于ModuleAlucene 4.1具有传递依赖性(但已重新定位,因此不会与 moduleB 发生冲突)而ModuleBlucene 7.5.0具有传递依赖性,因此我想使用先前构建和着色的ModuleA jar在Maven的阴影插件ModuleDist (因为如果我在搬迁Lucene的ModuleDist它是所有搬迁的lucene类)。

How could I do that ?我怎么能那样做?
or is there another way of doing this ?或者有另一种方法吗?

To add all your jars of the runtime dependencies to the assembly, add something like this:要将所有运行时依赖项的 jar 添加到程序集中,请添加如下内容:

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

Take a look at the assembly plugin documentation for more details.有关更多详细信息,请查看程序集插件文档 A working example can be found here (no shading used).可以在此处找到一个工作示例(未使用阴影)。

My personal experience is that it's better to only have one final module that assembles all stuff.我的个人经验是最好只有一个最终模块来组装所有东西。 This way the shader doesn't have to disassemble and assembler everything over and over.这样着色器就不必一遍又一遍地拆卸和组装所有东西。

Shading is a big problem when using Jackson and Log4j2 because it breaks some extension lookup mechanisms that expect everything to be in a separate jar.使用 Jackson 和 Log4j2 时,着色是一个大问题,因为它破坏了一些期望所有内容都在单独的 jar 中的扩展查找机制。 I recommend not using it anymore.我建议不要再使用它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM