首页 > 解决方案 > 如何在不使用“硬”依赖项的情况下在程序集中包含另一个模块的构建输出?

问题描述

我的(非常简化的)Maven 项目,包括所需的输出文件如下所示:

.
│   pom.xml
├───db-delivery
│   │   assembly.xml
│   │   db-delivery-info.txt
│   │   pom.xml
│   └───target
│           db-delivery-1.0.0-SNAPSHOT.zip
└───delivery
    │   assembly.xml
    │   delivery-info.txt
    │   pom.xml
    └───target
            delivery-1.0.0-SNAPSHOT.zip

的内容delivery-1.0.0-SNAPSHOT.zip看起来像这样(db-deliveryZIP 文件只包含db-delivery-info.txt):

.
│   delivery-info.txt
└───db
        db-delivery-info.txt

如果我执行mvn verify一切都很好。但是由于 to 的依赖msa:deliverymsa:db-delivery:zip一些需要依赖解析的“快速” Maven 目标无法执行,例如mvn dependency:treeor mvn org.basepom.maven:duplicate-finder-maven-plugin:check。问题是,除非package涉及阶段,否则反应堆中的 ZIP 文件db-delivery不可用,这会导致Could not find artifact msa:db-delivery:zip:1.0.0-SNAPSHOT目标在delivery.

我试图将依赖类型从更改zippom. 这样,“快速”目标就起作用了,但是如果我mvn verify再次执行“完整”构建(db-delivery-1.0.0-SNAPSHOT.pom文件。

有没有办法在不直接依赖(类型)的情况下将 ZIP 工件db-delivery放入程序集中?只要我确保模块是在之前构建的(例如依赖于 type ),必要的文件在需要时就可用,所以从技术上讲这应该是可能的。deliveryzipdb-deliverydeliverypom

根(聚合器和父 POM):

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>msa</groupId>
  <artifactId>root</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>delivery</module>
    <module>db-delivery</module>
  </modules>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.3.0</version>
          <configuration>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptors>
              <descriptor>assembly.xml</descriptor>
            </descriptors>
          </configuration>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

模块交付

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>msa</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>delivery</artifactId>
  <packaging>pom</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>msa</groupId>
      <artifactId>db-delivery</artifactId>
      <version>${project.version}</version>
      <type>zip</type><!-- using pom makes `mvn dependency:tree` work but creates wrong ZIP file content -->
    </dependency>
  </dependencies>
</project>
<assembly>
  <id>delivery-zip</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <files>
    <file>
      <source>delivery-info.txt</source>
    </file>
  </files>

  <dependencySets>
    <dependencySet>
      <outputDirectory>db</outputDirectory>
      <unpack>true</unpack>
      <useProjectArtifact>false</useProjectArtifact>
      <includes>
        <include>msa:db-delivery</include>
      </includes>
    </dependencySet>
  </dependencySets>
</assembly>

模块db-delivery

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>msa</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>db-delivery</artifactId>
  <packaging>pom</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
<assembly>
  <id>db-delivery-zip</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <files>
    <file>
      <source>db-delivery-info.txt</source>
    </file>
  </files>
</assembly>

标签: mavenmaven-assembly-pluginmaven-dependency-plugin

解决方案


推荐阅读