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 Wrapper
2. 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.java
3. 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:
只声明默认版本和默认配置,不等于一定执行 plugin
4. 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 -v
Maven Wrapper:
project-local Maven launcher
CI and developers use the same Maven version
commit mvnw, mvnw.cmd, and .mvn/wrapper
6. Build#
./mvnw clean test
./mvnw clean package
java -cp target/order-api-1.0.0-SNAPSHOT.jar com.example.order.App
7. 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