Maven QuickStart
验证:未记录
- 适用版本
- 未记录
- 验证范围
- 尚未登记验证记录。
本文缩写(英文全称与中文含义)
| 缩写 | 英文全称/名称 | 中文含义 |
|---|---|---|
| CI | Continuous Integration | 持续集成 |
| JDK | Java Development Kit | Java 开发工具包 |
| POM | Project Object Model | Maven 项目对象模型 |
Links#
https://maven.apache.org/guides/getting-started/
https://maven.apache.org/wrapper/
https://maven.apache.org/plugins/maven-compiler-plugin/
https://maven.apache.org/surefire/maven-surefire-plugin/1. Goal#
create a Java project:
compile with JDK 21
run unit test
build jar
use Maven Wrapper2. Project Structure#
order-api
├── pom.xml
├── mvnw
├── mvnw.cmd
├── .mvn
│ └── wrapper
├── src
│ ├── main/java/com/example/order/App.java
│ └── test/java/com/example/order/AppTest.java3. Minimal POM#
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>order-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>pluginManagement 声明默认版本和配置,不代表插件一定执行;排查时核对 effective POM、plugins 与 lifecycle phase。
pluginManagement:
只声明默认版本和默认配置,不等于一定执行 plugin4. Code#
package com.example.order;
public class App {
public String hello(String name) {
return "hello " + name;
}
}package com.example.order;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppTest {
@Test
void hello() {
assertEquals("hello order", new App().hello("order"));
}
}5. Maven Wrapper#
mvn wrapper:wrapper
./mvnw -vMaven Wrapper:
project-local Maven launcher
CI and developers use the same Maven version
commit mvnw, mvnw.cmd, and .mvn/wrapper6. Build#
./mvnw clean test
./mvnw clean package
java -cp target/order-api-1.0.0-SNAPSHOT.jar com.example.order.App7. Useful Options#
./mvnw -B clean verify
./mvnw -DskipTests package
./mvnw -Dmaven.test.skip=true package
./mvnw -DskipITs verify-DskipTests:
compile tests but do not run them
-Dmaven.test.skip=true:
skip test compilation and test execution
prefer:
use -DskipTests only when another pipeline stage runs tests