晨港飞燕的博客

Windows上安装Maven

Maven的具体参考书可以看:《Maven实战》

下载maven可以到:http://maven.apache.org/

Maven的eclipse基本使用可以在这里看到:http://www.iteye.com/topic/1123225

1、把下载下来的maven的zip文件随便解压到一个地方,比如:f:\tool\apache-maven-3.2.5

 

2、设置环境变量(雷同JAVA_HOME):

新加一个"MAVEN":

把"MAVEN"加到Path中:

%MAVEN%\bin

 

3、打开命令行看看maven是否已经正确配置:

打开cmd窗口:输入 mvn -version,出现如下内容表示安装成功。

 

 

4、生成Maven本地仓库。在Maven项目中,用户无需像以前一样自己下载依赖的jar包再放入项目中,只需要定义项目的 pom.xml 文件,对项目使用Maven命令时,Maven会自动从网络上下载相应的包到本地仓库,项目就可以直接使用本地仓库的包。第一次安装Maven时在windows的命令提示符窗口输入 mvn help:system 命令然后回车,等其执行完后就可以在 C:\Users\Admin\.m2\repository 看到 Maven 下载的一些文件。

mvn help:system

运行结果:

此命令运行完后,将会在windows用户文件夹自动生成一个“.m2”的文件夹,里头有一个repository目录,比如:

C:\Users\Admin\.m2

 这是本地用户仓库,未来使用maven所自动下载的jar包会下载到这。

注:用户自定义本地仓库

maven 的仓库默认是放在本地用户的临时文件夹下面的 .m2 文件夹下的 repository 下,我的是在 C:\Users\Admin\.m2\repository 目录下,

 现在我们来修改将它指定到我们自己的路径下,我现在要将仓库指定到 D:\Repositories\Maven 目录下,

找到maven的存放目录(或者安装目录)打开conf文件夹下的settings.xml文件,找到第53行,把注释去掉,修改成:

<localRepository> D:\Repositories\Maven</localRepository>

在 cmd 中敲并回车执行:mvn help:system 这时候 maven 就会从远程仓库开始下载一大堆的东西,迟早都要下载的,

注:使用用户级别的maven配置

Maven有一个全局配置文件为 Maven根目录/conf/settings.xml 文件(比如我的就是 C:\tools\apache-maven-3.2.5\conf\settings.xml),Maven默认是使用此配置文件,所有用户共享此配置。但是推荐每一个用户配置自己的配置文件,防止无意思影响系统中其他用户,只需要将全局的配置文件复制到用户目录下的 .m2 文件夹即可(我的当前用户是 Admin, 所以复制后为 C:\Users\Admin\.m2\settings.xml )。(如果没有 .m2 文件夹 请先执行上一步,maven会在当前用户的目录下生成 .m2 文件夹)。

注:eclipse 中安装 maven 插件

  插件名字:m2eclipse,在eclipse菜单栏中选择Help  -->  Install New Software,你会看到一个Install的对话框,单击Add按钮,在name字段输入m2e,在Location字段中输入http://m2eclipse.sonatype.org/sites/m2e,单击OK,eclipse 会下载meeclipse安装站点上的资源信息,等待资源载入完成后按照提示安装即可

  eclipse一般集成了此插件和一个内嵌的Maven

 

5、Eclipse配置Maven:

点击eclipse中的window->Perference->Maven->Installations,设置自己下载的Maven。

原eclipse自带的maven可移除,因为大多是版本不一样,会导致后面有莫名的问题。

 

6、新建Maven的Web项目方法:

6.1)Ctrl + N:

这样,一个Maven的web项目已经建成。但默认,Project Facet中的Java版本是1.5的,要把它修改为本地的java版本。

6.2)右键项目->Properties,把它修改为:

我这里是使用Tomcat,所以要把Dynamic Web Module中的Runtimes设为tomcat:

6.2)以Maven的默认契约新建一个src/main/java源文件夹:

这个文件夹需要手工建,不能以新建源文件夹方式来建。(如果新建提示已经存在,但事实上又没有,只需在buildpath中选用自己的jdk版本就行)

6.3)打开pom.xml文件,加入依赖(dependency)

