1. Maven环境搭建
1.1 Maven简介
1.2 Maven下载及环境设置
Maven下载地址:http://maven.apache.org/download.cgi
当前下载版本:apache-maven-3.5.0-bin.zip,解压之后路径 D:\Program Files\apache-maven-3.5.0
Maven环境变量设置:
注:设置Maven环境变量之前,需先设置JAVA_HOME系统变量,参考 Java基础:Java简介及安装配置(1)。
(1)新增系统变量【MAVEN_HOME】,值:D:\Program Files\apache-maven-3.5.0
(2)系统变量【Path】追加值: %MAVEN_HOME%\bin
(3)运行cmd,检查是否配置成功。
mvn -version
1.3 Eclipse设置Maven Repository
选择菜单:Window -> Maven -> User Settings
设置值:maven安装路径
1.4 本地仓储配置
本地仓储配置文件:D:\Program Files\apache-maven-3.5.0\conf\settings.xml
修改配置:
找到以下部分代码,本地仓储默认配置为 ${user.home}/.m2/repository。
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
修改本地仓储,保存路径 D:\Program Files\apache-maven-3.5.0\repository
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\Program Files\apache-maven-3.5.0\repository</localRepository>
1.5 Maven设置阿里云仓储
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
1.6 设置默认JDK
方式一:项目pom.xml文件中设置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
方式二:在Maven的settings.xml文件中设置
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2. Maven基本操作
2.1 Maven项目约定目录
MavenProjectRoot
|----src
| |----main
| | |----java —— 存放项目的.java文件
| | |----resource —— 存放项目资源文件,如spring, hibernate配置文件
| |----test
| | |----java —— 存放所有测试.java文件,如JUnit测试类
| | |----resource —— 存放项目资源文件,如spring, hibernate配置文件
|----target —— 项目输出位置
|----pom.xml —— 用于标识该项目是一个Maven项目
2.2 创建Maven项目
(1)生成项目
mvn archetype:generate
Define value for property 'groupId': libing
Define value for property 'artifactId': com-test-api
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' libing: : com.libing.test
Confirm properties configuration:
groupId: libing
artifactId: com-test-api
version: 1.0-SNAPSHOT
package: com.libing.test
查看项目生成目录:
cd /d F:\workspace\com-test-api
F:\workspace\com-test-api>tree
文件夹 PATH 列表
卷序列号为 0000002C 962B:5AD5
F:.
└─src
├─main
│ └─java
│ └─com
│ └─libing
│ └─test
└─test
└─java
└─com
└─libing
└─test
创建时指定类型:
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=libing -DartifactId=libing-test-api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=libing -DartifactId=libing-test-api -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
(2)编译项目
F:\workspace\com-test-api>mvn compile
完成编译后,生成target文件夹及编译文件。
F:\workspace\com-test-api>tree
文件夹 PATH 列表
卷序列号为 0000005E 962B:5AD5
F:.
├─src
│ ├─main
│ │ └─java
│ │ └─com
│ │ └─libing
│ │ └─test
│ └─test
│ └─java
│ └─com
│ └─libing
│ └─test
└─target
├─classes
│ └─com
│ └─libing
│ └─test
└─maven-status
└─maven-compiler-plugin
└─compile
└─default-compile
(3)打包项目
F:\workspace\com-test-api>mvn package
打包完成之后,在F:\workspace\com-test-api\target中生成com-test-api-1.0-SNAPSHOT.jar。