您的位置:首页 > 其它

Maven实战——仓库的分类详解

2015-05-29 14:17 387 查看
仓库的分类

本地仓库、远程仓库(中央仓库、***、其他公共库)

1、本地仓库

无论是在window还是在Linux系统上,每一个用户在自己用户目录下都有一个.m2/repository/仓库的目录。例如:

我的windows上是c:\users\jader.m2\repository,linux上是:home/jader/.m2/repository,在Linux系统中,以.开头的文件或目录是隐藏的,可以使用ls -a来显示隐藏文件或目录

有时候C盘空间可以自定义本地仓库目录,在Maven的安装目录的conf下找到settings.xml文件,配置如下:

<localRepository>D:\Respositories\Maven</localRepository>


一个构件只有在本地仓库之后,才能由其他Maven项目使用,那么构件如何进入到本地仓库呢?最常见的是从远程仓库下载到本地仓库,还有一种的常见的情况是,将本地项目的构件安装到Maven仓库中。例如:

本地有两个项目A、B,两者都无法从远程仓库获得,而同时A又依赖于B,为了能构建A、B,那就必须首先得以构建并安装到本地仓库。

在某个项目执行mvn clean install命令

install插件的install将项目构建输出文件安装到本地仓库。

2、远程仓库

安装好Maven之后如果不执行Maven命令,本地仓库目录是不存在的,当用户输入第一条Maven命令之后Maven才会创建本地目录。本地仓库就好比书房,我读书的时候先从本地书房找,如果没有我再去网上书店购买,购买完毕之后再放到自己的书房。每一个用户只有一个本地仓库,但是可以配置多个远程仓库。

我们需要在什么时候配置远程仓库呢?当你连接中央仓库的速度比较慢时,或者你为你的公司搭建了自己的仓库,比如Nexus仓库管理(后面我会介绍),又或者你苏需要的jar存在另外一个公共仓库,比如我们配置一个国内的镜像地址:

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


这里我们可以看到,允许配置多个repository和 plugin repository,<
releases><enabled>true</enabled></releases>
告诉Maven可以从这个仓库下载releases版本的构件,而
<snapshots><enabled>false</enabled></snapshots>
告诉Maven不要从这个仓库下载snapshot版本的构件,之所以不让你从这个仓库下载snapshot版本,是因为这些版本是不稳定的,但是snapshot版本在我们内部项目开发的时候可是作用巨大,后面的问文章我会讨论这个问题。至于
<pluginRepositories>,
这是配置Maven从什么地方下载插件构件,Maven的所有行为都是通过插件来完成的,其内部配置与
<repository>
类似,这里就不多说了。

尽管pom.xml中可以方便的哦配置中央仓库,但是我并不推荐大家这么做,尤其是在大的公司中,因为一个公司会有很多的项目,如果每个项目都这样配置,那么又开始做重复的copy工作了,如何解决呢,我们往下走

在 settings.xml 中配置远程仓库

Pom.xml的作用范围是一个项目,一个公司不可能只做一个项目,那么为了避免重复配置,那么我们需要把一些公共信息配置在setting.xml中。但是setting.xml中并不支持
<repositories>
<pluginRepositories>,
为了解决这个问题我们使用profile:

<settings>    
   …    
   <profiles>    
     <profile>    
       <id> myProfiel</id>    
       <!—在这里加入<repositories>及<pluginRepositories>–>   
     </profile>    
   </profiles>    
   <activeProfiles>    
     <activeProfile> myProfiel </activeProfile>    
   </activeProfiles>    
   …    
</settings>


这里通过元素来激活这个profile,这样我们就可以全局的使用这个配置,不再需要为每个POM做重复的配置了。

Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。profile可以在pom.xml中配置,并给定一个id。然后你就可以在运行Maven的时候使用的命令行标记告诉Maven运行特定profile中的目标。以下pom.xml使用production profile覆盖了默认的Compiler插件设置。

在实际的操作过程中,这里我们最好不要配置远程仓库,最好能够通过nexus建立公司或者组织自己的仓库,然后这把把地址指向自己的仓库,后面我会介绍为什么要这么做,怎么做。

<profiles> 
    <profile> 
      <id>production</id> 
      <build> 
        <plugins> 
          <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <configuration> 
              <debug>false</debug> 
              <optimize>true</optimize> 
            </configuration> 
          </plugin> 
        </plugins> 
      </build> 
    </profile> 
</profiles>


要使用production profile来运行mvn install,你需要在命令行传入-Pproduction参数。要验证production profile覆盖了默认的Compiler插件配置,可以像这样以开启调试输出(-X) 的方式运行Maven。

如果你开始大量使用Maven profile,你会希望将profile从POM中分离,使用一个单独的文件如profiles.xml。你可以混合使用定义在pom.xml中和外部profiles.xml文件中的profile。只需要将profiles元素放到${basedir}目录下的profiles.xml文件中,然后照常运行Maven就可以。profiles.xml文件的大概内容如下

<profiles> 
     <profile> 
       <id>development</id> 
       <build> 
         <plugins> 
           <plugin> 
             <groupId>org.apache.maven.plugins</groupId> 
             <artifactId>maven-compiler-plugin</artifactId> 
             <configuration> 
               <debug>true</debug> 
               <optimize>false</optimize> 
             </configuration> 
           </plugin> 
         </plugins> 
       </build> 
     </profile> 
     <profile> 
       <id>production</id> 
       <build> 
         <plugins> 
           <plugin> 
             <groupId>org.apache.maven.plugins</groupId> 
             <artifactId>maven-compiler-plugin</artifactId> 
             <configuration> 
               <debug>false</debug> 
               <optimize>true</optimize> 
             </configuration> 
           </plugin> 
         </plugins> 
       </build> 
     </profile> 
</profiles>


settings profile可以应用到所有你使用Maven构建的项目。你可以在两个地方定义settings profile:定义在~/.m2/settings.xml中的用户特定settings profile,或者定义在${M2_HOME}/conf/settings.xml中的全局settings profile。

配置镜像

如果你想覆盖中央仓库的默认地址,那么这里我们就会使用的镜像了,还在setting.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>


3、中央仓库

由于最原始的本地仓库是空的,Maven必须知道至少一个可用的远程仓库,才能在执行Maven命令的时候下载到需要的构件。中央仓库就是一个默认的远程仓库。在${M2_HOME}/lib/maven-model-builder-3.0.jar中,访问org/apache/maven/model/pom-4.0.0.xml可以看到配置如下:

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


如果你想覆盖中央仓库的默认地址,那么这里我们就会使用的镜像了,还在setting.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>


<mirrorOf> central</mirrorOf>
表示只为central仓库做镜像,如果想为所有的仓库做镜像那么可以改为:

<mirrorOf>*</mirrorOf>


4、***

特殊的远程仓库,架设在局域网内的仓库服务,看看下面的图就能知道***的作用:




***的优点:

节省自己的外网宽带

加速Maven的构建

部署第三方构件

提高稳定性,增强控制

降低中央仓库的负荷
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: