Maven First Project
Create Maven Project
Generate a project using Maven Archetype
Maven provides project templates (Archetypes) to quickly generate a standard project structure. The most commonly used is maven-archetype-quickstart (basic Java project template).
Execute the command:
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-first-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Parameter Description:
| Parameter | Description |
|---|---|
-DgroupId |
Organization name (e.g., company domain in reverse) |
-DartifactId |
Project name (becomes the project folder name) |
-DarchetypeArtifactId |
The template used (quickstartis a basic Java project) |
-DinteractiveMode=false |
Non-interactive mode (avoid manual confirmation) |
The project structure generated after execution:
my-first-app/ ├── pom.xml # Maven 项目配置文件 ├── src/ │ ├── main/ # 主代码目录 │ │ └── java/ # Java 源代码 │ │ └── com/example/App.java # 自动生成的示例类 │ └── test/ # 测试代码目录 │ └── java/ # 测试类 │ └── com/example/AppTest.java # 自动生成的测试类
Project Structure Analysis
Detailed Explanation of pom.xml
This is Maven's core configuration file, defining the project's basic information and dependencies.
Example of the generated pom.xml:
<project>
<!-- POM 模型版本,固定 4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目坐标(唯一标识) -->
<groupId>com.example</groupId>
<artifactId>my-first-app</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 项目打包方式(默认 jar,也可以是 war、pom 等) -->
<packaging>jar</packaging>
<!-- 项目名称和 URL(可选) -->
<name>my-first-app</name>
<url>http://www.example.com</url>
<!-- 依赖管理 -->
<dependencies>
<!-- JUnit 测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> <!-- 仅用于测试 -->
</dependency>
</dependencies>
</project>
Source Code Structure
| Directory | Purpose |
|---|---|
src/main/java |
Main Java source code |
src/main/resources |
Configuration files (such asapplication.properties) |
src/test/java |
Test code |
src/test/resources |
Test resource files |
Example code:
Main class App.java
Example
public class App {
public static void main(String[] args) {
System.out.println("Hello Maven!");
}
}
Test class AppTest.java:
Example
import org.junit.Test;
import static org.junit.Assert.*;
public class AppTest {
@Test
public void testApp() {
assertTrue(true); // Example test
}
}
Building and Running
Common Maven Commands
| Command | Purpose |
|---|---|
mvn compile | Compile source code |
mvn test | Run tests |
mvn package | Package (generates.jarfile) |
mvn install | Install to local repository (for other projects to depend on) |
mvn clean | Cleantargetdirectory |
Complete Build Process
1. Compile the project:
mvn compile
The compiled .class files will be placed in the target/classes directory.
2. Run tests:
mvn test
Execute the test classes under src/test/java.
Test reports are generated in target/surefire-reports.3. Package:
mvn package
Generates target/my-first-app-1.0-SNAPSHOT.jar.
4. Run the program:
java -cp target/my-first-app-1.0-SNAPSHOT.jar com.example.App
5. Output:
Hello Maven!
Extension: Modifying the Project
Adding Dependencies
For example, addGsonfor JSON processing:
Modify pom.xml:
<dependencies>
<!-- 原有 JUnit 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 新增 Gson 依赖 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>Run mvn compile, Maven will automatically download Gson.
Modify the main class to use Gson
Example
import com.google.gson.Gson;
public class App {
public static void main(String[] args) {
Gson gson = new Gson();
String json = gson.toJson("Hello Maven with Gson!");
System.out.println(json);
}
}
Repackage and run:
mvn package java -cp target/my-first-app-1.0-SNAPSHOT.jar com.example.App
Output:
"Hello Maven with Gson!"
Common Problems
Dependency download failure
Cause: Network issue or repository unavailable.
Solution:
Check the network connection.
Configure a domestic mirror repository (e.g., Alibaba Cloud):
<!-- 在 settings.xml 或 pom.xml 中配置 -->
<mirror>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
Alibaba Cloud repository introduction:https://developer.aliyun.com/mvn/guide。
Compilation version issue
Error: javac: invalid target release: 11
Solution: Specify the Java version in pom.xml:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
Summary
Create a project:
mvn archetype:generateGenerate a standard structure.Project structure:
pom.xmlDefine dependencies and configuration.src/main/javaPlace main code,src/test/javaPlace test code.
Build commands:
mvn compile→mvn test→mvn package。
Run:
java -jarorjava -cpExecute.jarfile.