Maven

  在开发Java项目的过程中,经常会引入大量的Jar文件,在项目目录下,往往会看到一个名为/lib的子目录,那里存放着各类第三方依赖jar文件,如 log4j.jar,junit.jar等等。每建立一个项目,开发人员都必须走这一步。多个项目不共用相同的jar文件,不仅会造成磁盘资源的浪费,也使得版本的一致性管理变得困难。此外,如果使用版本管理工具,如 SVN(没有使用版本管理工具?马上试试SVN吧,它能帮你解决很多头疼的问题),需要将大量的jar文件提交到代码库里,可是版本管理工具在处理二 进制文件方面并不出色。Maven仓库就是放置所有JAR文件(WAR,ZIP,POM等等)的地方,所有Maven项目可以从同一个Maven仓库中获取自己所需要的依赖 JAR,这节省了磁盘资源。此外,由于Maven仓库中所有的JAR都有其自己的坐标,该坐标告诉Maven它的组ID,构件ID,版本,打包方式等等, 因此Maven项目可以方便的进行依赖版本管理。也不在需要提交JAR文件到SCM仓库中,可以建立一个组织层次的Maven仓库(接下来的文章会一一介绍),供所有成员使用。Maven仓库能帮助我们管理构件(主要是JAR)。

Maven本地仓库和远程仓库

  运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。

  Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。也可以通过设置指定到自己的仓库中

  1)、修改${user.home}/.m2/settings.xml :

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



  2)、修改${maven_home}/../conf/setings.xml:

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

  Maven 远程仓库可以是Maven的中心仓库和自己设置的nexus服务(接下来的文章会一一介绍),Maven的远程仓库可以通过设置指定到理想的远程仓库中

  1)、默认远程仓库的配置文件 ${M2_HOME}/lib/maven-2.0.10-uber.jar,打开该文件,找到超级POM:\org\apache\maven\project\pom-4.0.0.xml ,它是所有Maven POM的父POM,所有Maven项目继承该配置,你可以在这个POM中发现如下配置:

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



url即为Maven指定的远程仓库。

  2)、通过配置超级POM.XML文件,修改Maven的远程仓库:

<project>
...
  <repositories>
    <repository>
    <id>maven-public</id>
    <name>aline</name>
    <url>http://localhost:8082/repository/maven-public/</url>
    <releases>
    <enabled> true</enabled>
    </releases>
    <snapshots>
    <enabled> false</enabled>
    </snapshots>
    </repository>
</repositories>

<pluginRepositories>
  <pluginRepository>
  <id>maven-public</id>
  <name>aline</name>
  <url>http://localhost:8082/repository/maven-public/</url>
  <releases>
  <enabled> true</enabled>
  </releases>
  <snapshots>
  <enabled> false</enabled>
  </snapshots>
  </pluginRepository>
</pluginRepositories>
...
</project>



  其中参数releases =true 表示可以从该仓库下载releases版本的构件,releases=false则相反

${maven_home}/../conf/setings.xml 文件可以修改远程仓库:

</settings>
  ...
  <activeProfiles>
    <activeProfile>myProfiel </activeProfile>
  </activeProfiles>
  <profiles>
    ...
    <profile>
      <id>myProfiel</id>
      <repositories>
        <repository>
        <id>maven-public</id>
        <name>aline</name>
        <url>http://localhost:8082/repository/maven-public/</url>
        <releases>
        <enabled> true</enabled>
        </releases>
        <snapshots>
        <enabled> false</enabled>
        </snapshots>
        </repository>
      </repositories>

      <pluginRepositories>
        <pluginRepository>
        <id>maven-public</id>
        <name>aline</name>
        <url>http://localhost:8082/repository/maven-public/</url>
        <releases>
        <enabled> true</enabled>
        </releases>
        <snapshots>
        <enabled> false</enabled>
        </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    ...
  </profiles>
  ...
</settings>



可以通过配置setting.xml,但是该配置文件并不支持<repositories>及<pluginRepositories>。通过<activeProfile>元素来激活这个profile,这样我们就可以全局的使用这个配置,不再需要为每个POM做重复的配置了。