在多模块maven项目中创建程序集归档时出错

时间:2017-10-23 12:05:24

标签: java maven maven-assembly-plugin maven-module

我创建了一个包含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>

assembly.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>&amp; <moduleSet>代码?

3 个答案:

答案 0 :(得分:1)

最后我找到了解决方案

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>

3)添加要在汇编描述符的<{1}}元素中汇编的文件/工件

assembly.xml

<files>

答案 1 :(得分:0)

使用示例here

这是一个工作示例,父母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>

Child 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>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>
分发/ 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

答案 2 :(得分: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>

希望这会帮助其他人...

参考:to see more