Use Maven's assembly plugin to implement custom packaging

1. Background

  Recently, we have more and more projects, and then I was thinking about how to unify the packaging methods of basic services and generate them according to our requirements. Through research, we have perfectly realized this by using the maven assembly plugin. The demand is so cool that there is no wood. This article shares the configuration of the plugin and the unified packaging method of microservices.

2. Configuration steps and other matters

1. First we need to configure the maven assembly plugin in pom.xml

 1 <build>
 2    <plugins>
 3      <plugin>
 4        <groupId>org.apache.maven.plugins</groupId>
 5        <artifactId>maven-jar-plugin</artifactId>
 6        <version>2.3.1</version>
 7        <configuration>
 8          <archive>
 9            <manifest>
10              <!--The main class to run when running the jar package requires the full name of the class --> 
11               < mainClass > com.hafiz.Runner </ mainClass > 
12               <!-- Whether to specify dependencies under the project classpath --> 
13               < addClasspath > true < / addClasspath > 
14              <!-- Declare a prefix when specifying dependencies --> 
15               < classpathPrefix > ./ </ classpathPrefix > 
16             </ manifest > 
17           </ archive > 
18         </ configuration >
19      </plugin > 
20       < plugin > 
21         < groupId > org.apache.maven.plugins </ groupId > 
22         < artifactId > maven-assembly-plugin </ artifactId > 
23         < executions > 
24           < execution > <!-- configure executor- -> 
25             < id > make-assembly </ id > 
26             < phase > package </ phase > <!-- Bind to the package lifecycle phase -->
27            <goals>
28              <goal>single</goal><!-- 只运行一次 -->   
29            </goals>
30            <configuration>
31              <finalName>${project.name}</finalName>
32              <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径--> 
33            </configuration>
34          </execution>
35        </executions>
36      </plugin>
37    </plugins>
38 </build>

2.接着我们在src/main/assembly文件中配置assembly.xml文件

 1 <assembly>
 2     <id></id>
 3     <formats>
 4         <format>tar.gz</format><!--打包的文件格式,也可以有:war zip-->
 5     </formats>
 6     <!--tar.gz压缩包下是否生成和项目名相同的根目录-->
 7     <includeBaseDirectory>true</includeBaseDirectory>
 8     <dependencySets>
 9         <dependencySet>
10             <!--是否把本项目添加到依赖文件夹下-->
11             <useProjectArtifact>true</useProjectArtifact>
12             <outputDirectory>lib</outputDirectory>
13             <!--将scope为runtime的依赖包打包-->
14             <scope>runtime</scope>
15         </dependencySet>
16     </dependencySets>
17     <fileSets>
18         <fileSet>
19             <directory>src/main/bin</directory>
20             <outputDirectory>/</outputDirectory>
21         </fileSet>
22     </fileSets>
23 </assembly>

其中,生成的lib文件夹下放该项目的所有依赖以及该服务jar包,src/main/bin文件夹下我们一般放start.sh和stop.sh两个脚本文件用来开启和关闭该服务,打包后直接放到根目录下。生成的tar.gz文件的名字为:maven-assembly-plugin插件中配置的finalName-assembly.xml配置的id(若assembly中没有指定id,则只有前半部分).

assembly的具体语法,请参见官网http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

这里面涉及到如何生成可执行的jar包,具体参见:用Maven快速生成带有依赖的可执行jar包

3.项目目录结构

其中红色方框内就是通过assembly插件是生成的tar.gz文件,解压后里面的结构如下:

其中lib目录下是本项目生成的可执行jar包以及它的所有依赖jar包。若assembly插件中配置<useProjectArtifact>为false,则不会生成assembly-demo文件夹,直接就是lib文件夹以及两个脚本文件。

代码Github地址:https://github.com/hafizzhang/assembly-demo.git

4.打包方式思考

我们通过这种方式,每个微服务就是一个以服务名称命名的tar.gz文件,解压后里面lib目录下是该服务生成的可执行jar包以及它所有的依赖jar包,我们直接运行根目录下start.sh和stop.sh两个脚本文件来进行服务的开启和关闭。风格统一,简单明了!

三、总结

  随着微服务的越来越火,我们将服务进行细粒度拆分后,也需要很好的把服务的打包以及部署方式进行统一,这样我们就可以走自动化发布工具来进行统一服务的管理和部署,使得越来越方便

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326737493&siteId=291194637