項目開發過程中,有時候會使用到公司內部的一些開發包,顯然把這些包放在外部是不合適的。另外,由於項目一直在開發中,這些內部的依賴可能也在不斷的更新。可以通過搭建公司內部的Maven服務器,也會遇見每次在第三方網站加載jar太慢,所以公司會將第三方和內部的依賴統一管理。
這裏使用Nexus來搭建本地的Maven服務器,過程比較簡單。
一、安裝服務器
1、下載
我們可以在nexus的官網上找到它的相關介紹,下載地址是:http://nexus.sonatype.org/downloads/,在這裏可以找到最新的版本,
如果需要以前的版本,在官網上應該也可以找到下載地址。我下載的是:nexus-2.14.5-02-bundle.tar.gz。
關於Nexus的詳細使用方法可以參照:Repository Management with Nexus.
2、安裝
解壓下載的文件:
# tar xzvf nexus-2.14.5-02-bundle.tar.gz
解壓後會在同級目錄中,出現兩個文件夾:nexus-2.14.5-02和sonatype-work,前者包含了nexus的運行環境和應用程序,後者包含了你自己的配置和數據。
3、啓動nexus
在上面的提到,nexus的運行環境在nexus-2.14.5-02目錄,下面就進入這個目錄啓動:
# cd nexus-2.14.5-02/bin/
# ./nexus
執行上面的命令,可以得到nexus命令的用法提示:start 命令啓動,stop命令停止。下面啓動nexus:
# ./nexus start
Starting Nexus OSS...
Started Nexus OSS
從控制檯輸出可以看到Nexus已經啓動成功,我們可以通過log文件查看更詳細的信息:
# cd ~/nexus-2.14.5-02/log
# tail -f wrapper.log
在log中可以看到nexus默認監聽的端口是8081。那麼我們就可以在瀏覽器中訪問:http://hosthost:8081/nexus
配置linux系統的防火牆8081可訪問,
問題1:如果使用root用戶,那麼會出現If you insist running as root, then set theenvironment variable RUN_AS_USER=root before running this script.
解決:配置profile文件,增加export RUN_AS_USER=root 配置
二、配置Nexus
由於在新搭建的nexus環境中只是一個空的倉庫,所以第一步就是要和遠程的Maven中心倉庫進行同步。
如果在Reindex之後,並沒有同步到遠程的倉庫,可以檢查每個倉庫的設置。下面是Maven Central的設置:
三、在項目中使用私服
在完成了上面的配置後,就可以將項目中默認的Repository切換爲本地的私服了,只需要在pom.xml中增加repositories就可以了
<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>org.maven.demo</groupId>
<artifactId>MavenDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenDemo</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.206.132:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.206.132:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
將pom.xml保存後,再回過頭來了看去nexus管理界面看,就會發現junit已經被下載到本地的nexus服務器中了。
結束。。。。