当前位置: 首页 > 知识库问答 >
问题:

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

公羊学义
2023-03-14

我是maven的新手,在使用程序集插件生成zip文件时花了大约3天的时间,引用了http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/My project is multi module,所以我还引用了使用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或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-谢谢

共有1个答案

韩夕
2023-03-14

您应该更改的基本内容是创建一个单独的模块,在该模块中进行封装,如下所示。

   +-- 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>
 类似资料:
  • 我有一个多模块的项目,有两个模块:war和ear模块。我正在尝试使用Maven发行版插件来管理发行版。 我的配置到目前为止... 父POM: war模块POM: ear模块POM: 正如您看到的表单日志,test-war-0.0.1-sources.jar正在上传两次。这是为什么?我怎样才能编辑我的配置,使它只上传一次?

  • 问题内容: 我一直在尝试使用Maven Shade插件来获取jar,但是我仍然没有成功。 这是我的项目结构: Module1(pom.xml): Module2(pom.xml): MainModule(pom.xml): 根据这段代码,我得到了2个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望得到1个jar文件(MainMo

  • 我一直在尝试使用Maven Shade插件获取jar,但仍然没有成功。 这是我的项目结构: 模块1(pom.xml): 模块2(pom.xml): 主模块(pom.xml): 根据这段代码,我得到了2个jar文件(Module1-version.jar和Module2-version.jar)。但这不是我想要的。我希望得到1个jar文件(MainModule-version.jar),它将包含另一

  • 我正在使用Enunciate为多模块Maven项目中的RestEasy服务生成文档。在试图获取响应对象属性的描述时,我在文档生成方面遇到了一些问题。 例如,如果我的服务定义为: 当生成留档时,不生成属性描述。 在我只得到的所有响应对象中(未提供文档)。一个重要的想法是,所有响应类都位于服务所在的不同 Maven 模块中。响应类位于 UTIL 模块中。这些服务位于 EJB 模块中。 问题的原因肯定是

  • 我曾广泛使用过Maven 目前有5个不同的maven项目,每个项目都有一个不同的pom.xml。到目前为止,它们之间存在依赖关系,如果需要,每一个都指向 中的另一个。 现在我们不喜欢的是 当我们发布子projectA时,我们需要手动修改所有将projectA作为依赖项的项目以使用新版本。Saw Maven有一个版本插件,不知道这会有什么帮助。 作为解决方案,我希望在POM之间有一个更干净的组织,并

  • 我的项目结构如下: 是内的依赖项。 > 我应该在Jenkins中创建单独的作业来单独构建每个模块吗? 我为创建了一个作业,但出现以下错误(已将目标和操作设置为“清洁安装”: [INFO]扫描项目...[INFO] [INFO] ------------------------------------------------------------------------ [INFO]构建mypro