【问题标题】:How to use Maven assembly plugin with multi module maven project如何在多模块 Maven 项目中使用 Maven 程序集插件
【发布时间】:2014-07-27 03:59:26
【问题描述】:

我是 maven 新手,花了大约 3 天时间用汇编插件生成 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 (primary or main)

 <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) 由于在主 pom 中引用了 assembly.xml,因此所有子模块也会被压缩。如何仅指定要压缩的某些子模块,而忽略所有其他子模块。我试图将 YY-main/src/main/assembly/assembly.xml 从主 pom 移动到子/子模块 pom 并在 main 中有 dummyassembly.xml 什么都不做。但这不起作用(可能是因为 dummyassembly.xml 缺少一些必需的行)。尝试了一些东西,但似乎没有任何效果

2) 同样在 assembly.xml (dependencyset) 中,它会将所有库复制到“lib”文件夹。我怎样才能避免这种情况。我再次尝试了一些基于http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet 的东西(排除..),但没有任何效果。

谁能提供我应该在我的 pom 或汇编文件中更改以解决 1 和 2 的具体声明,谢谢

【问题讨论】:

标签: maven maven-assembly-plugin maven-jar-plugin


【解决方案1】:

您应该更改的基本内容是创建一个单独的模块,您可以在其中执行如下所示的打包。

   +-- 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 的问题以及虚拟描述符的问题。 Here you can find a real 这种结构的例子。

此外,将模块放在一个文件夹中,将依赖项放在另一个文件夹中,您可以使用assembly descriptor like this

  <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>

【讨论】:

  • 如何从命令行运行它(mvn clean package assembly:single)??
  • 如果你像上面那样配置它,你只需要一个简单的mvn clean package
  • 我有一个类似的项目结构。但就我而言,我想包含 mod 1 的源代码。是否也可以这样做 /?
  • 使用 maven-sources-plugin 创建相应模块的源代码,并为 mod-assembly 模块添加补充依赖。
  • @khmarbaise - 如果你知道怎么做,我在这里有个问题:stackoverflow.com/q/33088454/1735836
【解决方案2】:

我认为How to Assemble Multi-Module Project with Maven 的帖子可能会通过示例回答您的问题。

我对您问题的回答:

1) 您应该在 buildpluginManagement 标记内的父 pom 中添加 Assembly 插件,以便在模块中重用它;上面提到的帖子是如何做到这一点的一个很好的例子。您还可以查看在线书籍“Maven:完整参考”的8.6 Best Practise 章节,主题 8.6.2。

2) exclusion 标签可能很棘手。再次查看在线书籍“Maven: The Complete Reference”的8.5 Controlling the Contents of an Assembly 章节。例如,关于 dependencySets 的子主题 8.5.4 提到了如何微调 dependencySets 的依赖包含和排除;再次,现在关于 moduleSets 的子主题 8.5.5 展示了如何在该上下文中使用它。

【讨论】:

猜你喜欢
  • 2016-01-10
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2011-03-10
  • 2011-07-08
相关资源
最近更新 更多