build的finalName的配置
build标签的finalName属性,用于指定打包结束后的包名称,通常以模块名+版本号进行命名:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
...
</build>
指定编译的java版本、编码、外部lib
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
或者:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib</extdirs><!--指定外部lib-->
</compilerArguments>
</configuration>
</plugin>
设置properties配置文件的编码
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
或者通过插件设置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
...
<encoding>UTF-8</encoding>
...
</configuration>
</plugin>
包含排除某些resource资源
<build>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
</build>
添加或更改新的资源文件夹
<resources>
<resource>
<directory>resources</directory>
</resource>
<resource>
<directory>resource2</directory>
<!-- 指定编译后的路径 target/classes/dir -->
<targetPath>dir/</targetPath>
</resource>
</resources>
编译前替换目标文本中的占位符${}
先开启资源过滤功能:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
开启后会寻找src/main/resources
的所有文件,并将文件中的${xxx}
替换为maven变量值,比如如下这个文件位于资源目录下:
Hello ${name}
Project Version Is ${project.version}
Spring Boot Version Is ${springboot.version}
可以读取properties标签中的变量,也可以访问maven的内部变量
也可以将这些变量放在另一个配置文件中:
<build>
<filters>
<filter>my-filter-values.properties</filter>
</filters>
</build>
或者在执行时,指定变量的值:
mvn resources:resources -Dname="world"
文件在转换时可能存在转义的问题,可以通过定义转义符的方式解决:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<escapeString>\</escapeString>
</configuration>
</plugin>
这样配置文件中的 \${name}
就不会呗Filter处理。
占位符替换需要排除二进制文件
禁止将二进制放入到<filtering>true</filtering>
的resources
中,这可能导致二进制数据损坏。
排除指定扩展名的二进制文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
...
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
...
</configuration>
</plugin>
排除指定的二进制文件:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>src/main/resources/webapp/**.*</exclude>
</excludes>
</resource>
将需要处理的文件单独放在一个文件夹中:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
flatten管理多模块项目的版本
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${maven-flatten-version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
<pomElements>
<dependencies>expand</dependencies>
</pomElements>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
定义版本信息:
<properties>
<maven-flatten-version>1.1.0</maven-flatten-version>
<revision>0.3.0-RC</revision>
</properties>
父pom定义:
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-project</artifactId>
<version>${revision}</version>
子pom定义:
<parent>
<artifactId>nacos-spring-boot-parent</artifactId>
<groupId>com.alibaba.boot</groupId>
<version>${revision}</version>
<relativePath>../nacos-spring-boot-parent/pom.xml</relativePath>
</parent>
.gitignore
中忽略生成的真实项目的pom.xml
文件:
在根项目中管理依赖项版本
在根项目中创建depdencyMangement
,管理子项目的版本在依赖时与父项目完全保持一致:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>business-management-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
也可以管理第三方依赖的版本:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>org.mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
记得定义properties:
<properties>
<mapstruct.version>1.5.2.Final</mapstruct.version>
</properties>
多模块项目如何打包
打包所有模块:
mvn clean install package
打包指定的模块:
mvn clean install -pl web/eweb,web/mweb
打包指定的模块,,并同时打包目标包依赖的模块:
mvn clean install -pl web/eweb -am
打包指定的模块,并同时打包目标包依赖模块 以及 依赖目标包的模块:
mvn clean install -pl web/eweb -am -amd
构建/部署源码包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
Forking: 在Maven构建过程中,有时候为了执行特定任务(如编译、测试等),Maven会选择启动一个新的JVM进程来运行这些任务。这种方式被称为“forking”。
Forking的优点在于可以隔离任务执行环境,防止不同任务间的相互影响,尤其是在需要使用不同JDK版本、内存设置、系统属性或JVM参数时。然而,forking也会带来额外的开销,如启动新进程的时间成本和资源消耗。
jar-no-fork: 在maven-source-plugin的jar目标中,jar-no-fork参数的作用是指示插件在生成源码JAR时不要启动新的JVM进程(即不进行forking)。这意味着插件将在当前Maven构建进程的JVM中直接执行源码打包任务,而不是启动一个新的JVM来完成。
使用jar-no-fork的好处主要包括:
效率提升:由于不需要启动新的JVM进程,jar-no-fork可以减少构建过程中的开销,使得源码打包过程更快捷。
资源节约:避免forking可以减少系统资源的消耗,特别是当频繁进行源码打包时,这有助于减少对系统资源的压力。
生成javadoc
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<charset>UTF-8</charset>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>