簡體   English   中英

如何在多模塊 Maven 項目中使用以前構建的 jars?

[英]How can I use previously built jars in a multi-module maven 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)

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 jar在Maven的陰影插件ModuleDist (因為如果我在搬遷Lucene的ModuleDist它是所有搬遷的lucene類)。

我怎么能那樣做?
或者有另一種方法嗎?

要將所有運行時依賴項的 jar 添加到程序集中,請添加如下內容:

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

有關更多詳細信息,請查看程序集插件文檔 可以在此處找到一個工作示例(未使用陰影)。

我的個人經驗是最好只有一個最終模塊來組裝所有東西。 這樣着色器就不必一遍又一遍地拆卸和組裝所有東西。

使用 Jackson 和 Log4j2 時,着色是一個大問題,因為它破壞了一些期望所有內容都在單獨的 jar 中的擴展查找機制。 我建議不要再使用它了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM