eclipse 中常用的maven配置

2018-01-15  本文已影响0人  Wenbook

使用eclipse(mars)内置maven插件

在eclipse中配置maven经常会遇到一些配置问题,当然可以使用eclipse的mars版本,这个版本中集成了maven的插件,默认配置的镜像仓库是国外的,所以如果图方便又想使用阿里等国内的镜像可以自己配置一份settings.xml文件然后在eclipse的Window->Preferences->Maven->User Settings中配置添加你自定义的settings.xml文件。
settings.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- 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>E:/software/mvn_repo</localRepository>

  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>


  <proxies>

  </proxies>


  <servers>

  </servers>


  <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>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 
  </mirrors>


  <profiles>

    <profile>
        <id>jdk17</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.7</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
        </properties>
    </profile>
  </profiles>

</settings>

但是使用eclipse内置的插件通常会出现一些问题,所以我们一般自己下载配置maven.

maven的安装和配置

  1. 访问官网 http://maven.apache.org/download.cgi 下载maven的安装包,然后解压到一本目录下面去,建议不要所有文件都放到C盘!

  2. 设置maven的环境变量:计算机->属性->高级系统设置->环境变量->在系统变量中新建环境变量(MAVEN_HOME)

    1.PNG
  3. 在Path中加入bin的路径


    2.PNG
  4. 在cmd中输入mvn -versionmvn -v测试是否配置成功。如果出现版本信息就表示已经配置成功了。当然配置maven之前必须要配置好jdk.

  5. 配置settings.xml文件说明:

<localRepository>E:/software/mvn_repo</localRepository> //可以在本地任意目录下配置,放在(settings)标签内
  1. 修改默认jdk: maven项目生成时默认的jdk会是1.5这个可以在settinds.xml文件中进行配置(位置在<profiles></profiles>标签内部)。
<profile>
        <id>jdk17</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.7</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
        </properties>
</profile>

  1. 为了让eclipse找到我们新配置的windows的maven,还需要在eclipse中进行配置:


    4.png
  2. 配置maven访问阿里云的镜像仓库

    <!--阿里云镜像-->
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 
  </mirrors>

配置settings文件:
用户默认访问的是用户级别的User Settings设置文件,最好配置全局变量,路径就是maven工具下的conf/settings.xml文件。有时候User Settings配置的settings文件会找不到,这个时候一般将maven工具下的conf/settings.xml文件复制一份到C盘的 \.m2\目录下。


5.png
上一篇 下一篇

猜你喜欢

热点阅读