在開發過程當中,有時候會使用到公司內部的一些開發包,顯然把這些包放在外部是不合適的。另外,因爲項目一直在開發中,這些內部的依賴可能也在不斷的更新。能夠經過搭建公司內部的Maven服務器,將第三方和內部的依賴統一管理。linux
這裏使用Nexus來搭建本地的Maven服務器,過程比較簡單。web
1、安裝服務器apache
一、下載瀏覽器
咱們能夠在nexus的官網上找到它的相關介紹,下載地址是:http://nexus.sonatype.org/downloads/,在這裏能夠找到最新的版本,若是須要之前的版本,在官網上應該也能夠找到下載地址。我下載的是:nexus-oss-webapp-1.8.0-bundle.tar.gz。關於Nexus的詳細使用方法能夠參照:Repository Management with Nexus.服務器
二、安裝app
解壓下載的文件:webapp
# tar xzvf nexus-oss-webapp-1.8.0-bundle.tar.gzmaven
解壓後會在同級目錄中,出現兩個文件夾:nexus-oss-webapp-1.8.0和sonatype-work,前者包含了nexus的運行環境和應用程序,後者包含了你本身的配置和數據。ui
三、啓動nexusurl
在上面的提到,nexus的運行環境在nexus-oss-webapp-1.8.0目錄,下面就進入這個目錄啓動:
# cd nexus-oss-webapp-1.8.0/bin/jsw/linux-x86-64/
在這個目錄下包含了一個文件夾和三個文件:lib、nexus、platform和wrapper,其中nexus就是啓動命令。
# ./nexus
執行上面的命令,能夠獲得nexus命令的用法提示:start 命令啓動,stop命令中止。下面啓動nexus:
# ./nexus start
Starting Nexus OSS...
Started Nexus OSS
從控制檯輸出能夠看到Nexus已經啓動成功,咱們能夠經過log文件查看更詳細的信息:
# cd ~/nexus-oss-webapp-1.8.0/log
# tail -f wrapper.log
在log中能夠看到nexus默認監聽的端口是8081。那麼咱們就能夠在瀏覽器中訪問:http://host:8081/nexus,
2、配置Nexus
因爲在新搭建的nexus環境中只是一個空的倉庫,因此第一步就是要和遠程的Maven中心倉庫進行同步。
若是在Reindex以後,並無同步到遠程的倉庫,能夠檢查每一個倉庫的設置。下面是Maven Central的設置:
3、在項目中使用私服
在完成了上面的配置後,就能夠將項目中默認的Repository切換爲本地的私服了,只須要在pom.xml中增長repositories就能夠了:
view plaincopy to clipboardprint?
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>org.maven.demo</groupId>
6 <artifactId>MavenDemo</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>jar</packaging>
9
10 <name>MavenDemo</name>
11 <url>http://maven.apache.org</url>
12
13 <repositories>
14 <repository>
15 <snapshots>
16 <enabled>true</enabled>
17 </snapshots>
18 <id>public</id>
19 <name>Public Repositories</name>
20 <url>http://172.28.189.138:8081/nexus/content/groups/public/</url>
21 </repository>
22 </repositories>
23 <pluginRepositories>
24 <pluginRepository>
25 <id>public</id>
26 <name>Public Repositories</name>
27 <url>http://172.28.189.138:8081/nexus/content/groups/public/</url>
28 </pluginRepository>
29 </pluginRepositories>
30 <dependencies>
31 <dependency>
32 <groupId>junit</groupId>
33 <artifactId>junit</artifactId>
34 <version>4.8.1</version>
35 <type>jar</type>
36 <scope>compile</scope>
37 </dependency>
38 </dependencies>
39 <properties>
40 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41 </properties>
42 </project>
將pom.xml保存後,再回過頭來了看去nexus管理界面看,就會發現junit已經被下載到本地的nexus服務器中了。