码迷,mamicode.com
首页 > 其他好文 > 详细

maven环境快速搭建

时间:2016-07-20 01:02:55      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

----------------准备工作-------------

Jdk  1.5以上java开发环境。

Eclipse IDE 一个。

Maven 3.0.3下载地址: http://maven.apache.org/docs/3.0.3/release-notes.html

 

第一步:配置maven环境

我的电脑-----属性----高级-----环境变量-----环境变量-----新建

变量名:MAVEN_HOME

变量值:F:\maven\apache-maven-3.0.3

找到path 

在环境变量值尾部加入:;%MAVEN_HOME%\bin;

 

打开命令提示符(开始---运行---cmd),检查我们的java环境和maven环境是否有误。 

技术分享

设置MAVEN_OPTS环境变量(可选配置)

  由于Maven命令实际上是执行了Java命令,所以可以通过JAVA命令参数的方式来设置MAVEN运行参数。MAVEN_OPTS环境变量正是用于此用途

  MAVEN_OPTS

  -Xms128m -Xmx512m,分别设置JVM的最小和最大内存,如下图所示:

  技术分享

 

第二步:修改仓库位置

 

修改仓库地址,仓库用于存放我们项目所依赖的所有jar包。

我的仓库路径:F:\maven\repo----这个路径是我自己创建,你可以将路径创建在任何位置。

我们打开F:\maven\apache-maven-3.3.9\conf\目录下的setting.xml文件,设置成我们创建的仓库路径

技术分享

可以使用命令来验证下:mvn help:system 

该命令会打印出所有的java系统属性和环境变量。这些信息对我们日常的编程工作很有帮助。

如果运行的过程中没有错误,打开我们仓库(F:\maven\repo)会发现里面多了一些文件。这些文件就是我们从maven的中央仓库下载到本地仓库的。

 

第三步:创建maven项目

方法一:使用命令行创建简单的MAVEN项目

F:\maven>mvn archetype:generate -DgroupId=com.fwx.test -DartifactId=hello -DpackageName=com.fwx.test -Dversion=1.0 -DarchetypeCatalog=internal 

因为是第一次构建项目,所有依赖的jar包都要从maven的中央仓库下载,所以需要时间等待。等以后我们的本地仓库中积累了我们常用的jar包后,我们的开发将变得非常规范和方便。

