在 Docker 搭建 Maven 私有庫

小引

If you are developing software without a repository manager you are likely missing a number of opportunities to reduce some pretty obvious inefficiencies. If everyone on your team has to hit public repositories like the Central Repository to download components, you are missing out on some simple gains in speed and efficiency. If you don’t have a local place to deploy components you are forced to share binary components using half-measures and compromises such as storing binaries in source control. Stop developing in the Dark Ages, read this book, and start using a repository manager. Trust us, once you start using a Nexus Repository Manager, you’ll wonder how you ever functioned without it. 此文源自Repository Management with Nexus

破船譯:在軟件開發中,如果沒有使用倉庫管理工具(repository manager),你將與高效率擦肩而過。如果團隊中的每個開發人員都去類似 Central Repository 這樣公共的倉庫中獲取組件,那麼肯定會在速度和效率上得不償失。在本地,如果沒有提供一個地方來部署組件,那你使用的策略肯定需要作出許多妥協,比如在代碼版本庫中存儲二進制文件(非常不建議這樣做)。停止如此原始的開發方式吧,請閱讀此書,並開啓 repository manager 時代吧。一旦你開始使用 repository manager(Nexus),你肯定會抱怨爲什麼以前不使用它呢。

作爲一個希望讓正確的事情持續發生的人來說,對速度和效率的追求是一個永恆的話題。身爲開發人員的我們,如今在開發過程中,無論是 Android、iOS 或後端開發,都不可避免會用到第三方庫,這也也催生了許多優秀的第三方依賴管理工具,例如 iOS 的 CocoaPods,Java 後端開發的 Maven,Android 則使用 Gradle。而由於 綠色環境的原因,有的人可能在下載第三方依賴時陷入了漫長的等待中,或許可以美名其曰:喝杯咖啡的時間剛剛好。但是當喝完咖啡回來時,發現屏幕上出現的是 TIMEOUT,頓時陷入了沉思。或許對於沒有發現因此而催生的特色產業(代理、VPN)人來說,已經開始懷疑人生了。當然,針對這個問題,國內也有許多加速器可以使用,或許別的辦法。不過,在這篇文章中,我不會告訴你如何尋找加速器、如何設置源、如何科學上網。本文我將給大家介紹如何搭建 Maven 私有庫。

簡介

我們在進行 Java 後端開發時,爲了解決工程對第三方庫依賴管理,一般都會使用 Maven 工程,大家都知道如果對每一個依賴庫都從 Central Repository 下載,那麼速度會很慢,有時候對於只有幾 K 的一個庫,都會下載不失敗(科學上網的可以忽略),這會讓人很抓狂,如果多人進行協同開發,那麼這樣的抓狂會被擴大。那麼比較好的解決辦法,就是搭建內部自己的 Maven 私有庫。我們希望私有庫提供這樣的功能,在工程下載依賴的時候,如果私有庫中存在這個依賴庫,則直接從這個私有庫中下載,如果私有庫中沒有所需要的依賴庫,私有庫先從 Central Repository 中下載並緩存,下次在獲取的時候,就直接使用該緩存即可。大家可能會問,有沒有這樣的工具呢?其實早就有了, Nexus 就是其中之一。下面就進入正文吧。

下載 Nexus 鏡像

這裏推薦的方案是利用 Docker 來搭建 Nexus 環境。這裏默認大家已經都有 Docker 環境了。

首先利用下面的命令下載最新的 nexus 鏡像

docker pull sonatype/nexus

下載過程如下所示:

docker pull sonatype/nexus
Using default tag: latest
latest: Pulling from sonatype/nexus

8d30e94188e7: Pull complete
9b5961d40d03: Pull complete
074855ae6153: Pull complete
cc0a3a494fba: Pull complete
8cfd0607bbfb: Pull complete
Digest: sha256:6bb1966022009da38c4f679b51d2106f4d595aa7b887ac0bf93d14ebe4c0faa2
Status: Downloaded newer image for sonatype/nexus:latest

創建並啓動 Nexus

首先創建一個數據卷容器,如下命令

docker run -d –name nexus-data sonatype/nexus echo “data-only container for Nexus”

然後啓動 Nexus

docker run -d -p 10081:8081 –name nexus –volumes-from nexus-data sonatype/nexus

上面的命令中,將本機的 10081 端口映射到容器中的 8081 端口,並加載 數據卷容器 nexus-data。

創建容器過程需要一段時間時間進行初始化,可以用如下命令查看相關日誌:

docker logs -f nexus

在瀏覽器輸入 http://localhost:10081/nexus/ 可以看到界面,說明 nexus 已經啓動好了。

使用 Nexus

在這個私有庫中,提供了一些倉庫地址給我們使用.

關於每個倉庫的意義何在,請移步到 Nexus 的官網中學習(文末給出了參考鏈接),此文不再進行介紹。

私有倉庫的使用有兩種方法,一種是對工程的 pom.xml 文件進行修改(這種方法只對 pom.xml 所屬工程生效),如下所示:
將如下的repositories節點添加到 pom.xml 文件即可,Maven 會到此文件中配置的私有庫,下載相應的依賴庫。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testnexus</groupId>
    <artifactId>zzx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>nexus</id>
            <name>Team Nexus Repository</name>
            <url>http://localhost:10081/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <dependencies>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.1.41</version>
    </dependency>

    </dependencies>
</project>

另外一種方法就是直接修改 maven 的配置文件,這樣所有工程默認都會使用私有庫。如下所示:

~/.m2/settings.xml 文件中,將 mirror 修改爲:

<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:10081/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

當利用 maven 進行工程打包時,可以看到,已經從私有庫中下載依賴了(當在另外一臺機器上使用相應的依賴庫時,就直接使用私有庫中所緩存的依賴了,不用再到互聯網中下載,是不是很節約時間!):

testnesus mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building zzx 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.pom
Downloaded: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.pom (9 KB at 2.9 KB/sec)
Downloading: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.jar
Downloaded: http://localhost:10081/nexus/content/groups/public/com/alibaba/fastjson/1.1.41/fastjson-1.1.41.jar (350 KB at 16.5 KB/sec)

關於更多內容,請上 Nexus 的官網學習。

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