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。