如何将模块jar合并到Maven2中的单个jar?

Xiè*_*léi 16 maven-2

我有一个带有几个jar模块的maven2项目,构建项目将获得目录模块/ XYZ/target/XYZ-xxxjar中每个模块的.jar存档

现在,如果我的项目P是版本Pp.qr,并且我想生成包含所有子模块的单个jar Pp.qr-all.jar,我该怎么办?

cet*_*nar 19

你想要的东西叫做超级罐子.此模块必须具有要包装到一个jar中的所有其他子模块的依赖性.如果您创建另一个将生成所需工件的子模块,则可以在reactor中构建其所有依赖项,但如果它是一个单独的项目,则必须安装所有uber jar依赖项.

| parent
| -- submodule1
...
| -- submoduleN
| -- uberjarSubmodule
Run Code Online (Sandbox Code Playgroud)

Uber jar可以通过以下方式完成:

  1. maven-shade-plugin - 在您的情况下,您必须记住从模块中排除传递依赖

    <project>
    ...
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.2.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>classworlds:classworlds</exclude>
                  <exclude>junit:junit</exclude>
                  <exclude>jmock:jmock</exclude>
                  <exclude>xml-apis:xml-apis</exclude>
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    </build>
    ...
    </project>
    
    Run Code Online (Sandbox Code Playgroud)
  2. maven-assembly-plugin - 在这个问题中你会找到一个详细的答案