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

Maven Environment Configuration

Maven Environment Configuration

Maven is a Java-based tool, so the first thing to do is install the JDK.

Download the JDK (JDK 8/11/17 recommended):

If you have not installed the JDK yet, you can refer to our Java Development Environment Configuration。

System Requirements

Projects Requirement
JDK Maven 3.3 requires JDK 1.7 or above
 Maven 3.2 要求 JDK 1.6 或以上       

 Maven 3.0/3.1 要求 JDK 1.5 或以上 |

| Memory | No minimum requirement | | Disk | Maven's own installation requires about 10 MB of space. In addition, extra disk space will be used for your local Maven repository. The size of your local repository depends on usage, but is expected to be at least 500 MB | | Operating System | No minimum requirement |

Check Java installation

Operating system Task Command
Windows Open the command console
c:> java -version
Linux Open the command terminal
# java -version
Mac Open the terminal
$ java -version

Maven Download

Maven download address:http://maven.apache.org/download.cgi

Download the corresponding package for different platforms:

System Package name
Windows apache-maven-3.3.9-bin.zip
Linux apache-maven-3.3.9-bin.tar.gz
Mac apache-maven-3.3.9-bin.tar.gz

After downloading the package, extract it to the corresponding directory:

System Storage location (can be configured according to your own situation)
Windows E:\Maven\apache-maven-3.3.9
Linux /usr/local/apache-maven-3.3.9
Mac /usr/local/apache-maven-3.3.9

Set Maven Environment Variables

Add the environment variable MAVEN_HOME:

System Configure
Windows

Right-click "Computer", select "Properties", then click "Advanced System Settings", click "Environment Variables", to set the environment variables, the following system variables need to be configured:

Create a new system variable MAVEN_HOME, variable value:E:\Maven\apache-maven-3.3.9

Edit the system variable Path, add the variable value:;%MAVEN_HOME%\bin

Note:Note that multiple values need to be separated by semicolons, then click OK.

Linux

Download and extract:

# wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

tar -xvf apache-maven-3.3.9-bin.tar.gz

sudo mv -f apache-maven-3.3.9 /usr/local/

Editing /etc/profile file sudo vim /etc/profile, add the following code at the end of the file:

export MAVEN_HOME=/usr/local/apache-maven-3.3.9

export PATH=${PATH}:${MAVEN_HOME}/bin

Save the file, and run the following command to make the environment variables take effect:

# source /etc/profile

Enter the following command in the console; if you can see Maven-related version information, then Maven has been installed successfully:

# mvn -v
| | Mac |

Download and extract:

$ curl -O http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

$ tar -xvf  apache-maven-3.3.9-bin.tar.gz

$ sudo mv -f apache-maven-3.3.9 /usr/local/

Editing /etc/profile file sudo vim /etc/profile, add the following code at the end of the file:

export MAVEN_HOME=/usr/local/apache-maven-3.3.9

export PATH=${PATH}:${MAVEN_HOME}/bin

Save the file, and run the following command to make the environment variables take effect:

$ source /etc/profile

Enter the following command in the console; if you can see Maven-related version information, then Maven has been installed successfully:

$ mvn -v

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)

Maven home: /usr/local/apache-maven-3.3.9

Java version: 1.8.0_31, vendor: Oracle Corporation

Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre

Default locale: zh_CN, platform encoding: ISO8859-1

OS name: "mac os x", version: "10.13.4", arch: "x86_64", family: "mac"

|

Verify the installation



mvn -v  # 应输出 Maven 版本和 Java 信息

Configure Maven Local Repository

Maven downloads dependencies from remote repositories by default and stores them locally:

Default local repository path:

Modify the repository location (optional):

Modify in MAVEN_HOME/conf/settings.xml:



/path/to/your/repo


First Maven Project

Create a Project with Maven

Run the following command to generate a standard Java project:



mvn archetype:generate \

    -DgroupId=com.example \

    -DartifactId=my-first-app \

    -DarchetypeArtifactId=maven-archetype-quickstart \

    -DinteractiveMode=false

This will generate a standard Maven project structure:

my-first-app/

├── pom.xml           # 项目配置文件

├── src/

│   ├── main/         # 主代码

│   │   └── java/     # Java 源代码

│   └── test/         # 测试代码

│       └── java/     # 测试类

Interpreting pom.xml

The generated pom.xml example:





    4.0.0

    com.example      

    my-first-app  

    1.0-SNAPSHOT     



    

        

            junit    

            junit

            4.12

            test         

        

    



Compiling and running



mvn compile    # 编译项目

mvn test       # 运行测试

mvn package    # 打包成 .jar 文件

java -jar target/my-first-app-1.0-SNAPSHOT.jar  # 运行(如果可执行)