F:\maven>mvn archetype:generate -DgroupId=com.fwx.test -DartifactId=hello -Dpac
ageName=com.fwx.test -Dversion=1.0 -DarchetypeCatalog=internal
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/mave
-archetype-plugin/maven-metadata.xml
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
archetype-plugin/maven-metadata.xml (759 B at 0.4 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/mave
-archetype-plugin/2.4/maven-archetype-plugin-2.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
archetype-plugin/2.4/maven-archetype-plugin-2.4.pom (9 KB at 1.6 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/mave
-archetype-plugin/2.4/maven-archetype-plugin-2.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
archetype-plugin/2.4/maven-archetype-plugin-2.4.jar (92 KB at 15.5 KB/sec)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4: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: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archet
pe which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An arch
type which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype
which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An arch
type which contains a sample Maven plugin site.
      This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetyp
 which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An arche
ype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype w
ich contains a sample Maven site which demonstrates
      some of the supported document types like APT, XDoc, and FML and demonstr
tes how
      to i18n your site. This archetype can be layered upon an existing Maven p
oject.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An arch
type which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetyp
 which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive c
ntains): 7: y
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archet
pe which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An arch
type which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype
which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An arch
type which contains a sample Maven plugin site.
      This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetyp
 which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An arche
ype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype w
ich contains a sample Maven site which demonstrates
      some of the supported document types like APT, XDoc, and FML and demonstr
tes how
      to i18n your site. This archetype can be layered upon an existing Maven p
oject.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An arch
type which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetyp
 which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive c
ntains): 7: 7
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/m
ven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma
en-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar (7 KB at 5.8 KB/
ec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/m
ven-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/ma
en-archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom (2 KB at 3.6 KB/
ec)
[INFO] Using property: groupId = com.fwx.test
[INFO] Using property: artifactId = hello
[INFO] Using property: version = 1.0
[INFO] Using property: package = com.fwx.test
Confirm properties configuration:
groupId: com.fwx.test
artifactId: hello
version: 1.0
package: com.fwx.test
 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.fwx.test
[INFO] Parameter: packageName, Value: com.fwx.test
[INFO] Parameter: package, Value: com.fwx.test
[INFO] Parameter: artifactId, Value: hello
[INFO] Parameter: basedir, Value: F:\maven
[INFO] Parameter: version, Value: 1.0
[INFO] project created from Old (1.x) Archetype in dir: F:\maven\hello
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:45 min
[INFO] Finished at: 2016-07-19T22:20:53+08:00
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------

这样会生成一个hello项目。

F:\maven\hello
   |----src
   |     |----main
   |     |         |----java ——存放项目的.java文件
   |     |----test
   |     |         |----java ——存放所有测试.java文件,如JUnit测试类
   |----pom.xml ----用于标识该项目是一个Maven项目

pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- POM使用的对象模型的版本,这个值很少改动-->
<modelVersion>4.0.0</modelVersion>
<!--groupId指的是项目名的项目组,默认就是包名-->
<groupId>com.fwx.test</groupId>
<!--artifactId指的是项目中的某一个模块,默认命名方式是"项目名-模块名"-->
<artifactId>hello</artifactId>
<!--version指的是版本,这里使用的是Maven的快照版本,可以经常看到SNAPSHOT这个版本,表明项目处于开发阶段-->
<version>1.0</version>
<packaging>jar</packaging>
<!-- 项目的显示名称 -->
<name>hello</name>
<!-- 项目站点,通常用于maven产生的文档中 -->
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

 

方法二:手动创建Maven项目 

MavenProjectRoot(项目根目录)
   |----src
   |     |----main
   |     |         |----java ——存放项目的.java文件
   |     |         |----resources ——存放项目资源文件,如spring, hibernate配置文件
   |     |----test
   |     |         |----java ——存放所有测试.java文件,如JUnit测试类
   |     |         |----resources ——存放项目资源文件,如spring, hibernate配置文件
   |----target ——项目输出位置
   |----pom.xml ----用于标识该项目是一个Maven项目

只要按照这个目录创建即可。

 

第四步:使用Maven编译

技术分享

技术分享

这两种方式都行。 

build success之后会在项目的根目录下生成一个target目录。

可以使用mvn clean命令,删除target文件夹。

 

项目是编译完了,但项目的目录结构还不是我们想要的eclipse的项目结构,是不能导入到eclipse中的。所以,还需要执行一个命令:mvn eclipse:eclipse 命令执行完成后就得我们需要的项目目录了。

 

第五步:导入eclipse工具 

先配置maven仓库路径

Window----Perferences-----java-----Build Path-----Classpath Variables

New一个变量的类路径。

技术分享

Name :MAVEN_REPO   注意这个名字必须要大写。

Path :F:/maven/repo  点击“Folder…”找到有本地仓库的位置。

然后,

技术分享

技术分享

然后往eclipse中导入maven工程即可。

 

eclipse导入Maven项目报错:

An internal error occurred during: "Importing Maven projects". Unsupported IClasspathEntry kind=4

解决方案:
   1. Right-click on your project, select Maven -> Remove Maven Nature.
   2.Open you terminal, go to your project folder and do “mvn eclipse:clean”
   3.Right click on your Project and select “Configure -> Convert into Maven Project”

 

第六步:包的更新与下载 

打开项目发现我们junit 是3.8.1的,有点老了。那我想换成4.7的,如何通过maven的方式更换呢。其实,很简单,打开我们项目下的的pom.xml文件。

技术分享
……
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
……
技术分享

更改变,junit的版本号,然后重新执行:mvn eclipse:eclipse  

Maven 中央仓库地址:http://search.maven.org

 

假如,我们想下载一个struts 的jar包。在搜索框内搜索strruts ,会要列出中央仓库中的所有struts版本。

列表的格式与我们pom.xml配置文件的格式是对应的。 

 

技术分享 

然后在pom.xml中加入:

<groupId>stuts</groupId>

 <artifactId>struts-scripting</artifactId>

 <version>1.0.1</version>

然后更新项目就可从中央仓库下载我们想要的任意jar包(必须是开源的包)

 

maven环境快速搭建

标签:

原文地址:http://www.cnblogs.com/siv8/p/5686764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!