Maven Nexus私庫搭建及使用,你還不會嗎?

爲什麼要使用私庫

maven默認去遠程中央倉庫下載JAR包的,訪問國外網絡相當慢,如果團隊每個人都去下載一遍無疑是網絡的浪費,當然也可以添加國內的鏡像,如阿里的比較穩定,但如果想添加遠程不存在的像第三方公司的JAR包就比較麻煩。

所以,使用私庫,第一,開源包只要有一個人下載過其他人就不需要再下載了,直接從私庫下載即可。第二,可以用來管理第三方公司的或者遠程倉庫不存在的JAR包,或者公司不開源的JAR包。

推薦國內穩定的鏡像,如阿里的

http://maven.aliyun.com/nexus/content/groups/public/

nexus下載安裝

首先去sonatype官網下載nexus包,要下載開源免費版的OSS版,即Open Source Software。

https://www.sonatype.com/nexus-repository-oss

下載最新的3.X的版本,這裏以windows爲例進行下載。

下載後點擊bin目錄中的啓動文件即可,默認的端口是8081,訪問路徑是/,也可以去配置文件中修改,這裏以默認。

啓動後,打開localhost:8081,nexus默認的用戶名是admin/admin123

默認安裝有以下這幾個倉庫,在控制檯也可以修改遠程倉庫的地址,第三方倉庫等。

Maven配置

修改maven主目錄conf/setting.xml配置文件。

添加nexus認證的用戶名和密碼配置信息。

<servers>

    <server>
          <id>nexus-releases</id>
          <privateKey>admin</privateKey>
          <passphrase>admin123</passphrase>
    </server>

    <server>
          <id>nexus-snapshots</id>
          <privateKey>admin</privateKey>
          <passphrase>admin123</passphrase>
    </server>

</servers>

添加mirror鏡像

 <mirrors>
    <mirror>
          <id>Nexus</id>
          <mirrorOf>*</mirrorOf>
          <name>Nexus</name>
          <url>http://127.0.0.1:8081/repository/maven-public/</url>
     </mirror>
  </mirrors>

添加私庫

<profiles>
    <profile>
    <id>Nexus</id>
    <repositories>
        <repository>
            <id>Nexus</id>
            <name>Nexus</name>
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
        <id>Nexus</id>
        <name>Nexus</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        </pluginRepository>
    </pluginRepositories>
    </profile>
</profiles>

激活私庫

<activeProfiles>
      <activeProfile>Nexus</activeProfile>
</activeProfiles>

發佈到私庫

在pom配置文件中添加

<!-- nexus-releases nexus-snapshots與settings.xml中server下的id對應 -->
<distributionManagement>

    <repository>
        <id>nexus-releases</id>
        <name>Nexus Releases Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>

    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Nexus Snapshots Repository</name>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>

</distributionManagement>

在項目上使用命令mvn deploy打包就能發佈到私庫。

推薦去我的博客閱讀更多:

1.Java JVM、集合、多線程、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、後端、架構、阿里巴巴等大廠最新面試題

覺得不錯,別忘了點贊+轉發哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章