4

1、前言
最近项目需要搭建maven私服,方便管理后期团队成员使用上传自己的包,因此决定使用nexus来搭建私服,搭建好的nexus地址

准备工作

阿里云服务器ECS一台 1核CPU 2G内存(注意:最低为2G,否则运行报错)

clipboard.png

开始安装

安装java

java的安装网上的文章好多,不过我是自己写的shell文件安装的,如下:

#!/bin/bash

# jdk install
# 请将下载的jdk-xxx-linux-xxx.tar.gz包与此脚本放置到同一目录
# 授予此脚本可执行权限(chmod +x install_jdk.sh)
# 在终端执行此脚本开始安装(./文件名)
# 注意:不可有多个版本的jdk包!
#      为了使配置的环境变量生效,安装完成后你应该重新登陆。

jvmpath=/usr/local/java
# 不存在
if [ ! -d "$jvmpath" ]; then
    echo "正在创建$jvmpath目录"
    sudo mkdir $jvmpath
    echo "目录$jvmpath创建成功"
fi

jdkfile=$(ls | grep jdk-*-linux-*.gz)
jdkdirname="jdk1.8.0_201"

if [ ! -f "$jdkfile" ]; then
   echo "正在下载jdk请稍等..."
   wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz"
fi

jdkfile=$(ls | grep jdk-*-linux-*.gz)

if [ -f "$jdkfile" ]; then

    sudo tar -zxvf $jdkfile -C /usr/local/java/

    echo "安装JDK成功"

    echo "配置环境变量"

    mv ~/.bashrc ~/.bashrc.backup.java
    cat ~/.bashrc.backup.java >> ~/.bashrc
    echo "PATH=\"$PATH:$jvmpath/$jdkdirname/bin\"" >> ~/.bashrc
    echo "JAVA_HOME=$jvmpath/$jdkdirname" >> ~/.bashrc
    echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> ~/.bashrc
    source ~/.bashrc
    echo "配置环境成功"

    echo "测试是否安装成功"
    java -version
    echo "安装成功"

fi

执行该shell文件,如下所示:

clipboard.png

安装maven

maven安装我也是自己写的shell文件,如果你们不想用我的可以到网上找文章看看吧,下面是我的shell文件:

#!/bin/bash

# maven install

mvnpath=/usr/local/maven
# 不存在
if [ ! -d "$mvnpath" ]; then
    echo "正在创建$mvnpath目录"
    sudo mkdir $mvnpath
    echo "目录$mvnpath创建成功"
fi

#apache-maven-3.6
echo "正在下载maven安装包,请稍等..."

wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://211.162.31.136/files/71480000031E20AE/mirrors.hust.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz"

mvnfile=$(ls | grep apache*maven-*.gz)


if [ -f "$mvnfile" ]; then

    #这个名字其实就是mvn .tar.gz解压之后的文件夹的名字
    mvndirname="apache-maven-3.6.0"

    #不能加 用'zxvf' 加了 z 就创建了包里面的apace* 文件夹,而我们只要把apace*文件夹下的文件全部解压到 mvnpath里面就好
    tar zxvf $mvnfile -C $mvnpath

    echo "安装maven成功"
    echo "配置环境变量"

    mv ~/.bashrc ~/.bashrc.backup.mvn
    cat ~/.bashrc.backup.mvn >> ~/.bashrc

    echo "PATH=\"$PATH:$mvnpath/$mvndirname/bin\"" >> ~/.bashrc
    echo "MAVEN_HOME=$mvnpath/$mvndirname" >> ~/.bashrc

    source ~/.bashrc

    echo "配置环境成功"
    echo "测试是否安装成功"
    mvn -v
    echo "安装成功"
else
    echo "没有找到maven文件"

fi

执行该shell文件,如下所示:

clipboard.png

安装nexus

nexus虽然我也是使用shell文件安装,但有些配置我们还是要手动设置,下面是安装shell文件:

#!/bin/bash
#判断是否是roo用户
if [ $(id -u) != "0" ]; then
        echo "Error:You must be root to run this script"
fi

#每次使用只需修改自定义内容即可
#自定义用户名和组
Group_Name="nexus"
User_Name="nexus"

#自定义nginx变量
Install_Path="/usr/local/nexus"
Version="nexus-3.15.0-01"
Package_Type=".tar.gz"

Package=$Version$Package_Type

#创建/usr/local/nexus目录
#mkdir /usr/local/nexus
if [ -e $Install_Path ]
then
    echo " $Install_Path 目录已经存在."
    echo " $Install_Path Directory Already Exists."
else
    echo " $Install_Path 目录正在创建."
    mkdir $Install_Path
fi

#下载nexus 文件
Setup_path="/root/"
cd $Setup_path
wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.15.0-01-unix.tar.gz


Group_User(){
egrep "^$Group_Name" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    echo "nexus 用户组正在添加."
    groupadd $Group_Name
else
    echo " The $Group_Name user group already exists."
    echo "nexus 用户组已经添加."
fi

#判断nexus用户是否存在
egrep "^$User_Name" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    echo "nexus 用户正在添加."
    useradd -g $Group_Name $User_Name
else
    echo "nexus 用户已经添加."
    echo " The $User_Name user already exists."
fi
}

Group_User

# 设置/usr/local/nexus 目录所属组和用户是nexus
chown -R nexus:nexus $Install_Path

#判断文件是否存在
if [ -e $Setup_path$Version$Package_Type ]
then
        echo "$Package The Package exists."
else
        echo "$Package The package does not exist."
fi

cd $Setup_path

#解压nexus包到/usr/local/nexus
tar -zxvf $Package -C $Install_Path

echo '设置环境变量'
mv ~/.bashrc ~/.bashrc.backup.nexus
cat ~/.bashrc.backup.nexus >> ~/.bashrc
echo "NEXUS_HOME=$Install_Path/$Version" >> ~/.bashrc
echo "PATH=\"$PATH:$NEXUS_HOME/bin\"" >> ~/.bashrc

# 切换nexus用户
su nexus

echo '接下来配置:1、vim bin/nexus.rc run_as_user="nexus"'

安装完成之后,我们到/usr/local/nexus/nexus-3.15.0-01/bin目录下修改nexus文件

# 设置本地jdk目录
INSTALL4J_JAVA_HOME_OVERRIDE="/usr/local/java/jdk1.8.0_201"

接着,我们相同目录下nexus.rc文件,如下

# 指定用户是nexus而不是root,如果是root会出现警告!
run_as_user="nexus"

好了,这样就安装好了,我们访问下网站,注意默认端口是8081,账号:admin,密码:admin123

clipboard.png

免费申请阿里云SSL证书

clipboard.png

购买成功之后,配置好域名,我这里的域名是: nexus.awbeci.com,设置好之后等待审核,审核成功之后选择nginx版本并下载证书

clipboard.png

clipboard.png

下载完成之后是个.zip的压缩包,我们上传到服务器上,使用下面命令:

scp your-cert.zip root@your-server-ip:/your-server-directory

上传成功之后我们等待下一步操作。

安装nginx并代理nexus 8081端口,同时配置https访问

因为访问网站的时候端口是8081,所以想要使用80端口访问的话,我们就用nginx 80端口代理8081,同时设置https访问

安装nginx 我们还是通过shell文件来安装,如下

#!/bin/bash
#判断是否是roo用户
if [ $(id -u) != "0" ]; then
        echo "Error:You must be root to run this script"
fi

#每次使用只需修改自定义内容即可
#自定义用户名和组
Group_Name="nginx"
User_Name="nginx"

#自定义nginx变量
Install_Path="/usr/local/nginx"
Package_Type=".tar.gz"
Version="nginx-1.15.8"
Package=$Version$Package_Type
Setup_path="/root/"
RPM="nginx"

#创建/usr/local/nginx目录
#mkdir /usr/local/nginx
if [ -e $Install_Path ]
then
    echo " $Install_Path 目录已经存在."
    echo " $Install_Path Directory Already Exists."
else 
    echo " $Install_Path 目录正在创建."
    mkdir $Install_Path
fi 
 
#下载nginx 文件
cd $Setup_path
wget http://nginx.org/download/nginx-1.15.8.tar.gz

#安装依赖关系
yum group install "Development Tools" "Server Platform Deveopment"
yum install -y curl openssl-devel pcre-devel
Group_User(){
egrep "^$Group_Name" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    echo "nginx 用户组正在添加."
    groupadd $Group_Name
else
    echo " The $Group_Name user group already exists."
    echo "nginx 用户组已经添加."
fi

#判断nginx用户是否存在
egrep "^$User_Name" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    echo "nginx 用户正在添加."
    useradd -g $Group_Name $User_Name
else
    echo "nginx 用户已经添加."
    echo " The $User_Name user already exists."
fi
}

Group_User

#判断文件是否存在
if [ -e $Setup_path$Version$Package_Type ]
then
        echo "$Package The Package exists."
else
        echo "$Package The package does not exist."
fi

#编译安装nginx
cd $Setup_path

#解压nginx包到/usr/local/nginx 
tar -zxvf $Package -C $Install_Path

cd $Install_Path

cd $Version

configure_opts=(
    --prefix=$Install_Path 
    --user=nginx 
    --group=nginx 
    --with-http_ssl_module 
    --with-http_flv_module
    --with-http_stub_status_module 
    --with-http_gzip_static_module 
)

./configure ${configure_opts[@]}

if [[ $? -eq 0 ]]
then
    make && make install
else
    echo "编译失败,请重新编译" && exit 1
fi

#添加Nginx命令到环境变量
cat >/etc/profile.d/nginx.sh <<EOF
export PATH=/usr/local/nginx/sbin/:$PATH
EOF
source /etc/profile

#启动服务
/usr/local/nginx/sbin/nginx
ss -tnlp | grep nginx

安装成功后,我们把一步上传的证书.zip复制到/root文件夹下,并解压缩,如下:

# 创建ssl文件夹
mkdir -p /usr/local/nginx/cert

# 把上一步的.zip证书解压并复制到ssl文件夹下
unzip /root/your-cert-package.zip

# 解压之后应该是两个文件.pem和.key
# 复制.crt和.key文件到ssl目录下
cp your-cert.crt your-cert.key /usr/local/nginx/cert

设置好之后,接下来我们配置/usr/local/nginx/conf/nginx.conf文件,如下所示:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 100m;
    client_header_timeout    1m;
    client_body_timeout      1m;

    proxy_connect_timeout 18000; ##修改成半个小时
    proxy_send_timeout 18000;
    proxy_read_timeout 18000;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  nexus.awbeci.com;
        return       301 https://nexus.awbeci.com$request_uri;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_pass http://127.0.0.1:8081; #代理8081端口
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port $server_port;
        }
    }


    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  nexus.awbeci.com;

        ssl_certificate      /usr/local/nginx/cert/nexus.awbeci.com.pem;
        ssl_certificate_key  /usr/local/nginx/cert/nexus.awbeci.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
              proxy_pass http://127.0.0.1:8081; #代理8081端口
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header X-Forwarded-Proto $scheme;
              proxy_set_header X-Forwarded-Port $server_port;
        }
    }

}

这样就成功的配置好了,现在我们重启下nginx,并访问nexus.awbeci.com网站看看

# 重启nginx
/usr/local/nginx/sbin/nginx -s reload

clipboard.png

开始使用Nexus

Nexus介绍

这里有一篇文章写得非常好,大家可以看看,我就不特别详细的写介绍了,主要还是告诉你们怎么结合项目使用。

创建Blob Stores

clipboard.png

创建User

clipboard.png

创建阿里云的代理仓库

clipboard.png

然后再public组里面将这个aliyun-proxy仓库加入,排在maven-central之前即可

clipboard.png

创建Host仓库 策略是release

clipboard.png

创建Host仓库 策略是snapshots

clipboard.png

创建好之后我们再来Public设置下优先顺序,把刚才加的两个仓库放到aliyun-proxy前面

clipboard.png

创建完仓库预览

clipboard.png

配置本地maven settings.xml

提示:两种配置方法,一种是直接配置maven目录下的conf下的settings.xml文件,另外一种是复制该文件到用户目录下的.m2目录,两种方法配置效果是一样的,看个人喜好了,加载顺序是.m2下的settings.xml目录接着是maven config目录下的settings.xml,配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<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">
          
  <pluginGroups>
  </pluginGroups>

  <proxies>    
  </proxies>

  <servers>
    <!--这里配置我们刚才创建的user用户所对应的releases-->
    <server>
        <id>releases</id>
        <username>user</username>
        <password>123456</password>
    </server>
    <!--这里配置我们刚才创建的user用户所对应的Snapshots-->
    <server>
        <id>snapshots</id>
        <username>user</username>
        <password>123456</password>
    </server>
  </servers>
  <mirrors>
    <!-- <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> -->
    <!--这里配置我们线上的public仓库就好-->
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>https://nexus.awbeci.com/repository/maven-public/</url>
    </mirror>
  </mirrors>
</settings>

完整的setting.xml文件

<?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.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>/path/to/local/repo</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>
    -->
    <pluginGroup>com.spotify</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>
        <id>awbeci-releases</id>
        <username>user</username>
        <password>repopwd</password>
    </server>
    <server>
        <id>awbeci-snapshots</id>
        <username>user</username>
        <password>repopwd</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>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>https://nexus.awbeci.com/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>artifactory</id>
      <repositories>
        <!-- <repository>
          <id>Nexus</id>
          <url>https://nexus.awbeci.com/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
        <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository> -->
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>releases</id>
          <name>releases</name>
          <url>https://nexus.awbeci.com/repository/maven-releases/</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>snapshots</name>
          <url>https://nexus.awbeci.com/repository/maven-snapshots/</url>
        </repository>
      </repositories>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
    </profile>
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
<activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

配置要上传到nexus项目pom.xml文件

<!--后缀是RELEASE是正式版本,后缀是SNAPSHOTS是snapshots版本-->
<version>1.0-RELEASE</version>
    <packaging>jar</packaging>
    <distributionManagement>
        <!--配置线上releases仓库地址,只要是正式版本都会上传到该地址(注意要和settings.xml文件里面的配置名称相同)-->
        <repository>
            <id>releases</id>
            <url>https://nexus.awbeci.com/repository/awbeci/</url>
        </repository>
        <!--配置线上Snapshots仓库地址,只要是快照版本都会上传到该地址(注意要和settings.xml文件里面的配置名称相同)-->
        <snapshotRepository>
            <id>snapshots</id>
            <url>https://nexus.awbeci.com/repository/awbeci-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

编辑并发布jar包

mvn clean && mvn deploy -DskipTests=true

注意:如果<version>版本号里面含有RELEASE,则会上传到release版本上,如果含有SNAPSHOTS则会上传到snapshort版本上,千万注意!!!
maven deploy无法上传到私有库的release路径下问题
执行完成后,我们到线上看看是否上传成功

clipboard.png

clipboard.png

可以看到不管是release版本还是snapshot版本都上传并发布成功

测试发布的包

自己新建一个maven项目,然后引入我们刚才发布的release包

<dependency>
    <groupId>com.awbeci</groupId>
    <artifactId>awbeci-core</artifactId>
    <version>1.0.8-SNAPSHOT</version>
</dependency>

clipboard.png
执行该代码,如下所示:

clipboard.png

测试成功!!!

上传第三方包

有两种方式,一种是命令

mvn deploy:deploy-file \
    -DgroupId=<group-id> \
    -DartifactId=<artifact-id> \
    -Dversion=<version> \
    -Dpackaging=<type-of-packaging> \
    -Dfile=<path-to-file> \
    -DrepositoryId=<server-id-settings.xml> \
    -Durl=<url-of-the-repository-to-deploy>

另外一种是使用Nexus上传

clipboard.png

两种方式结果都是一样,就看你偏向哪种了。

总结

服务器内存刚开始配置是1CPU 1G 内存,nexus start运行之后报错,升级服务器为2G内存之后就没问题了

nexus 默认是8081端口,我们可以修改文件/usr/local/nexus/nexus-3.15.0-01/etc/nexus-default.properties

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
# 设置成自己想要的端口
application-port=8081
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

值得注意的是不能把端口直接改成80,这样你就不能启动nexus,所以我们是通过nginx 80端口代理8081端口了。

nexus 配置内存是在 /usr/local/nexus/nexus-3.15.0-01/bin/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

最好自己创建nexus用户,不要使用root用户启动nexus否则会出现警告

WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended! 
WARNING: ************************************************************
Starting nexus

启动nexus:

//注意:切换到nexus用户启动nexus
su nexus
/usr/local/nexus/nexus-3.15.0-01/bin/nexus {start|stop|run|run-redirect|status|restart|force-reload}

nexus新版本地址:
nexus下载地址

注意

如何本地出现was cached in the local repository错误,如下所示:

 Plugin com.spotify:docker-maven-plugin:1.4.10 or one of its dependencies could not be resolved: Failure to find com.spotify:docker-maven-plugin:jar:1.4.10 in https://nexus.awbeci.com/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced -> [Help 1]

解决方案:在本地.m2文件夹找到*.lastUpdated文件,删除即可!
https://blog.51cto.com/qiangs...

引用

  1. Maven私服Nexus3.x环境构建操作记录
  2. Nexus 3.x Linux环境搭建(手把手) 排坑之旅
  3. Centos7.3安装nexus-3.14.0-04
  4. 搭建nexus3版的maven私服(Centos7环境)
  5. Gitlab+Nexus Maven部署
  6. Linux 使用 Nexus3.x 搭建 Maven 私服指南
  7. maven私服nexus3.x搭建与使用
  8. centos7搭建nexus maven私服
  9. centos7搭建nexus maven私服
  10. Maven Nexus
  11. Nexus 私有仓库搭建与 Maven 集成
  12. Nexus max file descriptors
  13. maven私服nexus3.x环境配置
  14. 搭建Maven私服-Nexus

Awbeci
3.1k 声望213 粉丝

Awbeci