在开发过程当中,有时候会使用到公司内部的一些开发包,显然把这些包放在外部是不合适的。另外,因为项目一直在开发中,这些内部的依赖可能也在不断的更新。能够经过搭建公司内部的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服务器中了。