Maven Setup
Maven is a Java-based tool, so the first thing to do is install the JDK.
Download the JDK (recommended JDK 8/11/17):
* (https://www.oracle.com/java/technologies/javase-downloads.html)
* (https://adoptium.net/)
If you haven't installed the JDK yet, you can refer to our (#).
### System Requirements
| Item | Requirement |
| --- | --- |
| JDK | Maven 3.3 requires JDK 1.7 or above. Maven 3.2 requires JDK 1.6 or above. Maven 3.0/3.1 requires JDK 1.5 or above. |
| Memory | No minimum requirement. |
| Disk | Maven itself requires approximately 10 MB of space for installation. Additionally, extra disk space will be used for your local Maven repository. The size of your local repository depends on usage, but expect at least 500 MB. |
| Operating System | No minimum requirement. |
### Check Java Installation
| Operating System | Task | Command |
| --- | --- | --- |
| Windows | Open Command Console | c:> java -version |
| Linux | Open Command Terminal | # java -version |
| Mac | Open Terminal | $ java -version |
### Maven Download
Maven download address: [http://maven.apache.org/download.cgi](http://maven.apache.org/download.cgi)
!(#)
Download the corresponding package for your platform:
| 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 situation) |
| --- | --- |
| Windows | E:Mavenapache-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 | Configuration |
| --- | --- |
| Windows | Right-click "Computer", select "Properties", then click "Advanced system settings", click "Environment Variables" to set environment variables. The following system variables need to be configured: Create a new system variable **MAVEN_HOME**, variable value: E:Mavenapache-maven-3.3.9 !(#) Edit the system variable **Path**, add 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/ Edit the **/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.9export 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, it means 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/ Edit the **/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.9export 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, it means 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.9Java version: 1.8.0_31, vendor: Oracle CorporationJava 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 Installation
mvn -v # Should output Maven version and Java information
### Configure Maven Local Repository
Maven downloads dependencies from a remote repository by default and stores them locally:
Default local repository path:
* Windows: `C:Users.m2repository`
* Linux/macOS: `~/.m2/repository`
Modify 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 # Project configuration fileβββ src/β βββ main/ # Main codeβ β βββ java/ # Java source codeβ βββ test/ # Test codeβ βββ java/ # Test classes
### Understanding pom.xml
Generated pom.xml example:
4.0.0 com.example my-first-app 1.0-SNAPSHOT junit junit 4.12 test
### Compile and Run
mvn compile # Compile the project mvn test # Run tests mvn package # Package into a .jar file java -jar target/my-first-app-1.0-SNAPSHOT.jar # Run (if executable)
YouTip