Maven 构建配置文件学习笔记

1. 什么是Maven构建配置文件?

Maven是Java项目管理和构建工具,通过pom.xml配置文件来管理项目依赖和构建过程。pom.xml是Maven项目的核心配置文件,它定义了项目的依赖、插件、构建目标等信息。

2. pom.xml文件的基本结构

pom.xml文件由一系列元素组成,它们按照顺序出现。以下是一个基本的pom.xml文件结构示例:

xmlCopy Code
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 基本信息 --> <groupId>com.example</groupId> <artifactId>example-project</artifactId> <version>1.0-SNAPSHOT</version> <name>Example Project</name> <!-- 依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- 插件 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

3. 常用的配置元素

(1) groupId、artifactId、version

groupIdartifactIdversion 是一个Maven项目的核心标识信息。其中,groupId表示项目组,artifactId表示项目名,version表示版本号。

xmlCopy Code
<groupId>com.example</groupId> <artifactId>example-project</artifactId> <version>1.0-SNAPSHOT</version>

(2) dependencies

dependencies元素用于定义项目的依赖,包括依赖的groupIdartifactIdversion等信息。

xmlCopy Code
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>

(3) plugins

plugins元素用于配置插件,包括groupIdartifactIdversion等信息。插件可以用来扩展Maven的功能,例如编译代码、打包、运行测试等操作。

xmlCopy Code
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>

(4) profiles

profiles元素用于定义不同的构建环境,例如不同的操作系统、不同的Java版本等。在每个profile中可以定义对应的依赖、插件、配置等信息。

xmlCopy Code
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <!-- 配置dev环境的插件等信息 --> </build> </profile> <profile> <id>prod</id> <build> <!-- 配置prod环境的插件等信息 --> </build> </profile> </profiles>

4. 示例

以下是一个示例pom.xml文件:

xmlCopy Code
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 基本信息 --> <groupId>com.example</groupId> <artifactId>example-project</artifactId> <version>1.0-SNAPSHOT</version> <name>Example Project</name> <!-- 依赖 --> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> </dependencies> <!-- 插件 --> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> <executions> <execution> <id>pre-unit-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <propertyName>coverageAgent</propertyName> </configuration> </execution> <execution> <id>post-unit-test</id> <goals> <goal>report</goal> </goals> <phase>test</phase> <configuration> <dataFile>target/jacoco.exec</dataFile> <outputDirectory>target/jacoco-ut/</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> <!-- 配置profile --> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>

该示例项目定义了一个com.example:example-project:1.0-SNAPSHOT的Java项目,它依赖于commons-lang3:3.8.1junit:junit:4.12。同时,它配置了jacoco-maven-plugin插件来生成测试覆盖率报告,并定义了devprod两个环境的构建配置。