目录

Apache Maven Assembly Plugin

Maven Assembly Plugin 使开发人员能够将项目输出组合到一个可分发的存档中,该存档还包含依赖项、模块、站点文档和其他文件。

您的项目可以使用 预制描述符文件(Pre-defined Descriptor Files) 描述符之一轻松构建分发“程序集” 。这些描述符处理许多常见操作,例如将项目的工件与生成的文档一起打包到单个 zip 存档中。或者,您的项目可以提供自己的描述符,并对依赖项、模块、文件集和单个文件在程序集中的打包方式进行更高级别的控制。

要在 Maven 中使用 Assembly 插件,您只需要:

  • 选择或编写要使用的程序集描述符,
  • 在项目的 pom.xml 中配置 Assembly Plugin ,以及
  • 在您的项目上运行 mvn assembly:single

Assembly Descriptor

使用内置的 Assembly Descriptor 要使用maven-assembly-plugin,需要指定至少一个要使用的assembly descriptor 文件。默认情况下,maven-assembly-plugin 内置了几个可以用的assembly descriptor:

Pre-defined Descriptor Files:

  • bin : 类似于默认打包,会将bin目录下的文件打到包中;
  • jar-with-dependencies : 会将所有依赖都解压打包到生成物中;
  • src :只将源码目录下的文件打包;
  • project : 将整个project资源打包。

maven-assembly-plugin-2.5.5.jar 中 对应 bin 的 assembly descriptor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<plugin>  
  <groupId>org.apache.maven.plugins</groupId>  
  <artifactId>maven-assembly-plugin</artifactId>  
  <version>2.5.5</version>  
  <executions>  
    <execution>  
      <id>make-assembly</id> 
      <!-- 绑定到 package 生命周期阶段上 -->
      <phase>package</phase>  
      <goals>
        <goal>single</goal>  
      </goals>  
    </execution>  
  </executions>  
</plugin>         

使用 jar-with-dependencies 打包依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            com.allstar.configcenter.control.ConfigCenterEntry  
                          </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

自定义 Assembly Descriptor

format:指定打包类型; includeBaseDirectory:指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下); fileSets:指定要包含的文件集,可以定义多个fileSet; directory:指定要包含的目录; outputDirectory:指定当前要包含的目录的目的地。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

<plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-assembly-plugin</artifactId>  
                <version>2.5.5</version>  
                <configuration>  
                  <!-- 指定 Main-Class -->
                    <archive>  
                        <manifest>  
                            <mainClass>com.gsm.crm.App</mainClass>  
                        </manifest>  
                    </archive>  
                    <descriptors>
                      <descriptor>asseblies/demo.xml
                      </descriptor>
                      </descriptors>
                      <outputDirectory>output
                        </outputDirectory>
                </configuration>  
            </plugin>  

如果只想有 finalName

1
2
<finalName> </finalName>
<appendAssemblyId>false</appendAssemblyId>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-assembly-plugin</artifactId>  
                <version>2.5.5</version>  
                <configuration>  
                    <archive>  
                        <manifest>  
                            <mainClass>com.gsm.crm.App</mainClass>  
                        </manifest>  
                    </archive>  
                    <descriptorRefs>  
                        <descriptorRef>jar-with-dependencies</descriptorRef>  
                    </descriptorRefs>  
                </configuration>  
                <executions>  
                    <execution>  
                        <id>make-assembly</id>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>single</goal>  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin>  

https://maven.apache.org/plugins/maven-assembly-plugin/index.html

assembly descriptor 文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<assembly>
    <id>${assembly-id}</id>
    <!-- 最终打包成一个用于发布的war文件 -->
    <formats>
        <format>${assembly-format}</format>
    </formats>

    <fileSets>
        <!-- 把项目公用的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources/base</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>

        <!-- 把项目环境的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources/${env}</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>

        <!-- 打包项目自己编译出来的jar文件 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>WEB-INF/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <!-- 打包项目依赖的jar文件 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>WEB-INF/lib/*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

附录