如何在多模块Maven项目中使用Maven Assembly插件

17

我对maven不太熟悉,花了大约3天时间使用assembly插件生成zip文件。参考链接:http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/ 我的项目是多模块的,所以我也参考了 Managing multi-module dependencies with Maven assembly plugin

但是,我仍然存在一些效率低下的问题。下面是assembly.xml(我从第一个链接继承而来):

 <assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
    <format>zip</format>
</formats>

<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
    <dependencySet>
        <!-- Project artifact is not copied under library directory since it is 
            added to the root directory of the zip package. -->
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>lib</outputDirectory>
        <unpack>false</unpack>

    </dependencySet>
</dependencySets>
<moduleSets>
    <moduleSet>

        <!-- Enable access to all projects in the current multimodule build! <useAllReactorProjects>true</useAllReactorProjects> -->

        <!-- Now, select which projects to include in this module-set. -->
        <includes>
            <include>com.XX:YY-main</include>
        </includes>

        <!--<binaries> <outputDirectory>YY-main/target</outputDirectory> <unpack>false</unpack> 
            </binaries> -->
    </moduleSet>
</moduleSets>
<fileSets>
    <!-- Adds startup scripts to the root directory of zip package. The startup 
        scripts are located to src/main/scripts directory as stated by Maven conventions. -->
    <fileSet>
        <directory>${project.build.scriptSourceDirectory}</directory>
        <outputDirectory>conf</outputDirectory>
        <includes>
            <include>*</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>log4j.properties</include>
        </includes>
    </fileSet>
    <!-- adds jar package to the root directory of zip package -->
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>*with-dependencies.jar</include>
        </includes>
    </fileSet>
</fileSets>

以下是pom.xml文件(主要或主文件)

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
             <configuration>                    <descriptors>
              <descriptor>YY-main/src/main/assembly/assembly.xml</descriptor>   
         <!-- <descriptor>DummyAssembly.xml</descriptor> -->

                </descriptors>
            </configuration>
        </plugin>   

问题:

1)由于primary pom中引用了assembly.xml,所有的子模块都会被压缩。我该如何指定只压缩某些子模块,而忽略其他所有子模块。我尝试将YY-main/src/main/assembly/assembly.xml从主pom移动到子模块pom中,并在主pom中使用什么也不做的dummyassembly.xml。但这不起作用(可能是因为dummyassembly.xml缺少一些必需的行)。我尝试了一些方法,但似乎没有什么用。

2)同样在assembly.xml(dependencyset)中,它将所有库复制到“lib”文件夹中。我该如何避免这种情况。我也尝试过一些方法(如排除等),基于http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet,但没有任何结果。

有人能够为我提供特定的语句,以更改我的pom或assembly文件以解决问题1和问题2-谢谢。


如果您知道如何做到这一点,我这里有一个问题:http://stackoverflow.com/q/33088454/1735836 - Patricia
2个回答

28

你应该改变的基本事情是创建一个单独的模块,在这个模块中进行包装,它将会是这样。

   +-- root (pom.xml)
        +--- mod-1 (pom.xml)
        +--- mod-2 (pom.xml)
        +--- mod-assembly (pom.xml)

mod-assembly中的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>org.test.parent</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

  <artifactId>dist</artifactId>
  <packaging>pom</packaging>

  <name>Packaging Test : Distribution</name>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-one</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-two</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <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>proj1-assembly.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

这将解决在每个子模块中运行maven-assembly-plugin的问题以及虚构描述符的问题。在这里,您可以找到一个真实的这种结构的示例。

此外,将模块放在一个文件夹中,将依赖项放在另一个文件夹中,您可以使用像这样的装配描述符:

  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
          <useProjectArtifact>false</useProjectArtifact>
          <useTransitiveDependencies>true</useTransitiveDependencies>
          <outputDirectory>lib</outputDirectory>
          <unpack>false</unpack>
          <excludes>
            <exclude>${project.groupId}:*:*</exclude>
          </excludes>
      </dependencySet>
  </dependencySets>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <binaries>
        <outputDirectory>modules</outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>

1
我该如何在命令行中运行它(mvn clean package assembly:single)? - kashili kashili
1
如果您按照上述配置,只需要简单的 mvn clean package 命令即可完成。 - khmarbaise
我有一个类似的项目结构。但在我的情况下,我想包含模块1的源代码。这也可能吗? - Himz
使用maven-sources-plugin创建适当模块的源代码,并向mod-assembly模块添加补充依赖。 - khmarbaise
@khmarbaise - 如果您知道如何做到这一点,我在这里有一个问题:http://stackoverflow.com/q/33088454/1735836 - Patricia

5
我认为这篇文章 如何使用Maven组装多模块项目 可以通过示例回答您的问题。
对于您的问题,我的回答如下:
1)您应该在父级pom中的buildpluginManagement标签中添加Assembly插件以便在模块中重用它;上述文章是一个很好的示例。您还可以查看在线书籍“Maven:The Complete Reference”中的8.6 最佳实践章节,主题8.6.2。

2) 排除标签可能会有些棘手。再次查看在线书籍《Maven:完全参考》的8.5 控制组件的内容章节。例如,子主题8.5.4关于dependencySets讲述了如何对dependencySets进行依赖项包含和排除的微调;同样,现在关于moduleSets的子主题8.5.5展示了如何在该上下文中使用它。


Alexandre - 我正在查看你的链接。如果你知道如何开始这个过程,我在这里有一个问题:http://stackoverflow.com/q/33088454/1735836 - Patricia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接