`
chyou1988
  • 浏览: 6783 次
社区版块
存档分类
最新评论

maven仓库

 
阅读更多

仓库的分类

远程仓库,本地仓库

当maven根据坐标寻找构件的时候,它首先会查看本地仓库,如果本地仓库存在此构件,则直接使用;如果本地仓库不存在此构件,或者需要查看是否有更新的构件版本,maven就会去远程仓库查找,发现需要的构件之后,下载到本地仓库再使用。如果本地仓库和远程仓库都没有需要的构件,maven就会报错

私服

是另一种特殊的远程仓库,为了节省带宽和时间,应该在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库内部的项目还能部署到私服上供其他项目使用

远程仓库的默认配置

${M2_HOME}/lib/maven-2.0.10-uber.jar ,打开该文件,能找到超级POM:

\org\apache\maven\project\pom-4.0.0.xml ,它是所有Maven POM的父POM,所有Maven项目继承该配置,你可以在这个POM中发现如下配置:

 

<repositories>  
  <repository>  
    <id>central</id>  
    <name>Maven Repository Switchboard</name>  
    <layout>default</layout>  
    <url>http://repo1.maven.org/maven2</url>  
    <snapshots>  
      <enabled>false</enabled>  
    </snapshots>  
  </repository>  
</repositories> 

上为超级POM配置了ID为central的远程仓库,如果pom.xml中未配置仓库,默认的将使用这个central的超级仓库。

最后需要注意的是snapshots元素,其子元素enabled的值为false,表示不从该中央仓库下载快照版本的构件

本地仓库

 手动创建本地仓库的存放路径,例如:D:\Java\m2\repository; 
  修改Maven安装目录下的 conf/文件夹内的setting.xml文件

<settings>
	<localRepository>D:\java\repository\</localRepository>
</settings>

 将上一步修改好的setting.xml文件复制到D:\Java\m2\repository目录下一份;

在POM中配置远程仓库

<project>  
...  
  <repositories>  
    <repository>  
      <id>maven-net-cn</id>  
      <name>Maven China Mirror</name>  
      <url>http://maven.net.cn/content/groups/public/</url>  
      <releases>  
        <enabled>true</enabled>  
      </releases>  
      <snapshots>  
        <enabled>false</enabled>  
      </snapshots>  
    </repository>  
  </repositories>  
  <pluginRepositories>  
    <pluginRepository>  
      <id>maven-net-cn</id>  
      <name>Maven China Mirror</name>  
      <url>http://maven.net.cn/content/groups/public/</url>  
      <releases>  
        <enabled>true</enabled>  
      </releases>  
      <snapshots>  
        <enabled>false</enabled>  
      </snapshots>      
    </pluginRepository>  
  </pluginRepositories>  
...  
</project>

 pluginRepositories:maven从何处下载插件

setting.xml中配置远程仓库

setting.xml不直接支持 这两个元素。利用profile配置仓库

<settings>  
  ...  
  <profiles>  
    <profile>  
      <id>dev</id>  
      <!-- repositories and pluginRepositories here-->  
    </profile>  
  </profiles>  
  <activeProfiles>  
    <activeProfile>dev</activeProfile>  
  </activeProfiles>  
  ...  
</settings>

 使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。

镜像使用

拦截仓库的访问到镜像下的地址,常用与配置私服,拦截所有的请求到私服地址

<settings>  
...  
  <mirrors>  
    <mirror>  
      <id>maven-net-cn</id>  
      <name>Maven China Mirror</name>  
      <url>http://maven.net.cn/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>  
    </mirror>  
  </mirrors>  
...  
</settings>

 

<settings>  
...  
  <mirrors>  
    <mirror>  
      <id>my-org-repo</id>  
      <name>Repository in My Orgnization</name>  
      <url>http://192.168.1.100/maven2</url>  
      <mirrorOf>*</mirrorOf>  
    </mirror>  
  </mirrors>  
...  
</settings>

分发构件到远程仓库

mvn install 会将项目生成的构件安装到本地Maven仓库

mvn deploy 用来将项目生成的构件分发到远程Maven仓库。

本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件

<project>    
  ...    
  <distributionManagement>    
    <repository>    
      <id>nexus-releases</id>    
      <name>Nexus Release Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
    </repository>    
    <snapshotRepository>    
      <id>nexus-snapshots</id>    
      <name>Nexus Snapshot Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
    </snapshotRepository>    
  </distributionManagement>    
  ...    
</project>

 一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:

<settings>    
  ...    
  <servers>    
    <server>    
      <id>nexus-releases</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>    
    <server>    
      <id>nexus-snapshots</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>      
  </servers>    
  ...    
</settings>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics