前言
項目開發時,一般采用Maven來進行項目構建,管理,jar包下載,項目組開發人員開發時,只要在項目的pom.xml文件中,添加如下pom.xml信息從公共的庫中下載相關的Jar包以及依賴Jar包,但有時候公共的庫不是那么穩定而且下載速度往往很慢,一個團隊一起開發項目時,如果每個組員都去下載一遍所有依賴的Jar包,其實是一件很麻煩的事情,所以這時候就有必要搭建一個Maven的私庫了,這樣如果再添加其他的構件時,會先在Maven私庫下載好,以后才會下載到本地。以后,如果發現私服已經存在某一jar包,則會直接從私服下載,如果沒有再去網絡上下載,能大大的提高效率,因此很有必要搭建maven私服。

Maven私庫安裝部署
1.平台版本說明
- centos 7
- nexus-2.14.2
- JDK 8
- apache-maven-3.3.9
2.環境安裝:安裝nuxus maven私庫之前先要安裝好JDK、maven等必要環境
先安裝JDK
- 下載jdk 8,去官網下載jdk8
- 解壓jdk到當前目錄
tar -zxvf jdk-8u91-linux-x64.tar.gz
- 配置環境變量
vim /etc/profile 添加如下內容:JAVA_HOME根據實際目錄來 JAVA_HOME=/usr/java/jdk1.8.0_65 CLASSPATH=$JAVA_HOME/lib/ PATH=$PATH:$JAVA_HOME/bin export PATH JAVA_HOME CLASSPATH
再安裝Maven
- 下載maven
到Maven官網下載安裝文件,目前最新版本為3.3.9,下載文件為apache-maven-3.3.9-bin.tar.gz,下載可以使用wget命令;
- 進入下載文件夾,找到下載的文件,運行如下命令解壓
tar -xvf apache-maven-3.3.9-bin.tar.gz
- 配置環境變量
MAVEN_HOME=/usr/local/apache-maven-3.3.9 export MAVEN_HOME export PATH=${PATH}:${MAVEN_HOME}/bin
- 保存文件,並運行如下命令使環境變量生效
source /etc/profile
- 在控制台輸入如下命令,如果能看到Maven相關版本信息,則說明Maven已經安裝成功
mvn -v

3.Nexus Maven私庫安裝
- nexus maven下載,下載地址:https://www.sonatype.com/download-oss-sonatype,我下載的是nexus-2.14.2-01-bundle.tar.gz
- 安裝
linux版本無需安裝,直接解壓即可 tar -zxvf nexus-2.14.2-01-bundle.tar.gz
- 運行
進入bin目錄下,運行./nexus start,啟動服務
- 開機啟動
sudo ln -s /usr/local/nexus/nexus-2.14.2-01/bin/nexus /etc/init.d/nexus 使用 service nexus status/start/stop 查看nexus服務狀態、啟動服務、停止服務等 使用 chkconfig nexus on/off 設置nexus服務開機自啟動或者開機不啟動
Nexus Maven私庫的使用
1.初識Nexus Maven私庫
在地址欄里輸入服務IP地址和8081端口就可以打開用戶界面,例如http://192.168.2.1:8081,點Sign In登錄管理頁面,用戶名密碼為,admin和admin123,如下如所示,在Repositories頁面里顯示着,默認已經創建了5個倉庫(2個為group),直接可以拿來用,無需再自行創建倉庫。

8081為默認的端口號,要修改端口號可進入nexus-2.14.2-01-bundle\nexus-2.14.2\conf\打開nexus.properties文件,修改application-port屬性值就可以了。
一般用到的倉庫種類是hosted、proxy。Hosted代表宿主倉庫,用來發布一些第三方不允許的組件,比如oracle驅動、比如商業軟件jar包。Proxy代表代理遠程的倉庫,最典型的就是Maven官方中央倉庫、JBoss倉庫等等。如果構建的Maven項目本地倉庫沒有依賴包,那么就會去這個代理站點去下載,那么如果代理站點也沒有此依賴包,就回去遠程中央倉庫下載依賴,這些中央倉庫就是proxy。代理站點下載成功后再下載至本機。筆者認為,其實Maven這個自帶的默認倉庫一般情況下已經夠大多數項目使用了。特殊情況時在配置新的倉庫,指定url即可。
hosted 類型的倉庫,內部項目的發布倉庫
releases 內部的模塊中release模塊的發布倉庫
snapshots 發布內部的SNAPSHOT模塊的倉庫
3rd party 第三方依賴的倉庫,這個數據通常是由內部人員自行下載之后發布上去
proxy 類型的倉庫,從遠程中央倉庫中尋找數據的倉庫
group 類型的倉庫,組倉庫用來方便我們開發人員進行設置的倉庫
2.配置Nexus
- 開啟遠程索引
新搭建的neuxs環境只是一個空的倉庫,需要手動和遠程中心庫進行同步,nexus默認是關閉遠程索引下載,最重要的一件事情就是開啟遠程索引下載,點擊左邊Administration菜單下面的Repositories,找到右邊倉庫列表中的三個倉庫Apache Snapshots,Codehaus Snapshots和Maven Central,然后再沒有倉庫的configuration下把Download Remote Indexes修改為true,操作完后記得點擊下面的save按鈕如下圖:

