Nexus介绍
Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven仓库服务器,在代理远程仓库的同时维护本地仓库,以节省带宽和时间,Nexus就可以满足这样的需要。此外,他还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。
Nexus不是Maven的核心概念,它仅仅是一种衍生出来的特殊的Maven仓库。对于Maven来说,仓库只有两种:本地仓库和远程仓库。

本地仓库就是我们在maven的setting.xml中配置的本地仓库地址。由于最原始的本地仓库是空的,Maven必须知道至少一个可用的远程仓库,才能在执行Maven命令的时候下载到需要的构件。中央仓库是maven默认的远程仓库。
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

jboss是一个开放源代码的应用服务器, java .net是指java仓库和.net仓库
Nexus的功能:
1、节省外网带宽。大量对于外部仓库的重复请求会消耗带宽,利用私服代理外部仓库,可以消除对外的重复构件下载,降低带宽的压力。
2、加速Maven构建。不停地连接请求外部仓库十分的耗时,Maven在执行构建的时候不停地检查远程仓库的数据。利用私服,Maven只检查局域网的数据,提高构建的速度。
3、部署第三方构件。当某个构件无法从任何一个外部远程仓库获得。建立私服之后,便可以将这些构件部署到私服,供内部的Maven项目使用。
4、提高稳定性,增强控制。Maven构建高度依赖于远程仓库,因此,当网络不稳定的时候,Maven构建也会变得不稳定,甚至无法构建。私服缓存了大量构建,即使暂时没有网络,Maven也可以正常的运行。
5、降低中央仓库的负荷。使用私服可以避免很多对中央仓库的重复下载,降低中央仓库的压力。
安装nexus
#官方下载地址
wget https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/3/nexus-3.30.0-01-unix.tar.gz
#个人下载地址
wget http://test.hzdlsoft.com:4459/nexus-3.30.0-01-unix.tar.gz
#安装到/data/sonatype目录
mkdir -p /data/sonatype
tar -zxvf nexus-3.30.0-01-unix.tar.gz -C /data/sonatype/
cd /data/sonatype;ln -s nexus-3.30.0-01 nexus;ls -l
#添加用户,设置权限
useradd -d "/home/nexus" -m -s "/bin/bash" nexus
passwd nexus
chown -R nexus:nexus /data/sonatype
修改配置
进入/data/sonatype/nexus/bin目录,查看文件
[root@localhost bin]# ll
total 32
drwxr-xr-x. 2 nexus nexus 4096 Mar 2 20:42 contrib
-rwxr-xr-x. 1 nexus nexus 17269 Feb 21 17:24 nexus
-rw-r--r--. 1 nexus nexus 36 Mar 2 21:12 nexus.rc
-rw-r--r--. 1 nexus nexus 465 Feb 21 17:24 nexus.vmoptions
修改nexus.rc中的run_as_user为nexus
# run_as_user=""
run_as_user="nexus"
查看nexus.vmoptions文件
[root@localhost bin]# vi nexus.vmoptions
-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log (日志目录)
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../sonatype-work/nexus3
-Djava.io.tmpdir=../sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false
进入/data/sonatype/nexus/etc目录
[root@localhost etc]# ll
total 8
drwxr-xr-x. 2 nexus nexus 171 Mar 2 20:42 fabric
drwxr-xr-x. 2 nexus nexus 157 Mar 2 20:42 jetty
drwxr-xr-x. 2 nexus nexus 4096 Mar 2 20:42 karaf
drwxr-xr-x. 2 nexus nexus 51 Mar 2 20:42 logback
-rw-r--r--. 1 nexus nexus 349 Mar 2 21:36 nexus-default.properties
drwxr-xr-x. 2 nexus nexus 26 Mar 2 20:42 ssl
修改nexus-default.properties中的port和host属性,其中host属性不能写127.0.0.1和localhost,端口修改为非默认的7081
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=7081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/
# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
nexus-pro-feature
nexus.hazelcast.discovery.isEnabled=true
启动和访问
启动命令
[root@localhost bin]# ./nexus start //后台启动,不显示日志
[root@localhost bin]# ./nexus run & //后台启动,显示日志
访问http://212.64.113.174:7081/

创建角色和用户




默认仓库类型:

group(仓库组类型):又叫组仓库,用于方便开发人员,自己设定的仓库
hosted(宿主类型):内部项目的发布仓库(内部开发人员发布上去存放的仓库)
proxy(代理类型):从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage Location属性的值,即被代理的远程仓库的路径)
virtual(虚拟类型):虚拟仓库(这个基本上用不到)
Public Repositories下的仓库类型
3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库,即第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去;
Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库
Central: 用来代理maven中央仓库中发布版本构件的仓库
entral M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库
Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库
Releases: 内部的模块中release模块的发布仓库,用来部署管理内部的发布版本构件的宿主类型仓库;release是发布版本;
Snapshots:发布内部的SNAPSHOT模块的仓库,用来部署管理内部的快照版本构件的宿主类型仓库;snapshots是快照版本,也就是不稳定版本
开启 release的重复发版权限
开发中需要重复发版,则需要开启release类型仓库的对应权限设置 allow redeploy


设置代理仓库(阿里云maven仓库)



点击页面下边的create repository 创建完成之后可以看到新增加了阿里云库
将添加的阿里云proxy加入默认group中

点击页面下的save创建完成后再次点进来就会看到当前nexus地址

常见使用场景
重点:
release发版仓库(nexus默认已建立:maven-releases)
snapshot 测试中心快照仓库(nexus默认已建立:maven-snapshots)
central 中央仓库 (nexus默认已建立:maven-central)
关于组:在nexus中可以建立组,将不同类型仓库集合在一起(nexus默认已建立:maven-public)
场景一: 自定义新建proxy类型仓库,在maven配置中分别配置不同调用地址,或是将自定义新建的proxy仓库统一加入一个组,在maven配置中调用一个地址
场景二: 使用maven已经建立好的proxy仓库,且使用已建立好的组(maven-public)配置maven调用地址
场景一和场景二本质上讲没有什么变化,根据公司开发习惯和需求进行配置即可
Maven配置nexus私服,将远程仓库设置成本地搭建的nexus私服
回到maven的配置文件路径修改主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.home}/conf/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>/web/.m2/repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
<server>
<!-- 发布Releases版的账号,ID要与distributionManagement中的Releases ID一致 -->
<id>Releases</id>
<username>admin</username>
<password>pqq10030</password>
</server>
<server>
<!-- 发布snapshot版的账号,ID要与distributionManagement中的snapshot ID一致 -->
<id>Snapshots</id>
<username>admin</username>
<password>pqq10030</password>
</server>
<server>
<!-- 配置本地mirror访问权限 -->
<id>all</id>
<username>admin</username>
<password>pqq10030</password>
</server>
</servers>
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<!-- mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
-->
<mirror>
<id>all</id>
<mirrorOf>*</mirrorOf>
<name>private</name>
<url>http://212.64.113.174:7081/repository/maven-public/</url>
</mirror>
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
<profile>
<id>all</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://212.64.113.174:7081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://212.64.113.174:7081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
<activeProfiles>
<activeProfile>all</activeProfile>
</activeProfiles>
</settings>
项目打包后,到nexus查看缓存

上传到maven-releases仓库
如果要打release包,可以将pom文件中的SNAPSHOT标识去掉
把这行 <version>1.0-SNAPSHOT</version> 改成 <version>1.0</version>
[root@sdw3 ec-bizlog-server]#vi /tmp/testdir/ec-bizlog-server/pom.xml
<version>1.0</version>
再次上传maven包,可以在终端看到上传到releases仓库中
[root@sdw3 ec-bizlog-server]# mvn deploy


上传第三方maven包到nexus私服中




Maven添加第三方仓库地址(全局配置),并将之前编译的包上传

[root@sdw3 ec-bizlog-server]# vi /web/maven/conf/settings.xml
#添加仓库
<repository>
<id>3rdnexus</id>
<url>http://212.64.113.174:7081/repository/3rd/</url>
<release><enable>true</enable></release>
<snapshots><enable>true</enable></snapshots>
</repository>
#添加一个3rd认证
<server>
<id>3rdnexus</id>
<username>admin</username>
<password>pqq10030</password>
</server>
```shell
[root@sdw3 target]# mvn deploy:deploy-file -DgroupId=com.hzecool.bbizlog -DartifactId=ec-bizlog-server -Dversion=1.0 -Dpackaging=jar -Dfile=/tmp/testdir/ec-bizlog-server/target/ec-bizlog-server-1.0.jar -Durl=http://212.64.113.174:7081/repository/3rd/ -DrepositoryId=3rdnexus


网友评论