使用Maven程序集插件管理多模块依赖项

ser*_*tsy 8 dependency-management maven maven-assembly-plugin multi-module

我使用Maven程序集插件为我的多模块项目创建一个程序集.从这个多模块项目构建了两个独立的应用程序,每个应用程序都有一组独立的依赖项.我创建了一个自定义程序集描述符,它使用模块构建及其各自的依赖项组装两个目录(对于每个应用程序).它做的一切都很好,但有一点 - 它将两个模块的依赖关系放到彼此的程序集中.

以下是我的项目的简化版本,具有完全相同的行为.

考虑一个由两个模块和一个组装模块组成的项目:

APP
  module1
  module2
  assembly
Run Code Online (Sandbox Code Playgroud)

我已经添加了纯粹用于演示的依赖项:

com.test.app:module1:jar:1.0
\- commons-cli:commons-cli:jar:1.2:compile

com.test.app:module2:jar:1.0
\- commons-daemon:commons-daemon:jar:1.0.8:compile
Run Code Online (Sandbox Code Playgroud)

这是父POM:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>
  <artifactId>app</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>module1</module>
    <module>module2</module>
    <module>assembly</module>
  </modules>
</project>
Run Code Online (Sandbox Code Playgroud)

module1 POM:

<project>
  <parent>
    <groupId>com.test</groupId>
    <artifactId>app</artifactId>
    <version>1.0</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test.app</groupId>
  <artifactId>module1</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>1.2</version>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

module2 POM:

<project>
  <parent>
    <groupId>com.test</groupId>
    <artifactId>app</artifactId>
    <version>1.0</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test.app</groupId>
  <artifactId>module2</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>commons-daemon</groupId>
      <artifactId>commons-daemon</artifactId>
      <version>1.0.8</version>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

装配POM:

<project>
  <parent>
    <groupId>com.test</groupId>
    <artifactId>app</artifactId>
    <version>1.0</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test.app</groupId>
  <artifactId>assembly</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.2</version>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>

            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>

        <configuration>
          <appendAssemblyId>false</appendAssemblyId>

          <descriptors>
            <descriptor>src/main/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

最后,汇编描述符:

<assembly>
  <id>distribution</id>
  <includeBaseDirectory>false</includeBaseDirectory>

  <formats>
    <format>dir</format>
  </formats>

  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>

      <includes>
        <include>com.test.app:module1:jar</include>
      </includes>

      <binaries>
        <outputDirectory>module1</outputDirectory>
        <unpack>false</unpack>

        <dependencySets>
          <dependencySet>
            <unpack>false</unpack>
          </dependencySet>
        </dependencySets>
      </binaries>
    </moduleSet>

    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>

      <includes>
        <include>com.test.app:module2:jar</include>
      </includes>

      <binaries>
        <outputDirectory>module2</outputDirectory>
        <unpack>false</unpack>

        <dependencySets>
          <dependencySet>
            <unpack>false</unpack>
          </dependencySet>
        </dependencySets>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

如您所见,程序集绑定到程序包阶段.所以,当我执行时

mvn package
Run Code Online (Sandbox Code Playgroud)

从父目录,我有以下程序集

module1/
  commons-cli-1.2.jar
  commons-daemon-1.0.8.jar
  module1-1.0.jar
module2/
  commons-cli-1.2.jar
  commons-daemon-1.0.8.jar
  module2-1.0.jar
Run Code Online (Sandbox Code Playgroud)

基本上,这里的问题是module1不依赖于commons-daemon,但是程序集插件包含了依赖性.同样,使用module2和commons-cli.

有人可以解释为什么程序集插件的行为方式?

什么是解决方案?

Kei*_*ith 10

我总是有类似的经验使用汇编插件与多模块项目,最终结果不是我所期望的.我希望其他人可以提供更准确的答案,说明为什么会发生这种情况以及如何最好地同时使用这两个概念.

也就是说,可能的解决方法是让module1和module2生成自己的程序集工件,这些工件包含各自的jar和依赖项.然后,您可以修改程序集子模块pom文件,以从其兄弟模块中依赖于生成的分发工件,然后将这些工件解压缩到新的程序集中.

在Module1和Module2的pom文件中,您可以将程序集插件配置添加到程序包阶段,就像使用程序集子模块一样.

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.2</version>

        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>

        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    </plugins>
  </build>
Run Code Online (Sandbox Code Playgroud)

Module1将有一个像这样的src/main/assembly/descriptor.xml

<assembly>
  <id>distribution</id>
  <includeBaseDirectory>false</includeBaseDirectory>

  <formats>
    <format>zip</format>
  </formats>

  <dependencySets>
    <dependencySet>
      <outputDirectory>module1</outputDirectory>
      <unpack>false</unpack>
    </dependencySet>
  </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

Module2将有一个类似的src/main/assembly/descriptor.xml

<assembly>
  <id>distribution</id>
  <includeBaseDirectory>false</includeBaseDirectory>

  <formats>
    <format>zip</format>
  </formats>

  <dependencySets>
    <dependencySet>
      <outputDirectory>module2</outputDirectory>
      <unpack>false</unpack>
    </dependencySet>
  </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

然后在assembly/pom.xml中添加Module 1和2 zip工件作为依赖项

  <dependencies>
    <dependency>
      <groupId>com.test.app</groupId>
      <artifactId>module1</artifactId>
      <version>1.0</version>
      <type>zip</type>
      <classifier>distribution</classifier>
    </dependency>
    <dependency>
      <groupId>com.test.app</groupId>
      <artifactId>module2</artifactId>
      <version>1.0</version>
      <type>zip</type>
      <classifier>distribution</classifier>
    </dependency>
  </dependencies>
Run Code Online (Sandbox Code Playgroud)

...并修改程序集/ src/main/assembly/descriptor.xml文件,使其如下所示

<assembly>
  <id>distribution</id>
  <includeBaseDirectory>false</includeBaseDirectory>

  <formats>
    <format>dir</format>
  </formats>

  <dependencySets>
    <dependencySet>
      <useTransitiveDependencies>false</useTransitiveDependencies>
      <unpack>true</unpack>
    </dependencySet>
  </dependencySets>

</assembly>
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,这可能是一种可能的解决方法,不幸的是,在构建过程中添加了大量额外的XML配置.但它的确有效.