下载安装配置
首先在官网下载二进制版本,避免编译的麻烦:https://maven.apache.org/download.cgi
解压缩,移动到安装目录,我一般会移动到/opt目录:/opt/apache-maven-3.3.9
配置环境变量:
export M2_HOME=/opt/apache-maven-3.3.9 export M2=$M2_HOME/bin export HAVEN_OPTS="-Xms256m -Xmx512" export PATH=$M2_HOME:$M2:$HAVEN_OPTS:$PATH
测试安装成功与否:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00) Maven home: /opt/apache-maven-3.3.9 Java version: 1.8.0_112, vendor: Oracle Corporation Java home: /opt/jdk1.8.0_112/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-327.22.2.el7.x86_64", arch: "amd64", family: "unix"
maven目录结构
test01/ ├── pom.xml └── src ├── main │ └── java │ └── com │ └── imekaku │ └── maven01 │ └── model │ └── HelloWorld.java └── test └── java └── com └── imekaku └── maven01 └── model └── HelloWorldTest.java 13 directories, 3 files
项目下有是src目录和pom.xml
src目录有main, test
main有java
test有java
HelloWorld代码
package com.imekaku.maven01.model; public class HelloWorld { public String sayHello() { return "Hello world!"; } }
package com.imekaku.maven01.model; import org.junit.*; import org.junit.Assert.*; public class HelloWorldTest { @Test public void testHello() { Assert.assertEquals("Hello world!", new HelloWorld().sayHello()); } }
编译,测试:mvn compile
, mvn test
maven自动创建骨架
mvn archetype:generate # 依次输入groupid, artifactid, version, package # 一行 mvn archetype: generate -DgroupId=com.imekaku.maven04 -DartifactId=maven04-demo -Dversion=1.0.0SNAPSHOT -Dpackage=com.imekaku.maven04.demo
添加阿里中央仓库
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>