4.maven 镜像和私有仓库使用

maven的三大特性——依赖管理、生命周期管理和项目管理前面已详述,实际使用中一般的依赖管理都是类似如下结构,可以配置镜像和私有仓库。
4.maven 镜像和私有仓库使用_第1张图片

配置镜像

Maven的包从仓库中拉取引用,仓库按照本地仓库、私有仓库、镜像仓库、远程仓库的顺序依次访问,前面取到包则后续提前返回,默认maven的本地仓库直接下载保存在~/.m2/repository。
对于一般个人项目,国外maven仓库太慢,可以考虑配置国内常用阿里和华为的镜像(参考 https://mirrors.huaweicloud.com/home和https://developer.aliyun.com/mvn/guide),如下设置setting.xml

<mirrors>
  <mirror>
    <id>aliyunmavenid>
    <mirrorOf>*mirrorOf>
    <name>阿里云公共仓库name>
    <url>https://maven.aliyun.com/repository/publicurl>
  mirror>
  <mirror>
      <id>huaweicloudid>
      <mirrorOf>*mirrorOf>
      <url>https://repo.huaweicloud.com/repository/maven/url>
  mirror>
mirrors>

配置私有仓库

私有仓库配置需要在profiles下增加自己的profile说明当前私有仓库的地址和标志id,如下配置即可
maven实际拉取时会先从profile处地址拉取,找不到才从mirror处地址拉取

<profiles>
    <profile>
        <id>nexusid>
        <repositories>
          <repository>
            <id>nexus-releasesid>
            <name>Nexus Release Repositoryname>
            <url>http://192.168.1.7:8081/nexus/content/repositories/releases/url>
            <releases><enabled>trueenabled>releases>
            <snapshots><enabled>falseenabled>snapshots>
          repository>
          <repository>
            <id>nexus-snapshotsid>
            <name>Nexus Snapshot Repositoryname>
            <url>http://192.168.1.7:8081/nexus/content/repositories/snapshots/url>
            <releases><enabled>falseenabled>releases>
            <snapshots><enabled>trueenabled>snapshots>
          repository>
        repositories>
    profile>
profiles>

<activeProfiles>
    <activeProfile>nexusactiveProfile>
activeProfiles>

发布到私有仓库

如果想发布到如上私有仓库,首先如下配置私有仓库用户名密码

<servers>  
  <server>
    <id>nexus-releasesid>
    <username>adminusername>
    <password>admin123password>
  server>
  <server>
    <id>nexus-snapshotsid>
    <username>adminusername>
    <password>admin123password>
  server>
servers>

自动发布

在需要打包发布到仓库的项目pom中增加发布地址如下

<distributionManagement>
    <repository>
      <id>nexus-releasesid>
      <name>Nexus Release Repositoryname>
      <url>http://192.168.1.7:8081/nexus/content/repositories/releases/url>
    repository>
    <snapshotRepository>
      <id>nexus-snapshotsid>
      <name>Nexus Snapshot Repositoryname>
      <url>http://192.168.1.7:8081/nexus/content/repositories/snapshots/url>
    snapshotRepository>
distributionManagement>

IDEA点击maven发布即可,也可如下命令行执行发布
这里-P指定发布仓库id,-U说明强制刷新依赖的snapshot包拉取最新

mvn clean deploy -Dmaven.test.skip=true -Dgpg.skip -Dmaven.javadoc.skip=true -f pom.xml -Pnexus-releases -U

手动发布

对于部分项目第三方包可以手动发布到本地或私有仓库,如下操作
手动安装第三方jar包到本地

mvn install:install-file -Dfile=F://jar/push-sdk-2.2.15.jar -DgroupId=com.xiaomi -DartifactId=push-sdk -Dversion=2.2.15 -Dpackaging=jar
mvn install:install-file -Dfile=F://jar/bcprov-jdk16-1.45.0.jar -DgroupId=org.bouncycastle -DartifactId=bcprov-jdk16 -Dversion=1.45.0 -Dpackaging=jar

手动上传第三方jar包到nexus

mvn deploy:deploy-file -Dfile=F://jar/push-sdk-2.2.15.jar -DgroupId=com.xiaomi -DartifactId=push-sdk -Dversion=2.2.15 -Dpackaging=jar -DrepositoryId=nexus-releases -Durl=http://192.168.1.7:8081/nexus/content/repositories/releases/


参考

私有仓库搭建一般用nexus,可参考如下
https://cloud.tencent.com/developer/article/1540935
https://www.jb51.net/article/150483.htm

你可能感兴趣的:(#,Java,maven,java,mirror,profile)