2

我创建了一个包含 2 个模块(child1、child2)的多模块 maven 项目。
我正在尝试使用程序集插件创建一个包 - APP.zip,其中应包含 app.properties、child1.war 和 child2.war。
但是当我运行mvn assembly:single命令时出现以下错误

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.6:single (default-cli) on project app: Failed to create assembly: Error creating assembly archive My_Package: You must set at least one file. -> [Help 1]

项目文件夹结构

app
  |
   pom.xml
   assembly.xml
   app.properties 
   child 1
       | 
       pom.xml src target
                   |
                     child1.war
   child 2
       |
       pom.xml src target
                   |
                     child2.war   

父 pom (app/pom.xml)

    <project>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>parent</name>

        <modules>
           <module>child 1</module>
           <module>child 2</module>
        </modules>
        <build>
           <pluginManagement>
               <plugins>
                  <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>APP</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                    <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                     <execution>
                    <id>make-bundles</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                      </execution>
                </executions>
            </plugin>
        </plugins>
  </pluginManagement>
</build>
</project>

程序集.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <formats>
        <format>zip</format>
    </formats>
    <id>My_Package</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.karthik:child1</include>
                <include>com.karthik:child2</include>
            </includes>
        </moduleSet>
    </moduleSets>
</assembly>

我相信我错过了导致此错误的程序集描述符中的某些内容。
1) 您能否帮助解决这个问题并帮助创建一个包含 app.properties、child1.war 和 child2.war 的 zip(APP.zip)?
2)我们什么时候需要使用<fileSet>&<moduleSet>标签?

4

3 回答 3

4

最后我找到了解决方案

1)在父 POM 中添加一个单独的模块 -用于组装的分发。

父 POM

<project>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>parent</name>

        <modules>
           <module>child 1</module>
           <module>child 2</module>
           <module>distribution</module>
        </modules>
</project>

2)在分发模块中配置maven程序集插件

配电模块POM

    <parent>
        <groupId>com.karthik</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>distribution</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Distribution</name>

    <build>
        <plugins>
            <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                 <execution>
                <id>make-bundles</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                       <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>
  </plugins>
</build>

<files>3) 在程序集描述符的元素中添加要组装的文件/工件

程序集.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>distribution</id>
  <formats>
      <format>zip</format>
  </formats>
  <files>
        <file>
            <source>../Child1/target/child1.war</source>
        </file>
        <file>
            <source>../Child2/target/child2.war</source>
        </file>
        <file>
            <source>../app.properties</source>
        </file>
    </files>
</assembly>
于 2017-10-25T09:29:59.967 回答
0

在此处使用示例

这是一个工作示例,父 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.greg</groupId>
  <artifactId>assembly-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>assembly-example</name>

  <modules>
    <module>child1</module>
    <module>child2</module>
    <module>distribution</module>
  </modules>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
            <descriptors>
              <descriptor>src/assembly/assembly.xml</descriptor>
            </descriptors>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

儿童绒球:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>assembly-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child1</artifactId>
  <packaging>jar</packaging>

  <name>child1</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

分发pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>assembly-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>distribution</artifactId>
  <packaging>jar</packaging>

  <name>Distribution</name>

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>child1</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>child2</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>distro-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/assembly/assembly.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
</project>

distribution/src/assembly 目录中的 assembly.xml:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <includes>
        <include>com.greg:child1</include>
      </includes>
      <binaries>
        <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>
</assembly>

mvn clean package p

生成一个 zip 文件:

jar tvf distribution/target/distribution-1.0-SNAPSHOT-bin.zip
     0 Mon Oct 23 14:55:10 BST 2017 modules/
     0 Mon Oct 23 14:55:10 BST 2017 modules/maven-assembly-plugin/
  2086 Mon Oct 23 14:55:06 BST 2017 modules/maven-assembly-plugin/child1-1.0-SNAPSHOT.jar
  2086 Mon Oct 23 14:55:08 BST 2017 modules/maven-assembly-plugin/child2-1.0-SNAPSHOT.jar
于 2017-10-23T12:50:50.523 回答
0
jar tvf distribution/target/distribution-1.0-SNAPSHOT-bin.zip

     0 Mon Oct 23 14:55:10 BST 2017 modules/
     0 Mon Oct 23 14:55:10 BST 2017 modules/maven-assembly-plugin/
  2086 Mon Oct 23 14:55:06 BST 2017 modules/maven-assembly-plugin/**child1-1.0-SNAPSHOT**.jar
  2086 Mon Oct 23 14:55:08 BST 2017 modules/maven-assembly-plugin/**child2-1.0-SNAPSHOT**.jar

在这里,您也可以随心所欲地获得战争和罐子。像这样:

<outputFileNameMapping>${module.artifactId}.${module.extension}</outputFileNameMapping>

使用它来获取战争和 jar 文件。

在汇编文件中:

<moduleSets>
<moduleSet>
  <useAllReactorProjects>true</useAllReactorProjects>
  <includes>
    <include>com.greg:child1</include>
  </includes>
  <binaries>
    <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
    <unpack>false</unpack>
  </binaries>
</moduleSet>
这里只需添加上面的行获取该jar文件,如下所示:
<moduleSets>
<moduleSet>
  <useAllReactorProjects>true</useAllReactorProjects>
  <includes>
    <include>com.greg:child1</include>
  </includes>
  <binaries>
    <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
    <outputFileNameMapping>${module.artifactId}.${module.extension}</outputFileNameMapping>
    <unpack>false</unpack>
  </binaries>
</moduleSet>

希望这对其他人有帮助...

参考:查看更多

于 2021-04-13T07:06:24.773 回答