然后在Apache Snapshots,Codehaus Snapshots和Maven Central這三個倉庫上分別右鍵,選擇Repari Index,這樣Nexus就會去下載遠程的索引文件。

默認是從中央倉庫中下載jar包,但有時候中央倉庫下載很慢,經常下載不下來,所有有時候需要修改遠程倉庫的地址,可以改成jboss倉庫https://repository.jboss.org/nexus/content/groups/public/,還可以改成http://maven.aliyun.com/nexus/content/groups/public/,后面這個庫速度很快,不錯。

3.nexus私庫的一些使用介紹
在左邊菜單欄里面有個Artifact Search, 在輸入框里面輸入你想要搜索的構件名字,比如:spring, 那么查詢結果如下:

右下角就是這個Jar包的POM坐標信息,我們要想使用這個私服倉庫,先在項目pom中配置相關私服信息:
<repositories> <repository> <snapshots> <enabled>true</enabled> </snapshots> <id>public</id> <name>Public Repositories</name> <url>http://192.168.199.201:8081/nexus/content/groups/public/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>Public Repositories</name> <url>http://192.168.199.201:8081/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories>
這樣這個Maven項目構建的時候會從私服下載相關依賴。當然這個配置僅僅是在此項目中生效,對於其他項目還是不起作用。如果相對Maven的其他項目也生效的話。需要修改全局的settings.xml文件。
修改settings.xml為:
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!--設置本地倉庫--> <localRepository>${user.home}/.m2/repository</localRepository> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> <!-- interactiveMode | This will determine whether maven prompts you when it needs input. If set to false, | maven will use a sensible default value, perhaps based on some other setting, for | the parameter in question. | | Default: true <interactiveMode>true</interactiveMode> --> <!-- offline | Determines whether maven should attempt to connect to the network when executing a build. | This will have an effect on artifact downloads, artifact deployment, and others. | | Default: false <offline>false</offline> --> <!-- pluginGroups | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. |--> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <!-- proxies | This is a list of proxies which can be used on this machine to connect to the network. | Unless otherwise specified (by system property or command-line switch), the first proxy | specification in this list marked as active will be used. |--> <proxies> <!-- proxy | Specification for one proxy, to be used in connecting to the network. | <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <!--設置私庫認證信息--> <servers> <server> <id>nexus</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshot</id> <username>admin</username> <password>admin123</password> </server> </servers> <!--設置私庫mirror 表示maven所有的請求都由nexus來處理--> <mirrors> <!-- <mirror> <id>ibiblio.org</id> <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url> <mirrorOf>central</mirrorOf> </mirror> --> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <!-- <mirrorOf>central</mirrorOf> --> <name>Nexus Mirror.</name> <url>http://192.168.199.201:8081/nexus/content/groups/public</url> </mirror> </mirrors> <!--設置maven私庫信息--> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <name>Nexus</name> <url>http://192.168.199.201:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Nexus</name> <url>http://192.168.199.201:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> <!--覆蓋maven中央倉庫設置開啟releases和snapshots版本的下載--> <profile> <id>central</id> <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> <activeProfile>nexus</activeProfile> <activeProfile>central</activeProfile> </activeProfiles> </settings>
之后所有本機的Maven項目就在私服下載組件,這是推薦的做法。
假如我們開發了一些自己的Jar供大家一起使用,就需要上傳該jar包。選中宿主庫——3rd party,之后選擇Artifact Upload上傳至宿主空間。

4.maven的打包發布
因為遠程倉庫需要安全認證,所以需要在本地maven的settings.xml中進行配置。
打開settings.xml,在settings根標簽下添加如下內容:
<server>
<id>snapshots</id>
<username>deployment</username>
<password>deployment</password>
</server>
<server>
<id>releases</id>
<username>deployment</username>
<password>deployment</password>
</server>
這里的用戶名和密碼就是用來部署的用戶名和它的密碼,id就是用來上傳構建的庫的ID,如下圖
安全認證配置好后,接下來打開你要部署的maven項目的pom.xml,在project元素下添加如下內容:
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.199.201:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>http://192.168.199.201:8081/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
一般都會配置兩個庫,上面那個是快照,下面那個是release版本,這兩個庫的id與setting文件中的id要保持一致,還要注意一點,pom文件中指定構件的version很重要,它會決定最后打包到哪個庫里面去:
<version>0.0.1-SNAPSHOT</version> <version>0.0.1</version>
例如上面指定的兩種version,第一個會打包到snapshots庫中去,而第二種會打包到releases庫中去,如果找不到對應的庫就會報錯,最后,執行下列指令:

出現以下頁面就發布成功:
以上就是本人在maven私庫搭建過程的一些記錄和心得,有些資料來自網絡,當然maven的功能遠不止這些,我也還在探索學習當中,希望能把這個工具用的更加充分、熟練,再跟大家分享,文章中若有錯誤后者不足之處,歡迎大家提出指正,謝謝~
