Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven First Project

Maven First Project

Creating a Maven Project

Generating a Project with Maven Archetype

Maven provides project templates (Archetype) that can quickly generate a standard project structure. The most commonly used is maven-archetype-quickstart (basic Java project template).

Execute a command:



mvn archetype:generate \

    -DgroupId=com.example \

    -DartifactId=my-first-app \

    -DarchetypeArtifactId=maven-archetype-quickstart \

    -DinteractiveMode=false

Parameter Description:

Parameters Description
-DgroupId Organization name (such as a company domain name reversed)
-DartifactId Project name (will become the project folder name)
-DarchetypeArtifactId Template to use (quickstart is a basic Java project)
-DinteractiveMode=false Non-interactive mode (avoid manual confirmation)

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

pom.xml Explained

This is Maven's core configuration file, defining the project's basic information and dependencies.

The 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 (such as application.properties)
src/test/java Test code
src/test/resources Test resource files

Example 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

)

;

 

// Example test

   

}

}


Building and Running

Common Maven Commands

Command Purpose
mvn compile Compile source code
mvn test Running tests
mvn package Package (generate .jar file)
mvn install Install to the local repository (for other projects to depend on)
mvn clean Clean up target directory

Complete Build Process

1. Compile the project:



mvn compile

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

Generate 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

Add Dependency

For example, add Gson for JSON processing:

Modify pom.xml:



    

    

        junit

        junit

        4.12

        test

    



    

    

        com.google.code.gson

        gson

        2.8.9

    



Run mvn compile, and Maven will automatically download Gson.

Modify the 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!"


Common questions

Dependency download failure

Cause: network issues or repository unavailable.

Solutions:

Check the network connection.

Configure a domestic mirror repository (such as Alibaba Cloud):





    aliyun

    https://maven.aliyun.com/repository/public

    central



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:





    11

    11




Summary

  1. Create project:mvn archetype:generate Generate a standard structure.

  2. Project Structure:

  3. pom.xml Define dependencies and configuration.

  4. src/main/java for main code,src/test/java for test code.

  5. -

    pom.xml Define dependencies and configuration.

    -

    src/main/java for main code,src/test/java for test code.

    1. Build commands:

    2. mvn compile → mvn test → mvn package。

    3. -

      mvn compile → mvn test → mvn package。

      1. Run:java -jar or java -cp Executing .jar files.