[Reprinted] maven learning (in) - private server nexus build

Continuing from the last time, I believe that everyone can’t stand the turtle speed network that maven downloads dependencies from foreign sites every time. Today, let’s take a look at how to build a “private warehouse server”. At present, nexus is a widely used private server software. One, the basic usage will be described below:

 

1. Download the latest version from the nexus official website

1.1  Download address: http://www.sonatype.org/downloads/nexus-latest-bundle.zip  (the latest version is 2.7.2-03)

(2015-07-02 Note: The above download address seems to have expired, the new address is http://www.sonatype.org/nexus/archived/ )

1.2  After downloading, unzip it to the specified directory, for example: c:\java\nexus\

1.3  Start nexus

%nexus_home%\bin\nexus.bat install (installed as a windows service)

%nexus_home%\bin\nexus.bat start (start)

tips : directly nexus.bat without parameters, will display a complete list of parameters

Refer to the figure below, if it cannot be enabled, check the jdk version, nexus requires version 1.7 (if the version is not correct, please adjust the environment variable JAVA_HOME to point to the directory of jdk1.7)

If it is still not enabled after changing to 1.7, it is recommended to use nexus.bat console console mode to enable it, so that you can see the detailed startup process

After the startup is successful, you can use http://localhost:8081/nexus  to enter the control interface

Click Log In in the upper right corner, the default username/password is admin/admin123

 

2. Add the jboss repository address

2.1  First understand the resource library type of nexus:

Hosted Repository  - nexus local resource repository (equivalent to the jar and pom file repository that already exists on the hard disk of the server where nexus is located)

Proxy Repository  - Proxy library, many open source official organizations have published Repository on the Internet for developers all over the world to use, after setting this type of repository in nexus, other developers using nexus are apparently requesting the nexus server However, the essence of nexus is that it is requesting these Internet resources. For the first time, the resources that are not on the server will be downloaded from the Internet to the nexus server (saved to the hard disk in the form of files), and then returned to the developer. Download When there is the same pom/jar request again, it will be returned directly from the server hard disk, and no longer request the Internet

Repository Group  - Resource group, for example, we have many resource repositories: Repository A, Repository B..., we can combine them into a special resource C, and then other developers configure the address of resource C in maven, so that When maven requests a jar package from resource C, it will actually search for resource A and resource B

 

2.2  Add jboss agent repository

The default official resource library of maven is http://repo1.maven.org/maven2/ , many third-party jar packages do not have it, so I will add another jboss resource library

Click Repositories on the left, and then click Add on the right, a drop-down menu will pop up, select Proxy Repository

Next, fill in with reference to the following figure:

Fill in the Respository ID here: jboss-public-repository-group (you can change the name yourself)

Respository Name这里填写:JBoss Public Maven Repository Group (可以自已改名字)

其它项保持默认

Remote Storage Location这里填写:https://repository.jboss.org/nexus/content/groups/public-jboss/ (这是jboss公布的公开资源库)

 

2.3 下载jboss eap 6.x的离线资源包

首次使用nexus时,私服服务器上实质上没有任何jar包的缓存,所以第一次使用时,其实跟直接到互联网上下载没啥区别,只有后面再请求相同的jar包,服务器上才会有jar文件缓存,为了加快速度,我们可以直接上jboss官网下载离线资源包

下载地址: htp://www.jboss.org/jbossas/downloads

然后解压到指定目录,建议放在 %nexus_home%\sonatype-work\nexus\storage\ 下

然后add一个hosted repository,参考下图:

 

2.4 把刚才添加的二个respoitory添加到Public Repositories组

刚才我们添加了二个新的资源库,检查一下是否包括在默认的Public Repositories组里

如果没包括在组里,参考上图处理一下,然后保存。最后建议更新一下索引,参考下图:

 

三、修改本机当前用户下.m2目录下的settings.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
 4           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 6 
 7   <mirrors>
 8     <mirror>
 9       <id>nexus</id>
10       <mirrorOf>*</mirrorOf>     
11       <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
12     </mirror>     
13   </mirrors>  
14   
15   <profiles>
16     <profile>
17       <id>nexus</id>
18       <repositories>
19         <repository>
20           <id>nexus</id>
21           <name>Nexus</name>
22           <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
23           <releases><enabled>true</enabled></releases>
24           <snapshots><enabled>true</enabled></snapshots>
25         </repository>
26       </repositories>
27       <pluginRepositories>
28         <pluginRepository>
29           <id>nexus</id>
30             <name>Nexus</name>
31             <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
32             <releases><enabled>true</enabled></releases>
33             <snapshots><enabled>true</enabled></snapshots>
34         </pluginRepository>
35       </pluginRepositories>
36     </profile>
37   </profiles>
38   <activeProfiles>
39     <activeProfile>nexus</activeProfile>
40   </activeProfiles>
41 
42  </settings>
复制代码

注:大家自行把这段xml中的地址换成nexus服务器的ip。

然后随便创建一个maven项目,执行mvn clean package 之类,观察下输出:

从上图可以看出,现在依赖项都是从nexus服务器下载的

 

四、eclipse中添加资源xml

最后再补充一个小技巧,这跟私服其实关系不大。每次我们在eclipse里创建maven project时,都会弹出一个界面,让我们选择“项目模板“,这个过程需要联网获取模板列表,而且每次都次重复获取,很不合理,如下图:

可以点击Confiure,会弹出以下界面,点击”Add Local Catalog“

先在浏览里访问 https://repository.jboss.org/nexus/content/groups/public-jboss/archetype-catalog.xml 这个地址,然后保存为xml文件,接下面的界面里 Catalog File里,选择这个xml文件,然后Description填写一个名字

然后在创建Maven项目时,就可以直接选择这个了,选择后,将直接读取本机xml的内容,比联网获取要快得多

 

最后给一个小技巧:

有时候,在单位的局域网里架构nexus服务器,服务器并不能访问外网,这样就无法通过代理库从互联网下载jar包,可以这样做:

开发人员的机器通常是能上网的,先在本机修改settings.xml,不走nexus私服,直接从互联网中央仓库下载一次,把所有东西编译好以后,将本机 "当前用户\.m2\repository"复制到nexus私服服务器的相应存储目录,一般则nexus同级目录下的sonatype-work\nexus\storage\central 目录,然后再登录nexus管理界面手动更新索引,以后其它人就能正常使用nexus私服了。

 

参考:http://www.cnblogs.com/yjmyzz/p/3519373.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326297936&siteId=291194637