---
title: "eclipse中安装配置maven"
author: "rjw"
date: "2012-07-23T12:19:15Z"
tags: ["Eclipse", "maven", "安装", "配置"]
categories: ["开源软件"]
slug: "eclipse-maven"
draft: false
lastmod: "2012-07-23T12:19:15Z"
id: 1604138
url: "https://rjw.iteye.com/blog/1604138"
---

1、首先说一下在windows中安装maven。非常简单。。。

到[http://maven.apache.org/download.html](http://maven.apache.org/download.html)中下载maven，截止笔者发文时，maven最新版本为maven 3.0.4。我下载的即为此版本。具体地址为[http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.zip](http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.zip)（以后可能会有变化，自己去官网慢慢找就是了，不难）。

下载以后解压，我的解压目录为E:\\apache-maven-3.0.4，即为M2\_HOME

    接下来就是我们熟悉的配置环境变量：新建环境变量M2\_HOME,并在Path中加入%M2\_HOME%\\bin。

测试一下是否安装成功了，运行命令 mvn -v。如果出现下图所示，则表明安装成功。

     
![](http://dl.iteye.com/upload/attachment/0071/1909/e14673c1-c56f-376d-b9e2-b59ec9eb9d7c.jpg)

     2、当然也可以直接在eclipse中安装m2eclipse插件，具体方法与安装其他插件一样一样的哈。location中输入[http://m2eclipse.sonatype.org/sites/m2e](http://m2eclipse.sonatype.org/sites/m2e)。

     3、安装完成后，测试一下吧。新建一个maven项目（File&gt;New&gt;Other&gt;Maven&gt;Maven Project）。在弹出的New Maven Project对话框中，使用默认选项（不要选择Create a simple project选项，这样就可以使用Maven Archetype了）。Next，此时m2eclipse会提示我们选择一个archetype。这里我们选择maven-archetype-quickstart，Next，依次输入groupId,artifactId,version,package。如下图所示。

![](http://dl.iteye.com/upload/attachment/0071/1920/a3145686-c65e-35fe-b7ca-f6cbe1a588d8.jpg)

 

     这样就可以运行了。在项目上右击，run as&gt;maven build...&gt;clean install。

居然报错了，错误如下：

 

 

```
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-jsf-with-maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-jsf-with-maven ---
[INFO] Deleting E:\Projects\hello-jsf-with-maven\target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hello-jsf-with-maven ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Projects\hello-jsf-with-maven\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-jsf-with-maven ---
[INFO] Compiling 1 source file to E:\Projects\hello-jsf-with-maven\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Unable to locate the Javac Compiler in:
  C:\Java\jre7\..\lib\tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.766s
[INFO] Finished at: Mon Jul 23 11:08:55 CST 2012
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project hello-jsf-with-maven: Compilation failure
[ERROR] Unable to locate the Javac Compiler in:
[ERROR] C:\Java\jre7\..\lib\tools.jar
[ERROR] Please ensure you are using JDK 1.4 or above and
[ERROR] not a JRE (the com.sun.tools.javac.Main class is required).
[ERROR] In most cases you can change the location of your Java
[ERROR] installation by setting the JAVA_HOME environment variable.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
```

 

仔细看了一下上面的日志文件，初步推断是jre的配置问题。window&gt;Preference&gt;Java&gt;Installed JREs,果然是配置问题。安装jdk时，默认会有两个jre，eclipse中运行的jre一定是jdk中的jre不能是那个独立的jre。例如我的jdk的安装目录（即JAVA\_HOME）为：C:\\Java\\jdk1.7.0\_05。与这个目录同级的还有一个jre7目录。eclipse中配置的应为JAVA\_HOME中的目录，因为环境变量中就是这么定位的。

好了，改过来就ok了。