Maven Start
## Create Maven Project
### Generate Project Using Maven Archetype
Maven provides project templates (Archetype) that can quickly generate standard project structures. The most commonly used is maven-archetype-quickstart (basic Java project template).
Execute 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., reversed company domain) |
| `-DartifactId` | Project name (will become the project folder name) |
| `-DarchetypeArtifactId` | Template used (`quickstart` is the basic Java project) |
| `-DinteractiveMode=false` | Non-interactive mode (to avoid manual confirmation) |
Project structure generated after execution:
my-first-app/
βββ pom.xml # Maven project configuration file
βββ src/
β βββ main/ # Main code directory
β β βββ java/ # Java source code
β β βββ com/example/App.java # Auto-generated sample class
β βββ test/ # Test code directory
β βββ java/ # Test classes
β βββ com/example/AppTest.java # Auto-generated test class
* * *
## Project Structure Analysis
### pom.xml Details
This is Maven's core configuration file that defines basic project information and dependencies.
Generated pom.xml example:
4.0.0
com.example
my-first-app
1.0-SNAPSHOT
jar
my-first-app
http://www.example.com
junit
junit
4.12
test
### Source Code Structure
| Directory | Purpose |
| --- | --- |
| `src/main/java` | Main Java source code |
| `src/main/resources` | Configuration files (e.g., `application.properties`) |
| `src/test/java` | Test code |
| `src/test/resources` | Test resource files |
### Sample Code:
**Main Class App.java**
## Example
package com.example;
public class App {
public static void main(String[] args){
System.out.println("Hello Maven!");
}
}
**Test Class AppTest.java:**
## Example
package com.example;
import org.junit.Test;
import static org.junit.Assert.*;
public class AppTest {
@Test
public void testApp(){
assertTrue(true); // Sample test
}
}
* * *
## Build and Run
### Common Maven Commands
| Command | Purpose |
| --- | --- |
| `mvn compile` | Compile source code |
| `mvn test` | Run tests |
| `mvn package` | Package (generate `.jar` file) |
| `mvn install` | Install to local repository (for other projects to depend on) |
| `mvn clean` | Clean `target` directory |
### Complete Build Process
1. Compile project:
mvn compile
The compiled .class files will be placed in the target/classes directory.
2. Run tests:
mvn test
Execute 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 program:
java -cp target/my-first-app-1.0-SNAPSHOT.jar com.example.App
5. Output:
Hello Maven!
* * *
## Extension: Modify Project
### Add Dependencies
For example, add (#) for JSON processing:
Modify pom.xml:
junit
junit
4.12
test
com.google.code.gson
gson
2.8.9
Run mvn compile, Maven will automatically download Gson.
### Modify Main Class to Use Gson
## Example
package com.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!"
* * *
## Frequently Asked Questions
### Dependency Download Failed
Cause: Network issues or repository unavailable.
**Solution:**
Check network connection.
Configure domestic mirror repository (such as Aliyun):
aliyun
https://maven.aliyun.com/repository/public
<mirror
YouTip