maven学习(2)仓库和配置

1:本地资源库、中央存储库、远程存储库 

  

1.1   本地资源库

  当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖需要下载。首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如果没有找到,然后把它会从默认的 Maven 中央存储库 – http://repo1.maven.org/maven2 查找,然后下载到本地仓库。

  Maven的本地资源库是用来存储所有项目的依赖关系(插件jar和其他文件),这些文件被Maven下载到本地文件夹。当你建立一个Maven项目,所有相关文件将被存储在你的Maven本地仓库。默认情况下,Maven的本地资源库默认为 .m2 目录文件夹:Windows – C:Documents and Settings用户名.m2

  修改从Maven中心仓库下载到本地的jar包的默认存储位置:打开settings.xm(包含仓库地址、镜像、插件、代理等配置)l文件,在conf文件夹下面。

加入<localRepository>D:mavenmvnRespo</localRepository>这句代码;其中中间填写自己想要保存的本地库位置路径。例如:<localRepository>E:/repository</localRepository>

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

1.2 中央存储库

  (1)查看中央资源库位置,从解压的目录下面进入lib文件夹,里面有好多jar包,解压maven-model-builder-3.2.5.jar.在maven-model-builder-3.2.5orgapachemavenmodel下面的pom-4.0.0.xml文件里面打开,可找到下面代码:其中https://repo.maven.apache.org/maven2为中央仓库地址。

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

在使用中有可能会更换中央资源库位置:有两种方式:

  • 在pom.xml放入下面代码,将ID,name,URL改成自己需要的。<pluginRepositories>,这是配置Maven从什么地方下载插件构件。
<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>
  • 在setting.xml中进行配置,如果好多项目都需要在同一新仓库下载,每个项目的pom.xml下进行改变,重复工作太多,因此在根目录下conf文件夹下面的setting.xml进行改变。

 打开setting.xml文件,找到 <profiles>属性,然后将里面代码进行替换。提供了一种用户全局范围的仓库配置。

<profiles> 
<profile>
     
 <id>自己填写</id>  
       <!-- repositories and pluginRepositories here-->
    </profile>
</profiles>

     

1.3 远程存储库

  默认情况下,Maven从Maven中央仓库下载所有依赖关系。但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到。

  •  添加Java.net远程仓库的详细信息在“pom.xml”文件。
<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 </repositories>
</project>
  • 添加JBoss远程仓库的详细信息在 “pom.xml” 文件中。
<project ...>
    <repositories>
      <repository>
    <id>JBoss repository</id>
    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository>
    </repositories>
</project>

2:启用代理服务器(不是非必须)

  如果你的公司正在建立一个防火墙,并使用HTTP代理服务器来阻止用户直接连接到互联网。如果您使用代理,Maven将无法下载任何依赖。为了使它工作,你必须声明在 Maven 的配置文件中设置代理服务器:settings.xml。

点击打开settings.xml文件:在conf文件夹下面

打开setting.xml文件:找到下面这段代码,然后去掉注释,将里面的服务器信息换成自己的代理服务器信息保存后即可。

 <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

 3:maven依赖库管理

  原始情况下我们是这么处理的:以log4j为例

  1. 访问 http://logging.apache.org/log4j/
  2. 下载 Log4 j的 jar 库
  3. 复制 jar 到项目类路径
  4. 手动将其放到项目的依赖

所有的管理需要一切由自己做,如果有 Log4j 版本升级,则需要重复上述步骤一次。

  在maven中的方式:你需要知道 log4j 的 Maven 坐标(在pom.xml中体现),例如:

<dependencies>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
    </dependency>
</dependencies>

它会自动下载 log4j 的1.2.14 版本库。如果“version”标签被忽略,当有新的版本时它会自动升级库时

  1. 当 Maven 编译或构建,log4j 的 jar 会自动下载,并把它放到 Maven 本地存储库
  2. 所有由 Maven 管理

传统构建和maven构建对比下:可以发现当建立一个Maven的项目,pom.xml文件将被解析。eg:如果看到 log4j 的 Maven 坐标,然后 Maven 按此顺序搜索 log4j 库:

  1. 在 Maven 的本地仓库搜索 log4j ,搜索不到执行2;
  2. 在 Maven 中央存储库搜索 log4j,搜索不到执行3;
  3. 在 Maven 远程仓库搜索 log4j(如果在 pom.xml 中定义),搜索不到报错。

https://mvnrepository.com/

原文地址:https://www.cnblogs.com/brilliantl/p/7122540.html