1. 程式人生 > >在 Docker 搭建 Maven 私有庫

在 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

下載過程如下所示:

1234567891011
docker pull sonatype/nexusUsing default tag: latestlatest: Pulling from sonatype/nexus8d30e94188e7: Pull complete9b5961d40d03: Pull complete074855ae6153: Pull completecc0a3a494fba: Pull complete8cfd0607bbfb: Pull completeDigest: sha256:6bb1966022009da38c4f679b51d2106f4d595aa7b887ac0bf93d14ebe4c0faa2Status: 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

image

使用 Nexus

在這個私有庫中,提供了一些倉庫地址給我們使用,如下圖所示:

image

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

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

123456789101112131415161718192021222324252627
<?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 修改為:

12345678910111213141516171819202122232425262728293031323334353637
<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 進行工程打包時,可以看到,已經從私有庫中下載依賴了(當在另外一臺機器上使用相應的依賴庫時,就直接使用私有庫中所快取的依賴了,不用再到網際網路中下載,是不是很節約時間!):

12345678910
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.pomDownloaded: 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.jarDownloaded: 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 的官網學習。

參考資料