Welcome to my website, have a nice day!
Dream it, Do it, Make it!

Maven如何配置自己的私有远程仓库

使用maven是为了更好的管理项目中涉及的jar包等,为了节省和更好的管理jar包,公司内部有时会搭建一个Maven的私库,那么如何对自己本地的maven进行配置,从而可以直接从私库下载Jar包呢?

我的环境如下:

  • 操作系统:windows 10;
  • maven版本:apache-maven-3.5.3 ;
  • idea版本:IntelliJ Idea 2017.3.2(Ultimate edition)

maven有自己缺省的远程仓库,具体可以在maven目录下的lib/maven-model-builder-${version}.jar(maven2.x版本在lib/maven-${version}-uber.jar中)中的pom-4.0.0.xml,可以看到缺省的中央仓库central配置为:

    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>

如果我们想指定自己公司内部的私有仓库地址,有三种方式:

  • 直接修改$MAVEN_HOME/conf/下的setting.xml文件;
  • 修改pom.xml配置;
  • 自定义一个setting.xml;

我们可能多个项目都用一个私库,修改pom.xml文件需要修改每个工程的配置,比较麻烦,推荐在自己的用户目录下建一个setting.xml文件(直接复制$MAVEN_HOME/conf/setting.xml到你的个人目录)。下面是具体的步骤:

新建一个setting.xml文件

这里我将直接复制$MAVEN_HOME/conf/setting.xmlC:\Users\michael\.m2目录下。

修改用户setting配置文件

1.修改本地仓库位置

找到复制之后的setting.xml如下位置进行修改:

  <!-- 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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
-->
  <localRepository>C:\Users\michael\.m2\repository</localRepository>

其实我这里可以不做修改,因为默认的就是用户目录下的.m2\repository,但是如果你的本地仓库不是在这里,就必须修改。以后maven下载的jar包将存储在这里。

2.增加一个profile

setting.xml文件的</profiles>之前增加一个profile,具体内容如下:

<profile>
    <id>dev</id>
    <repositories>
        <repository>
          <id>nexus-jar</id>
          <name>Team Nexus jar Repository</name>
          <url>http://10.110.13.14:8081/nexus/content/groups/inspur-loushang/</url>
        </repository>
        <!-- 配置检察院私服使用信息 -->
        <repository>  
          <id>maven-public</id>  
          <name>My maven public</name>  
          <url>http://10.110.1.49:8888/repository/maven-public/</url>  
          <releases>  
          <enabled>true</enabled>
          </releases>  
          <snapshots>  
          <enabled>false</enabled>  
          </snapshots>  
        </repository>  
      </repositories>
    <pluginRepositories>
        <!-- 配置技术中心私服使用信息 -->
        <pluginRepository>
          <id>nexus-plugin</id>
          <name>Team Nexus plugin Repository</name>
          <url>http://10.110.13.14:8081/nexus/content/groups/inspur-loushang/</url>
        </pluginRepository>
   </pluginRepositories>
</profile>

上面我们增加了一个id为dev的profile。

3.激活增加的profile

修改setting.xml配置的下面位置:

 <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->

修改为:

<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

这样就启用了id为dev的profile配置。

修改idea配置

Idea的配置路径File->Setting->Build,Execution,Deployment->Build Tools->Maven,在Maven home directory下拉选项中idea已经自带maven2,maven3,这里我们选择本机自己装的apache-maven-3.5.3所在路径,并把User setting file修改为自己的自定义配置路径,如下图:

idea-maven-user-setting.jpg

重新下载Jar包

在工程上reimport一下,idea会自动下载jar包。

赞(0)
未经允许禁止转载:Ddmit » Maven如何配置自己的私有远程仓库

评论 抢沙发

登录

找回密码

注册