nexus搭建maven私服

1:下载安装

  下载地址:http://www.sonatype.org/nexus/go

  启动Nexus,就是启动一个web服务器,它的默认端口是8081。Nexus在一个名为Jetty的servlet容器中运行,它使用一个名为Tanuki Java Service Wrapper的本地服务包裹器启动。这个服务包裹器可以被配置成以Windows服务或Unix守护线程的形式运行 Nexus。要启动Nexus,需要为平台找到合适的启动脚本。要查看可用平台的列表,查看${NEXUS_HOME}/bin/jsw目录的内容。 

  可执行文件在${NEXUS_HOME} exus-2.9.1-02injswwindows-x86-32下: 

  InstallNexus.bat/UninstallNexus.bat是安装/卸载nexus为windows service。 
  ${NEXUS_HOME}/nexus-2.9.1-02/bin下的Nexus.bat是直接在命令行中启动Nexus,如果不想安装Nexus为windows service,可以用这个文件来手工控制Nexus的启动退出。 

2:配置

  首先登录,默认地址http://localhost:8081/nexus/,默认用户名密码为admin/admin123. 

  nexus默认是关闭远程索引下载功能的。开启的方式: 
  点击ViewRepositories下的Repositories,将这三个仓库Apache Snapshots,Codehaus Snapshots,Maven Central Release的 Download Remote Indexes修改为true。然后在这三个仓库上分别右键,选择update index,这样Nexus就会去下载远程的索引文件。

2.管理仓库

以管理员用户登陆然后点击左边导航菜单ViewRepositories下面的Repositories。Nexus提供了三种不同的仓库。 
(1)代理仓库 
  代理仓库是对远程仓库的一个代理。默认情况下,Nexus自带了如下配置的代理仓库: 
  Apache Snapshots    这个仓库包含了来自于Apache软件基金会的快照版本。http://people.apache.org/repo/m2-snapshot-repository 
  Codehaus Snapshots    这个仓库包含了来自于Codehaus的快照版本。 http://snapshots.repository.codehaus.org/ 
  Central Maven Repository 这是中央Maven仓库(发布版本)。 http://repo1.maven.org/maven2/ 
(2)宿主仓库 
  一个宿主仓库是由Nexus托管的仓库。Maven自带了如下配置的宿主仓库。 
  3rd Party 这个宿主仓库应该用来存储在公共Maven仓库中找不到的第三方依赖。这种依赖的样例有:你组织使用的,商业的,私有的类库如Oracle JDBC驱动。 
  Releases 这个宿主仓库是你组织公布内部发布版本的地方。 
  Snapshots 这个宿主仓库是你组织发布内部快照版本的地方。 
(3)虚拟仓库 
  一个虚拟仓库作为Maven 1的适配器存在。Nexus自带了一个central-m1虚拟仓库 

3. 管理组 
  组是Nexus一个强大的特性,它允许你在一个单独的URL中组合多个仓库。Nexus自带了两个组:public和public-snapshots。 public组中组合了三个宿主仓库:3rd Party, Releases, 和Snapshots,还有中央Maven仓库。而public-snapshots组中组合了Apache Snapshots和Codehaus Snapshots仓库。

4. 配置maven 

要让maven使用Nexus作为仓库,要修改~/.m2/settings.xml. 

 1 <profiles> 
 2    <profile> 
 3      <id>nexus</id> 
 4      <repositories> 
 5        <repository> 
 6            <id>nexus</id> 
 7            <name>local private nexus</name> 
 8            <url>http://localhost:8081/nexus/content/groups/public</url> 
 9        </repository> 
10      </repositories> 
11    </profile> 
12    <profile> 
13      <id>nexus-snapshots</id> 
14      <repositories> 
15        <repository> 
16            <id>nexus-snapshots</id> 
17            <name>local private nexus snapshots</name> 
18            <url>http://localhost:8081/nexus/content/groups/public-snapshots</url> 
19        </repository> 
20      </repositories> 
21    </profile> 
22 </profiles>
23 
24 <activeProfiles> 
25     <activeProfile>nexus</activeProfile> 
26     <activeProfile>nexus-snapshots</activeProfile> 
27 </activeProfiles> 

5.部署构件至Nexus

