Maven安装与配置(Linux)

1 环境

1.1 Ubuntu 16.04;
1.2 java 1.8;
1.3 Maven插件;

2 下载maven安装包

$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

3 安装maven

3.1 解压安装包:

$ sudo tar xf apache-maven-3.6.3-bin.tar.gz -C /opt/maven/
$ sudo mv apache-maven-3.6.3/ maven

3.2 配置maven环境变量:

$ sudo vim /etc/profile
export MAVEN_HOME=/opt/maven
export PATH=${MAVEN_HOME}/bin:$PATH
$ source /etc/profile

3.3 验证安装:

$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 1.8.0_251, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_251/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-106-generic", arch: "amd64", family: "unix"

$ 

4 创建maven代码仓库

4.1 创建maven本地仓库:

$ mkdir -p /home/shaw/data/maven/local_repository

4.2 配置仓库路径:

$ sudo vim /opt/maven/conf/settings.xml

修改本地仓库路径:

<localRepository>/home/shaw/data/maven/local_repository</localRepository>

修改国内仓库地址:

<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>*</mirrorOf>
  <name>Nexus aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

5 创建项目

5.1 创建mavendemo项目

maven:版本2为create,版本3为generate 创建

$ mvn archetype:generate -DgroupId=mavendemo -DartifactId=mavendemo -DpackageName=mavendemo
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (6.4 kB at 4.2 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom (9.2 kB at 25 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/22/maven-parent-22.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/22/maven-parent-22.pom (30 kB at 55 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11.pom (15 kB at 40 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar (27 kB at 70 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom (5.6 kB at 16 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.jar (27 kB at 71 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.pom (21 kB at 55 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/24/maven-plugins-24.pom
..................................
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1651: 
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
7: 1.3
8: 1.4
Choose a number: 8: 8
[INFO] Using property: groupId = mavendemo
[INFO] Using property: artifactId = mavendemo
Define value for property 'version' 1.0-SNAPSHOT: : 
[INFO] Using property: package = mavendemo
Confirm properties configuration:
groupId: mavendemo
artifactId: mavendemo
version: 1.0-SNAPSHOT
package: mavendemo
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: mavendemo
[INFO] Parameter: artifactId, Value: mavendemo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: mavendemo
[INFO] Parameter: packageInPathFormat, Value: mavendemo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: mavendemo
[INFO] Parameter: groupId, Value: mavendemo
[INFO] Parameter: artifactId, Value: mavendemo
[INFO] Project created from Archetype in dir: /home/uftp/02_opensource/02_java/mavendemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  06:16 min
[INFO] Finished at: 2020-07-15T23:56:31-07:00
[INFO] ------------------------------------------------------------------------

5.2 修改pom.xml配置

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

5.3 设置本地仓库

本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库。
Maven缺省的本地仓库路径为${user.home}/.m2/repository

5.4 编译项目

编译maven项目:

$ cd mavendemo
$ mvn package 
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< mavendemo:mavendemo >-------------------------
[INFO] Building mavendemo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ mavendemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/uftp/02_opensource/02_java/mavendemo/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ mavendemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/uftp/02_opensource/02_java/mavendemo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ mavendemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/uftp/02_opensource/02_java/mavendemo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ mavendemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/uftp/02_opensource/02_java/mavendemo/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ mavendemo ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running mavendemo.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 s - in mavendemo.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ mavendemo ---
[INFO] Building jar: /home/uftp/02_opensource/02_java/mavendemo/target/mavendemo-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.373 s
[INFO] Finished at: 2020-07-15T23:57:36-07:00
[INFO] ------------------------------------------------------------------------

运行程序:

$ java -cp target/mavendemo-1.0-SNAPSHOT.jar mavendemo.App 
Hello World!

Maven常见命令:

序号 命令 说明
1 mvn compile 编译项目
2 mvn test 编译运行单元测试
3 mvn package 打包(jar or war)
4 mvn install 将项目安装到本地仓库
5 mvn clean 清空项目
6 mvn eclipse:eclipse 生成eclipse工程

你可能感兴趣的:(Maven安装与配置(Linux))