maven-assembly-plugin(maven定制化打包)

芮瑾瑜
2023-12-01

maven-assembly-plugin(定制化打包)

定制化打包方式

  • maven打包插件:http://maven.apache.org/plugins/maven-assembly-plugin/

它是maven中针对打包任务而提供的标准插件

  • pom.xml(模板,拿来即用)
<build>
		<resources>
            <!-- 以下的这几种过滤规则最后取并集 -->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<excludes>
					<exclude>static/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>static/**</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>static/**</exclude>
				</excludes>
			</resource>
		</resources>
    
		<plugins>
			<!-- 1.此方法不执行测试用例,但会编译测试用例类-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>

			<!-- 2.maven-jar-plugin 这个插件通常用法是将maven工程打成 jar 包-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
                    <!-- 过滤掉不希望包含在jar中的文件  -->
					<excludes>
						<exclude>*.properties</exclude>
						<exclude>*.xml</exclude>
						<exclude>*.yml</exclude>
						<exclude>*.sh</exclude>
					</excludes>
					<archive>
						<manifest>
                            <!-- 是否添加依赖的jar路径配置 -->
							<addClasspath>true</addClasspath>
                            <!-- 指定入口函数 -->
							<mainClass>com.ebupt.crbt.Application</mainClass>
                            <!-- 依赖的jar包存放位置 -->
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
					</archive>
				</configuration>
			</plugin>
            
            <!-- 3.结构定制化的打包,打包规则定义到一个xml文件中 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2-beta-5</version>
				<configuration>
					<descriptors>
                        <!-- 描述文件路径 -->
						<descriptor>src/main/resources/package.xml</descriptor>
					</descriptors>
                    <!-- 打包解压后的目录名:${project.artifactId}是指:项目的artifactId -->
                    <finalName>${project.artifactId}</finalName>
                    <!-- 打包压缩包位置 -->
                    <outputDirectory>${project.build.directory}/</outputDirectory>
				</configuration>
                <!-- 加上下面这一段,使用 maven package命令,不加则使用mvn assembly--> 
				<executions>
					<execution>
						<id>make-assembly</id><!--名字任意 --> 
						<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
						<goals>
							<goal>single</goal><!-- 只运行一次 -->
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>

	</build>
  • package.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    
    <!-- 目标文件名则是artifactId−{id}.zip -->  
   <id>webApp</id>
   <formats>
    <!-- 最终打包成一个用于发布的zip文件 -->  
   	<format>zip</format>
   </formats>
   <includeBaseDirectory>true</includeBaseDirectory>  
   <!--相关说明文件放入根目录下  -->
   <fileSets>
    <fileSet>
    	<directory>${project.basedir}/src/main/resources</directory>
   		<outputDirectory>/</outputDirectory>
   		<includes>
		<include>*.sh</include>
		<include>*.properties</include>
		<include>*.json</include>
		<include>*.properties</include>
		<include>*.xml</include>
		<include>*.md</include>
	</includes>
    </fileSet>
   	<!--配置文件放入config  -->
   	<fileSet>
   		<directory>${project.basedir}/src/main/resources</directory>
   		<outputDirectory>/config</outputDirectory>
   		<includes>
   			<include>*.yml</include>
   		</includes>
   	</fileSet>
   	<!--程序的jar包放在根目录下,方便扫描config下的配置文件  -->
     <fileSet>
   		<directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>  
                <include>*.jar</include>    
           </includes>
   	</fileSet>
    <!-- 启动文件放在bin下  -->
      <fileSet>
          <directory>${project.basedir}/src/main/resources</directory>
          <outputDirectory>/bin</outputDirectory>
          <includes>
              <include>*.sh</include>
          </includes>
      </fileSet>
  </fileSets>
    
    <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
   <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>/lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

Maven项目中resources配置总结

# Maven项目的标准目录结构
src
 main
   java         #源文件 
   resources    #资源文件
   filters   #资源过滤文件
   config   #配置文件
   scripts   #脚本文件
   webapp   #web应用文件
 test
   java    #测试源文件
   resources    #测试资源文件
   filters    #测试资源过滤文件
 it       #集成测试
 assembly    #assembly descriptors
 site    #Site
target
  generated-sources
  classes
  generated-test-sources
  test-classes
  xxx.jar
pom.xml
LICENSE.txt
NOTICE.txt
README.txt

例如:

<build>
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
      <include>application.properties</include>
    </includes>
  </resource>
   <resource>
     <directory>src/main/resources</directory>
     <filtering>false</filtering>
       <excludes>
           <exclude>application.properties</exclude>
       </excludes>
   </resource>
   <!-- 最后是取两个配置分别过滤的文件集合的并集。 -->
</resources>
</build>
  • 标签 <directory> 指定资源文件目录;

  • 标签 <include> 指定资源文件目录中,哪些文件被打包。(包含)

  • 标签 <excludes> 指定资源文件目录中,哪些文件不被打包。(排除)

  • 标签 <filtering> 指配置文件中是否需要用变量进行替换实际的值。

    • # application.properties
      application.user=${username111}
      application.password=${password222}
      

      文件中使用${keyword}占位符来标识变量。这时候可以在pom.xml文件中定义变量的取值:如下

      <properties>
          <username111>mysql</username111>
          <password222>password123</password222>
      </properties>
      
 类似资料: