搭建Maven私服
在开发过程中,有时候会使用到公司内部的一些开发包,显然把这些包放在外部是不合适的。另外,由于项目一直在开发中,这些内部的依赖可能也在不断的更新。可以通过搭建公司内部的Maven服务器,将第三方和内部的依赖统一管理。
????? 这里使用Nexus来搭建本地的Maven服务器,过程比较简单。
????? 一、安装服务器
????? 1、下载
????? 我们可以在nexus的官网上找到它的相关介绍,下载地址是:http://nexus.sonatype.org/downloads/,在这里可以找到最新的版本,如果需要以前的版本,在官网上应该也可以找到下载地址。我下载的是:nexus-oss-webapp-1.8.0-bundle.tar.gz。关于Nexus的详细使用方法可以参照:Repository Management with Nexus.
????? 2、安装
????? 解压下载的文件:
????? # tar xzvf nexus-oss-webapp-1.8.0-bundle.tar.gz
????? 解压后会在同级目录中,出现两个文件夹:nexus-oss-webapp-1.8.0和sonatype-work,前者包含了nexus的运行环境和应用程序,后者包含了你自己的配置和数据。
????? 3、启动nexus
????? 在上面的提到,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,
?????
?????
?????? 二、配置Nexus
?????? 由于在新搭建的nexus环境中只是一个空的仓库,所以第一步就是要和远程的Maven中心仓库进行同步。
???????
??????
???????
?????? 如果在Reindex之后,并没有同步到远程的仓库,可以检查每个仓库的设置。下面是Maven Central的设置:
??????
?????? 三、在项目中使用私服
??????
?????? 在完成了上面的配置后,就可以将项目中默认的Repository切换为本地的私服了,只需要在pom.xml中增加repositories就可以了:
??????
view plaincopy to clipboardprint?
01.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
02.? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">?
03.? <modelVersion>4.0.0</modelVersion>?
04.?
05.? <groupId>org.maven.demo</groupId>?
06.? <artifactId>MavenDemo</artifactId>?
07.? <version>0.0.1-SNAPSHOT</version>?
08.? <packaging>jar</packaging>?
09.?
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>?
<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://172.28.189.138:8081/nexus/content/groups/public/</url>
??</repository>
?</repositories>
?<pluginRepositories>
??<pluginRepository>
???<id>public</id>
???<name>Public Repositories</name>
???<url>http://172.28.189.138: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服务器中了。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mydeman/archive/2010/09/25/5905424.aspx