复制代码
  1. ...
  2.     <properties>  
  3.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  4.         <springversion>3.1.1.RELEASE</springversion>  
  5.         <junitversion>3.8.1</junitversion>  
  6.     </properties>  
  7.   
  8.     <dependencies>  
  9.         <dependency>  
  10.             <groupId>junit</groupId>  
  11.             <artifactId>junit</artifactId>  
  12.             <version>${junitversion}</version>  
  13.             <scope>test</scope>  
  14.         </dependency>  
  15.         <dependency>  
  16.             <groupId>org.springframework</groupId>  
  17.             <artifactId>spring-aop</artifactId>  
  18.             <version>${springversion}</version>  
  19.             <type>jar</type>  
  20.             <scope>compile</scope>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-asm</artifactId>  
  25.             <version>${springversion}</version>  
  26.             <type>jar</type>  
  27.             <scope>compile</scope>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.springframework</groupId>  
  31.             <artifactId>spring-aspects</artifactId>  
  32.             <version>${springversion}</version>  
  33.             <type>jar</type>  
  34.             <scope>compile</scope>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-beans</artifactId>  
  39.             <version>${springversion}</version>  
  40.             <type>jar</type>  
  41.             <scope>compile</scope>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>org.springframework</groupId>  
  45.             <artifactId>spring-context</artifactId>  
  46.             <version>${springversion}</version>  
  47.             <type>jar</type>  
  48.             <scope>compile</scope>  
  49.         </dependency>  
  50.         <dependency>  
  51.             <groupId>org.springframework</groupId>  
  52.             <artifactId>spring-context-support</artifactId>  
  53.             <version>${springversion}</version>  
  54.             <type>jar</type>  
  55.             <scope>compile</scope>  
  56.         </dependency>  
  57.         <dependency>  
  58.             <groupId>org.springframework</groupId>  
  59.             <artifactId>spring-core</artifactId>  
  60.             <version>${springversion}</version>  
  61.             <type>jar</type>  
  62.             <scope>compile</scope>  
  63.         </dependency>  
  64.         <dependency>  
  65.             <groupId>org.springframework</groupId>  
  66.             <artifactId>spring-expression</artifactId>  
  67.             <version>${springversion}</version>  
  68.             <type>jar</type>  
  69.             <scope>compile</scope>  
  70.         </dependency>  
  71.         <dependency>  
  72.             <groupId>org.springframework</groupId>  
  73.             <artifactId>spring-jdbc</artifactId>  
  74.             <version>${springversion}</version>  
  75.             <type>jar</type>  
  76.             <scope>compile</scope>  
  77.         </dependency>  
  78.         <dependency>  
  79.             <groupId>org.springframework</groupId>  
  80.             <artifactId>spring-jms</artifactId>  
  81.             <version>${springversion}</version>  
  82.             <type>jar</type>  
  83.             <scope>compile</scope>  
  84.         </dependency>  
  85.         <dependency>  
  86.             <groupId>org.springframework</groupId>  
  87.             <artifactId>spring-orm</artifactId>  
  88.             <version>${springversion}</version>  
  89.             <type>jar</type>  
  90.             <scope>compile</scope>  
  91.         </dependency>  
  92.         <dependency>  
  93.             <groupId>org.springframework</groupId>  
  94.             <artifactId>spring-oxm</artifactId>  
  95.             <version>${springversion}</version>  
  96.             <type>jar</type>  
  97.             <scope>compile</scope>  
  98.         </dependency>  
  99.         <dependency>  
  100.             <groupId>org.springframework</groupId>  
  101.             <artifactId>spring-tx</artifactId>  
  102.             <version>${springversion}</version>  
  103.             <type>jar</type>  
  104.             <scope>compile</scope>  
  105.         </dependency>  
  106.         <dependency>  
  107.             <groupId>org.springframework</groupId>  
  108.             <artifactId>spring-web</artifactId>  
  109.             <version>${springversion}</version>  
  110.             <type>jar</type>  
  111.             <scope>compile</scope>  
  112.         </dependency>  
  113.         <dependency>  
  114.             <groupId>org.springframework</groupId>  
  115.             <artifactId>spring-webmvc</artifactId>  
  116.             <version>${springversion}</version>  
  117.             <type>jar</type>  
  118.             <scope>compile</scope>  
  119.         </dependency>  
  120.         <dependency>  
  121.             <groupId>org.springframework</groupId>  
  122.             <artifactId>spring-test</artifactId>  
  123.             <version>${springversion}</version>  
  124.             <type>jar</type>  
  125.             <scope>compile</scope>  
  126.         </dependency>  
  127.   
  128.         <dependency>  
  129.             <groupId>javax.servlet</groupId>  
  130.             <artifactId>jstl</artifactId>  
  131.             <version>1.2</version>  
  132.             <type>jar</type>  
  133.             <scope>compile</scope>  
  134.         </dependency>  
  135.   
  136.         <dependency>  
  137.             <groupId>commons-collections</groupId>  
  138.             <artifactId>commons-collections</artifactId>  
  139.             <version>3.1</version>  
  140.         </dependency>  
  141.   
  142.         <dependency>  
  143.             <groupId>commons-logging</groupId>  
  144.             <artifactId>commons-logging</artifactId>  
  145.             <version>1.1</version>  
  146.         </dependency>  
  147.     </dependencies>  
  148.   
  149.    ...
  150. </project>  

