对maven的深入理解

最近需要做maven的配置管理,这里做个记录,有延伸。

1.对本地仓库的配置:
本地仓库不宜放在C盘:原因:
1. 本地仓库大小约为1G,占用内存
2. 万一重装系统,C盘内容消失,build workSpace时间漫长

更改本地仓库方法:
1. 在maven安装包的conf/settings.xml中设置<localRepository>D:\tools\maven\m2\respository</localRepository>
2. 在eclipse中设置preferrences–maven–user settings–user settings中设置settings.xml路径(如果此时报错:如法读取文件,那么很有可能文件有错误:字母拼写不正确等),最后点击update
settings,就会出现eclipse右下角会出现build workSpace这样字样

2.鉴权配置:
主要用于执行maven命令deploy时的鉴权。找到servers标签并配置

<server>
    <id>maven_dev</id>
    <username>admin</username>
    <password>admin123</password>
</server>

具体配置多少个server根据需要而定。
注意:此处id必须和工程pom.xml文件中repository标签的id对应起来。如下

<repositories>
        <repository>
            <id>group_one</id>
            <name>group_maven_repos</name>
            <url>http://211.140.17.108:8031/nexus/content/groups/group_one</url>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

注:updatePolicy:更新策略
用户名密码:该用户名和密码是Nexus私服密码,默认用户名是admin密码admin123

3.setting.xml的建议:
settings.xml文件中也可以配置仓库信息。但是建议在工程的pom.xml文件中配置,这样可以将作用范围控制在工程级别,灵活。

4..maven本地仓库,私服Nexus和中央仓库:

  1. 本地仓库:c盘中.m2的包
  2. 远程仓库:不是本地仓库的仓库都是远程仓库(包含了私服Nexus和中央仓库)
  3. 私服Nexus:是远程仓库的一种,只不过远程仓库的地址是本地服务器而已
    访问私服:使用默认账户:admin 密码:admin123

  4. 访问顺序:本地仓库(本机)—>私服(局域网)—>中心仓库(外部网络)

中央仓库的配置:在maven安装路径下,在lib目录下,maven-model-builder-3.5.3.jar中,有一个默认的pom.xml文件:
配置了Maven默认连接的中心仓库:特征:central id名为 central

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

配置私服的两种方式:
方法一:在工程中配置xml:不许激活,可用maven -update强制更新就可

<project>

  ...

  <!-- 配置私服地址 -->
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </pluginRepository>
  </pluginRepositories>

  ...

<project>

方法二:在全局settings.xml中配置:需激活

<settings>  

  ...  

  <profiles>  
     <profile>  
      <id>profile-nexus</id>  

      <repositories>  
        <repository>  
          <id>nexus</id>  
          <url>http://localhost:8081/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
        <pluginRepository>  
          <id>nexus</id>  
          <url>http://localhost:8081/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </pluginRepository>  
      </pluginRepositories>  
    </profile>  
  </profiles>  

  ...  

</settings> 

之后在使用对上面的配置进行激活(通过配置的id标识进行激活)

<activeProfiles>  
  <activeProfile>profile-nexus</activeProfile>  
</activeProfiles> 

5.maven镜像:

  1. 镜像的定义:
    Maven的镜像是指如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像,也就是说任何一个可以从仓库Y获得的依赖,都能够从它的镜像中获取。比如阿里的Maven仓库http://maven.aliyun.com/nexus/content/groups/public就可以理解为是中央仓库https://repo.maven.apache.org/maven2/
  2. 镜像的作用:
    作用:使用镜像可以提高项目构建效率。

我们可以来看下 setting.xml中对于的解释
mirrors:This is a list of mirrors to be used in downloading artifacts from remote repositories. It works like this: a POM may declare a repository to use in resolving certain artifacts. However, this repository may have problems with heavy traffic at times, so people have mirrored it to several places.That repository definition will have a unique id, so we can create a mirror reference for that repository, to be used as an alternate download site. The mirror site will be the preferred server for that repository.

即为:粗体字显示的:由于网络问题,远程库可能有问题

在settings.xml中设置镜像:

<settings>  
  ...  
  <mirrors>  
    <mirror>  
      <id>maven.net.cn</id>  
      <name>one of the central mirrors in china</name>  
      <url>http://maven.net.cn/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>  
    </mirror>  
  </mirrors>  
  ...  
</settings>

翻译:的值为central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求,都会转到对应的镜像url中请求对应的资源,用户也可以使用同样的方法配置其他仓库的镜像。另外三个元素id,name,url与一般仓库配置无异,表示该镜像仓库的唯一标识符、名称以及地址。

由于私服可以代理任何外部的公共仓库(包括中央仓库),任何需要的构件都可以从私服获得,私服就是所有仓库的镜像。这时,可以配置这样的一个镜像

<settings>  
  ...  
  <mirrors>  
    <mirror>  
      <id>internal-repository</id>  
      <name>Internal Repository Manager</name>  
      <url>http://192.168.1.100/maven2</url>  
      <mirrorOf>*</mirrorOf>  
    </mirror>  
  </mirrors>  
  ...  
</settings> 

为了满足一些复杂的需求,Maven还支持更高级的镜像配置:
1. <mirrorOf>*</mirrorOf>
匹配所有远程仓库。

2.<mirrorOf>external:*</mirrorOf>
匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。

3.<mirrorOf>repo1,repo2</mirrorOf>
匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。

4.<mirrorOf>*,!repo1</miiroOf>
匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。

注意:由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件。

6.maven常用命令
mvn clean清除工程编译出来的字节码文件和resources文件,即删除target目录。
mvn package编译工程并将工程打成jar包,存放在target中
mvn install编译工程,将工程打成jar包并部署到本地仓库,路径为本地仓库/groupId.artifactId.version
mvn deploy编译工程,将工程打成jar包并部署到本地仓库,然后发布到配置好的私服中
mvn test编译工程并跑单元测试
mvn dependency:tree 查看工程的依赖树

Maven命令常用参数

-D 指定参数化的值。比如pom.xml文件中在properties标签下设置了参数<ereading.version>1.0< ereading.version>可通过mvn clean package –Dereading.version=1.1来指定ereading.version的值为1.1
-U 强制更新快照版本的jar包
-X 命令行执行maven命令的时候打印debug日志

猜你喜欢

转载自blog.csdn.net/sx1119183530/article/details/82085123