Maven安装与使用入门

本文档详细介绍了在Windows10系统中安装和配置Maven的步骤,包括设置环境变量、检查安装成功、配置本地库、添加镜像仓库、创建首个Maven项目以及在Eclipse中配置Maven的教程。
摘要由CSDN通过智能技术生成

1. 安装环境

操作系统:windows10 64位
JDK:jdk1.7.0_79
Maven: apache-maven-3.3.3

2. 配置Maven环境变量

M2_HOME=D:\Program File\apache-maven-3.3.3
PATH=%M2_HOME%/bin

3.使用mvn -v命令检查是否安装成功

C:\Users\Administrator>mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: D:\Program Files\apache-maven-3.3.3
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_79\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"

4. 配置maven本地库

修改%M2_HOME%/conf/settings.xml,添加localRepository,如下所示:

<localRepositoy>G:/maven/repo/</localRepository>

5. 添加中央仓库镜像

默认的中央仓库下载速度比较慢,可以添加国内的maven仓库,配置如下:

<mirror>
    <id>CN</id>
    <name>OSChina Central</name>                                            
    <url>http://maven.oschina.net/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

6. 创建第一个Maven工程

假设工作目录为G:/mvnworkspace,创建第一个Maven工程。执行mvn archetype:generate-Dfilter=org.apache:quickstart,如下所示

G:\mvnworkspace>mvn archetype:generate -Dfilter=org.apache:quickstart
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> org.apache.brooklyn:brooklyn-archetype-quickstart (This project defines
 an archetype for creating new projects which consume brooklyn,
    including an example application and an example new entity type,
    able to build an OSGi JAR and a binary assembly, with logging and READMEs.)
2: remote -> org.apache.flink:flink-quickstart-java (-)
3: remote -> org.apache.flink:flink-quickstart-scala (-)
4: remote -> org.apache.isis:quickstart-archetype (-)
5: remote -> org.apache.isis.archetype:quickstart_wicket_restful_jdo-archetype (-)
6: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype 
which contains a sample Maven project.)
7: remote -> org.apache.tapestry:quickstart (-)
8: remote -> org.apache.tuscany.sca:tuscany-quickstart (Create a simple Apache 
Tuscany SCA webapp project)
9: remote -> org.apache.tuscany.sca:tuscany-quickstart-bpel (Create an SCA Webapp project using BPEL)
10: remote -> org.apache.tuscany.sca:tuscany-quickstart-jsf (Create a Tuscany SCA and JSF project)
11: remote -> org.apache.tuscany.sca:tuscany-quickstart-jsonp (-)
12: remote -> org.apache.tuscany.sca:tuscany-quickstart-stripes (Create a simple 
Apache Tuscany SCA webapp project using the Stripes Web Framework)
13: remote -> org.apache.wicket:wicket-archetype-quickstart (-)

/* 选择maven模板 */
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 6: 6

/* 选择模板版本 */
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
Choose a number: 6: 6

/* 输入项目的groupId */
Define value for property 'groupId': : com.inspur.trc

/* 输入项目名称 */
Define value for property 'artifactId': : app

/* 输入项目版本信息 */
Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0

/* 输入项目包路径 */
Define value for property 'package':  com.inspur.trc: :

/* 确认输入信息 */
Confirm properties configuration:
groupId: com.inspur.trc
artifactId: app
version: 1.0.0
package: com.inspur.trc
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: 
maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.inspur.trc
[INFO] Parameter: packageName, Value: com.inspur.trc
[INFO] Parameter: package, Value: com.inspur.trc
[INFO] Parameter: artifactId, Value: app
[INFO] Parameter: basedir, Value: G:\mvnworkspace
[INFO] Parameter: version, Value: 1.0.0
[INFO] project created from Old (1.x) Archetype in dir: G:\mvnworkspace\app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:07 min
[INFO] Finished at: 2015-08-15T15:58:10+08:00
[INFO] Final Memory: 17M/211M
[INFO] ------------------------------------------------------------------------

7. 项目打包

G:\mvnworkspace\app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building app 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory G:\mvnworkspace\app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory G:\mvnworkspace\app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app ---
[INFO] Surefire report directory: G:\mvnworkspace\app\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.inspur.trc.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.451 s
[INFO] Finished at: 2015-08-15T16:20:08+08:00
[INFO] Final Memory: 11M/211M
[INFO] ------------------------------------------------------------------------

8. 修改eclipse中的maven配置

Window->Preferences->Maven->Installations配置maven安装了路径
Window->Preferences->User Settings选择配置文件位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值