【工作记录】maven本地仓库和远程仓库的理解

        今日小编在工作中遇到一个问题,我本地eclipse中导入一个关于micro server的新项目,有一些依赖和插件在本地仓库中并没有,需要从远程仓库中获取,奈何上午maven私服挂掉,我只能想法子去找jar包来使用了。

       之前与到这样的情况,我的解决办法会是从maven中央仓库(http://mvnrepository.com/),或者阿里云的中央仓库(http://maven.aliyun.com/nexus/#welcome),通过手动的方式,把我所需要的对应版本的jar包,或者pom包下载下来,之后扔到对应本地仓库路径中,执行"mvn clean install”,在pom中报错的地方就不会报错了,可是这样子手动操作对于“多个独立项目,且引用的本地仓库不存在的jar包都不同”的情况,就很棘手,比如:

                          

        这个时候,每个项目都会有很多不同的jar包需要我挨个手动去download,费时费力,再需要想想别的办法。

        我们使用maven管理jar包的时候,原理是如果本地仓库没有,就去远程仓库去拿(Addition:远程仓库分为:私服 && 中央仓库),私服中如果有对应版本的jar包,直接备份到本地仓库,并使用;如果私服中没有,则向中央仓库中去寻找并备份到本地仓库(Question:有没有会备份到私服,有了解的同学可以在评论区帮个忙),这样子,就实现了maven对jar包的管理。

        之后,参考我们maven中的setting文件(这个配置文件属于公司定制后的):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>D:\Maven\repository</localRepository>

	<!-- 设置发布 jar 包时的用户名及密 -->
	<servers>
		<server>
			<id>releases</id>
			<username>***</username>
			<password>***</password>
		</server>

		<server>
			<id>****napho</id>
			<username>a&*</username>
			<password>#¥%……%¥</password>
		</server>		
	</servers>

	<!-- 设置 maven 的远程仓库为 nexus -->
	<mirrors>
		<mirror>
			<id>nexus</id>
			<mirrorOf>central</mirrorOf>
			<name>nexus repository</name>
			<url>http://192.168.***.***:8081/nexus/*****/****/****</url>
		</mirror>
	</mirrors>

	<!-- 设置 central 的路径等,覆盖了maven module builder里面的central配置 -->
	<profiles>
		<profile>
			<id>central</id>
			<repositories>
				<repository>
					<id>central</id>
					<name>Central</name>
					<url>http://192.168.***.***:8081/nexus/*****/****/****</url>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>central</id>
					<name>Central</name>
					<url>http://192.168.**.***:8081/nexus</url>
				</pluginRepository>
			</pluginRepositories>
		</profile> 
	</profiles>


	<activeProfiles> 
		<activeProfile>central</activeProfile>
	</activeProfiles>

	<!-- 配置eclipse插件 -->
	<pluginGroups>
		<pluginGroup>org.mortbay.jetty</pluginGroup>	
		<pluginGroup>org.codehaus.cargo</pluginGroup>
		<pluginGroup>org.jvnet.hudson.tools</pluginGroup>
	</pluginGroups>

</settings>
         如上所示,是我整个maven中setting文件的配置,其中<localRepository>中配置了本地仓库的地址,同时在<mirror>标签中配置了远程仓库的镜像:

<mirror>
	<id>nexus</id>
	<mirrorOf>central</mirrorOf>
	<name>nexus repository</name>
	<url>http://192.168.22.***:8081/nexus/content/groups/d***-public</url>
</mirror>
          这样子,在本地仓库中找不到对应的jar包之后,就会到私服当中去寻找,可是上午让我比较纳闷的是,我们的中央仓库到底在哪里?参考了好多资料,在这位博主的文章中,我看到了我要的信息:( http://www.cnblogs.com/france/p/4808582.html),目录中依次打开“maven -> apache-maven-3.3.9(版本) ->  lib -> maven-model-builder-3.3.9.jar -> org -> apache -> maven -> model -> pom.xml”,对应的内容如下:

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

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <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>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->
       这里有真正的远程仓库的连接配置,如下:

<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>
       用浏览器连接:“https://repo.maven.apache.org/maven2”,发现登录成功了apach的中央仓库,so happiness.

       但是往往我们所要访问的中央仓库可能是阿里云的中央仓库(由于政府限制外网速率),就需要重新覆盖远程仓库的地址,当然我觉得可以直接改一下这个pom文件中repository的地址,一种办法就是在setting.xml中去覆盖远程仓库的地址。

        上文中有提到,远程仓库包含中央仓库和私服,在我上面的setting.xml中,有一段这样的标签:

<!-- 设置 maven 的远程仓库为 nexus -->
<mirrors>
	<mirror>
		<id>nexus</id>
		<mirrorOf>central</mirrorOf>
		<name>nexus repository</name>
		<url>http://192.168.22.***:8081/nexus/content/groups/***-public</url>
	</mirror>
</mirrors>
        如上,其实就用局域网的私服url地址中的某个respoity覆盖掉了中央仓库,这也就解释了为什么我们公司目前有些jar包不能直接从中央仓库去拿,如果自己玩的话,我觉得<mirrors>下面可以添加多个<mirror>,让私服和远程中央仓库处于同级关系,私服优先级高一点,私服中没有,就去其他的<mirro>中寻找,即远程中央仓库中寻找。(这一点 我还需要拿到itoo中去做验证,自己的猜测, 可行之后,重新更新文章。)

        这样,我们就可以在私服挂掉之后,直接从中央仓库去拿jar文件。

        分析我本地maven3.3.9对应默认的远程仓库是OSChina Central,对应到setting.xml中的写法如下:

<mirror>  
      <id>CN</id>  
      <name>OSChina Central</name>                                                                                                                         
      <url>http://maven.oschina.net/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>  
</mirror>  
       其实,只要你可以连接外网, 还有一些可以使用的中央仓库镜像地址,总结如下:

       repo2.maven.org

<mirror>    
      <id>repo2</id>    
      <mirrorOf>central</mirrorOf>    
      <name>Human Readable Name for this Mirror.</name>    
      <url>http://repo2.maven.org/maven2/</url>    
</mirror>  
         uk.maven.org 

<mirror>    
      <id>ui</id>    
      <mirrorOf>central</mirrorOf>    
      <name>Human Readable Name for this Mirror.</name>    
     <url>http://uk.maven.org/maven2/</url>    
</mirror>  
        mirrors.ibiblio.org 

<mirror>    
      <id>ibiblio</id>    
      <mirrorOf>central</mirrorOf>    
      <name>Human Readable Name for this Mirror.</name>    
     <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>    
</mirror>
       repository.jboss.org
<mirror>    
      <id>jboss-public-repository-group</id>    
      <mirrorOf>central</mirrorOf>    
      <name>JBoss Public Repository Group</name>    
     <url>http://repository.jboss.org/nexus/content/groups/public</url>    
</mirror>

        That's all.

        希望对此处有疑惑的同学,有所帮助。


        





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值