要部署构件至Nexus,在distributionManagement中提供仓库URL,然后运行mvn deploy。Maven会通过一个简单的HTTP PUT将项目POM和构件推入至你的Nexus安装。需要配置你项目POM中distributionManagement部分的repository。

 1 <distributionManagement> 
 2 <repository> 
 3     <id>releases</id> 
 4     <name>Internal Releases</name> 
 5     <url>http://localhost:8081/nexus/content/repositories/releases</url> 
 6 </repository> 
 7 <snapshotRepository> 
 8     <id>Snapshots</id> 
 9     <name>Internal Snapshots</name> 
10     <url>http://localhost:8081/nexus/content/repositories/snapshots</url> 
11 </snapshotRepository> 
12 </distributionManagement> 

  这样还没完,这时如果部署会报错,还要在~/.m2/settings.xml中添加如下的服务器登录信息:

 1 <server> 
 2 <id>releases</id> 
 3 <username>admin</username> 
 4 <password>admin123</password> 
 5 </server> 
 6 <server> 
 7 <id>Snapshots</id> 
 8 <username>admin</username> 
 9 <password>admin123</password> 
10 </server>

部署第三方构件: 
构件可能是私有数据库的JDBC驱动如Oracle,或者你依赖于另一个JAR,它既不开源也无法免费获得。在这样的情况下,你就需要手动拿来这些构件然后发布到你自己的仓库中。Nexus提供宿主的"third-party"仓库,就是为了这个目的。 
使用以下命令发布该文件至Nexus: 
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty

6.Nexus监听端口 
  默认情况下,Nexus监听端口8081。你可以更改这个端口,通过更改${NEXUS_HOME}/conf/plexus.properties的值,为此,停止Nexus,更改文件中applicationPort的值,然后重启Nexus。

7.Maven Profiles 
   Maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。profile可以在pom.xml中配置,并给定一个id。然后你就可以在运行Maven的时候使用的命令行标记告诉Maven运行特定profile中的目标。以下 pom.xml使用production profile覆盖了默认的Compiler插件设置。

 1 <profiles> 
 2    <profile> 
 3      <id>production</id> 
 4      <build> 
 5        <plugins> 
 6          <plugin> 
 7            <groupId>org.apache.maven.plugins</groupId> 
 8            <artifactId>maven-compiler-plugin</artifactId> 
 9            <configuration> 
10              <debug>false</debug> 
11              <optimize>true</optimize> 
12            </configuration> 
13          </plugin> 
14        </plugins> 
15      </build> 
16    </profile> 
17 </profiles> 

要使用production profile来运行mvn install,你需要在命令行传入-Pproduction参数。要验证production profile覆盖了默认的Compiler插件配置,可以像这样以开启调试输出(-X) 的方式运行Maven。 
    如果你开始大量使用Maven profile,你会希望将profile从POM中分离,使用一个单独的文件如profiles.xml。你可以混合使用定义在pom.xml中和外部 profiles.xml文件中的profile。只需要将profiles元素放到${basedir}目录下的profiles.xml文件中,然后照常运行Maven就可以。profiles.xml文件的大概内容如下:

 1 <profiles> 
 2     <profile> 
 3       <id>development</id> 
 4       <build> 
 5         <plugins> 
 6           <plugin> 
 7             <groupId>org.apache.maven.plugins</groupId> 
 8             <artifactId>maven-compiler-plugin</artifactId> 
 9             <configuration> 
10               <debug>true</debug> 
11               <optimize>false</optimize> 
12             </configuration> 
13           </plugin> 
14         </plugins> 
15       </build> 
16     </profile> 
17     <profile> 
18       <id>production</id> 
19       <build> 
20         <plugins> 
21           <plugin> 
22             <groupId>org.apache.maven.plugins</groupId> 
23             <artifactId>maven-compiler-plugin</artifactId> 
24             <configuration> 
25               <debug>false</debug> 
26               <optimize>true</optimize> 
27             </configuration> 
28           </plugin> 
29         </plugins> 
30       </build> 
31     </profile> 
32 </profiles> 

settings profile可以应用到所有你使用Maven构建的项目。你可以在两个地方定义settings profile:定义在~/.m2/settings.xml中的用户特定settings profile,或者定义在${M2_HOME}/conf/settings.xml中的全局settings profile。

原文地址:https://www.cnblogs.com/javaleon/p/3988734.html