保存pom.xml,eclipse会自动去到中央服务器中下载对应的jar包,jar包保存在用户文件夹的./m2下

 

这里,Maven的eclipse配置基本已经完成了。

 

7、构建和生成Maven

Eclipse中右键项目->Run As->Maven install

Maven默认会把生成的war文件、class所编译的文件都放在项目文件夹中的target目录下

 

二、在eclipse安装maven插件时,可能会遇到的错误:原文链接1 ,原文链接2

1.安装m2eclipse的时候,遇见这种错误如何解决?错误请见详细信息。

问题:Cannot complete the install because one or more required items could not be found.

Software being installed: Maven Integration for Eclipse (Required) 0.12.1.20110112-1712 (or
g.maven.ide.eclipse.feature.feature.group 0.12.1.20110112-1712)

Missing requirement: Maven Project Model Edit Bundle 0.12.1.20110112-1712 (org.maven.id
e.eclipse.maven_model_edit 0.12.1.20110112-1712) requires 'bundle org.eclipse.emf.ecore 0.
0.0' but it could not be found

Cannot satisfy dependency:

From: Maven Integration for Eclipse (Required) 0.12.1.20110112-1712 (org.maven.ide.eclip
se.feature.feature.group 0.12.1.20110112-1712)

To: org.maven.ide.eclipse.maven_model_edit [0.12.1.20110112-1712] 他说有个文件找不到
。但是到Maven的m2e的链接中,相关文件又是存在的。试了很多次了。都有这个问题。

解决:先连接Helios Update site (Helios - http://download.eclipse.org/releases/helios),安装
插件Graphical Editing Framework Zest Visualization Toolkit SDK。然后再安装 M2Eclipse plug-ins 。


2:如果装的插件版本太高会报

Cannot complete the install because one or more required items could not be
 found.
  Software being installed: m2e - Maven Integration for Eclipse (includes I
 
ncubating components) 1.5.0.20140606-0033 (org.eclipse.m2e.feature.feature.
 
group 1.5.0.20140606-0033)
  Missing requirement: Maven Integration for Eclipse 1.5.0.20140606-0033 (o
 
rg.eclipse.m2e.core 1.5.0.20140606-0033) requires 'bundle com.google.guava
 
 [14.0.1,16.0.0)' but it could not be found
 
  Cannot satisfy dependency:
 
    From: Maven Integration for Eclipse 1.5.0.20140606-0033 (org.eclipse.m2
 
e.core.ui 1.5.0.20140606-0033)
 
    To: bundle org.eclipse.m2e.core [1.5.0,1.6.0)
 
  Cannot satisfy dependency:
 
    From: m2e - Maven Integration for Eclipse (includes Incubating componen
 
ts) 1.5.0.20140606-0033 (org.eclipse.m2e.feature.feature.group 1.5.0.201406
 
06-0033)
 
答案:这种情况换一个低版本的安装,如
 
http://download.eclipse.org/technology/m2e/milestones/1.4
 

 

 

posted @ 2016-05-13 15:37  晨港飞燕  阅读(182)  评论(0编辑  收藏  举报