【问题标题】:Getting started with Maven assembly plugin with multi-module project使用多模块项目的 Maven 程序集插件入门
【发布时间】:2016-01-10 08:45:23
【问题描述】:

我们有一个人与我们合作,他使用 maven-assembly-plugin 来打包所有依赖项。他后来离开了公司,我正试图弄清楚他是怎么做到的。我现在正在学习 maven-assembly-plugin。

我们的应用程序被拆分为一个带有相关模块项目的 Maven POM 项目。在以下示例中,我包含了运行时模块项目。我试图在我的应用程序中创建一个名为 runtime 的模块项目,但是当我进行任何更改时,它就会失去与父项目的关联。

我们正在使用 Netbeans。

+- pom.xml
+- Simulator 
    +- pom.xml 
+- comms 
    +- pom.xml 
    +- src 
        +- main 
            +- java
+- framework 
    +- pom.xml 
    +- src 
        +- main 
            +- java
+- core 
    +- pom.xml 
    +- src 
        +- main    
            +- java

这就是我想要创建的:

+- runtime 
    +- pom.xml 
    +- src 
        +- assemble 
            +- assembly (xml file)
    +- target 
        +- Simulator   
        +- archive-tmp    
        +- classes     
        +- test-classes     
        +- Simulator (zip file)
    +- config 
    +- data   
    +- scripts  

在做了更多研究之后,我想我可能本末倒置。我已经有一个包含模块项目的主项目。

  1. 那么下一步究竟是什么?
  2. 创建程序集描述符文件?
  3. 将我的主 pom 文件编辑为父文件?

以下是一些有用的问题和答案,但我仍然感到困惑。谁能告诉我先做什么?

Maven assembly on multi module project with special structure

How to use Maven assembly plugin with multi module maven project

编辑:

我弄清楚了为什么该模块从我的主项目中消失了。 Netbeans!!我重新启动它,我的运行时模块就在那里。

那么,现在我需要在运行时模块中编辑我的 POM 文件吗?

【问题讨论】:

  • 那么您的问题到底是什么?
  • 第一步是什么?创建程序集描述符文件?或者将我的主 pom 文件编辑为父文件?或者描述符文件是否设置了父级?我对我必须做什么以及插件处理什么感到困惑。
  • 你想改变什么?
  • 我想将运行时模块添加到我的项目中,并将其设置为包含程序集描述符文件的项目。

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


【解决方案1】:

如果我没看错你的问题,你想要做的是在现有项目中添加一个新的 Maven 模块(称为runtime)并在这个新项目上使用maven-assembly-plugin 来打包它。

然后第一步是创建 Maven 模块。我不确定 Netbeans 是否提供了执行此操作的工具(Eclise 提供),但归结为:

  • 在父 POM 的 <modules> 部分中,为 runtime 添加新的 <module>
  • 在父 POM 的同一目录级别创建一个名为 runtime 的新文件夹
  • 在这个新文件夹中创建一个文件pom.xml,将根 POM 声明为父 POM,如下所示:

    <parent>
        <groupId>...</groupId>
        <artifactId>...</artifactId>
        <version>...</version>
    </parent>
    

然后,你需要配置maven-assembly-plugin 来做这个新模块的打包。这是在程序集描述符的帮助下完成的,其格式为documented here

首先,请考虑以下 POM 配置和程序集描述符,它将把 configdatascripts 下的所有内容打包到一个 zip 文件中:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/assemble/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
     </executions>
  </plugin>

assembly.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>
  <fileSets>
      <fileSet>
          <directory>config</directory>
          <outputDirectory>/config</outputDirectory>
      </fileSet>
      <fileSet>
          <directory>data</directory>
          <outputDirectory>/data</outputDirectory>
      </fileSet>
      <fileSet>
          <directory>scripts</directory>
          <outputDirectory>/scripts</outputDirectory>
      </fileSet>
    </fileSets>
</assembly>

在父 POM 上运行mvn clean install 后,会在模块runtimetarget 目录下创建一个zip 文件。它将包含 3 个指定的文件夹。

【讨论】:

  • Tunaki:非常感谢您的明确指示。你知道尝试学习这样的东西是多么令人难以抗拒和困惑。 :-)
猜你喜欢
  • 2014-07-27
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2020-05-27
  • 2011-07-08
相关资源
最近更新 更多