简体   繁体   中英

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

I have 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)

The dist pom plugins are configured like this :

        <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 :

<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).

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:

<?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. I recommend not using it anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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