多模块项目的 Maven 程序集插件入门

Getting started with Maven assembly plugin with multi-module project

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

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

我们正在使用 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 文件吗?

如果我没看错你的问题,你想要做的是向现有项目添加一个新的 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 to do the packaging of this new module. This is done with the help of a assembly descriptor, whose format is 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 个指定的文件夹。