Maven 私服搭建

Maven 私服通常采用 Nexus 搭建,本文以 CentOS 为例,总结了 Nexus 搭建方法。搭建 Maven 私服的必要性有:(1)整个公司只需把因特网上的公共仓库缓存至局域网私服一次,公司内所有开发人员直接从局域网私服下载,速度很快;(2)公司内部开发的组件,上传至私服,供整个公司使用。

作者:王克锋
出处:https://kefeng.wang/2018/02/10/maven-nexus/
版权:自由转载-非商用-非衍生-保持署名,转载请标明作者和出处。

1 Nexus 相关概念

1.1 仓库类型

1.1.1 hosted(宿主仓库)

当前私服自己维护的仓库,Nexus 有下面三个(repositoryId):
- releases: 我们自己开发的包(Release 版),pom.xml 中 version 不包含“-SNAPSHOT”的情况,用于正式发布的版本;
- snapshots: 我们自己开发的包(Snapshot 版),pom.xml 中 version 包含“-SNAPSHOT”的情况,用于调试中的不稳定版本;
- thirdparty: 第三方开发的包,手工导入其中。

1.1.2 proxy(代理仓库)

代理远程的公共仓库,Nexus 有下面三个(repositoryId):
- central: Maven 仓库
- apache-snapshots: Apache 仓库
- codehaus-snapshots: Codehaus 仓库,注意 默认的链接 已失效,需要手工修改。
修改路径为:Sonatype Nexus / Views / Repositories / Codehaus Snapshots / Configuration / Remote Storage Location

1.1.3 virtual(虚拟仓库)

  • central-m1

1.1.4 group(仓库组)

一个 group 可包含多个 hosted/proxy 仓库,Nexus 有下面一个(repositoryId):
- public: 默认情况下按优先次序包含 releases/snapshots/thirdparty/central 这四个。可根据需要增减或调整次序。

1.2 权限管理(Security)

  • 权限(Privileges): 通常无需调整;
  • 角色(Roles): 通常无需调整;
  • 用户(Users): 可酌情调整。

1.3 预置用户

  • admin: Nexus 管理员,拥有最高全部权限,建议只用于 WEB 控制台;
  • deployment: Nexus 部署者,拥有所有仓库的完全控制权限,建议只用于私服专有仓库的维护;
  • anonymous: Nexus 匿名者,拥有所有仓库的只读权限,建议用于只读私服的普通开发者;

2 安装

必须先安装后所依赖的 JDK7+,建议使用 JDK8。

2.1 下载并解压

http://www.sonatype.org/nexus/

## https://www.sonatype.com/nexus-repository-oss
wget http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz
sudo tar -zxf nexus-latest-bundle.tar.gz -C /opt
sudo mv /opt/sonatype-work /opt/nexus-2.11.1-01/work

2.2 设置环境变量

## sudo vim /etc/profile ## 影响用户有效
export NEXUS_HOME=/opt/nexus-2.11.1-01
export PATH=$PATH:$NEXUS_HOME/bin
## 重新登录后生效

2.3 调整启动选项

2.3.1 调整安装目录属主为 admin

sudo chown -R admin:admin $NEXUS_HOME

2.3.2 调整启动用户为 admin

## sudo vim $NEXUS_HOME/bin/nexus
RUN_AS_USER=admin ## 总是以该用户启动服务

2.3.3 调整 nexus.properties

## sudo vim $NEXUS_HOME/conf/nexus.properties

# Jetty section
application-port=8081 ## 监听端口,建议默认(为安全或避免端口冲突时可调整)
application-host=192.168.126.101 ## 建议改为局域网IP(原为0.0.0.0,调整后安全且不影响之前操作)
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus ## 建议默认(为安全时可调整)

# Nexus section
nexus-work=${bundleBasedir}/work/nexus ## 已移至 $NEXUS_HOME 下
runtime=${bundleBasedir}/nexus/WEB-INF

2.3.4 调整管理员密码

## 文件 security.xml,在首次启动服务,并浏览 WEB 控制台后才生成
## sudo vim $NEXUS_HOME/work/nexus/conf/security.xml
## 用命令 md5sum 加密

2.4 启动

sudo $NEXUS_HOME/bin/nexus status
sudo $NEXUS_HOME/bin/nexus start
sudo $NEXUS_HOME/bin/nexus stop
sudo $NEXUS_HOME/bin/nexus restart

2.5 防火墙放行

### sudo vim /etc/sysconfig/iptables
### 重启生效: sudo systemctl restart iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT

2.6 Windows 设置 hosts

192.168.126.101    nexus
# 192.168.90.155    nexus

3 管理员配置

3.1 登录

http://nexus:8081/nexus/index.html
默认账号/密码: admin/admin123

3.2 调整宿主仓库

路径: Views / Repositories / 各个 Repository[type=hosted] / Configuration(操作之后按 Save 按钮)
- Deployment Policy: Allow Redeploy

3.3 下载远程索引

下载远程索引之后,可以在本地搜索中使用。
路径: Views / Repositories / 各个 Repository[type=proxy] / Configuration(操作之后按 Save 按钮)
- Download Remote Indexes: True

3.4 设置 Deployment 账户密码

路径: Security / Users / User[id=deployment] / 右键菜单 Set Password(操作之后按 Save 按钮)
- New Password: deployment123
- Confirm Password: deployment123

4 发布者维护公司开发库

4.1 Maven 全局配置私服帐号

<!-- %MAVEN_HOME%\conf\settings.xml -->

<!-- 针对开发库发布者: 指定私服帐号 -->
<servers>
    <server>
        <id>nexus-releases</id><!-- serverId(随便命名) -->
        <username>deployment</username><!-- nexus 实际用户名称 -->
        <password>deployment123</password><!-- nexus 实际用户密码 -->
    </server>
    <server>
        <id>nexus-snapshots</id><!-- serverId(随便命名) -->
        <username>deployment</username><!-- nexus 实际用户名称 -->
        <password>deployment123</password><!-- nexus 实际用户密码 -->
    </server>
</servers>

4.2 公用库 pom.xml 配置发布地址

<packaging>jar</packaging><!-- JAR 类型 -->
<packaging>pom</packaging><!-- pom parent 类型 -->

<version>1.0.0</version><!-- Release 情景 -->
<version>1.0.0-SNAPSHOT</version><!-- Snapshot 情景 -->

<!-- 针对开发库发布者: 指定私服帐号、路径 -->
<distributionManagement>
    <repository><!-- Release 情景 -->
        <id>nexus-releases</id><!-- serverId(必须与 settings.xml 一致) -->
        <url>http://nexus:8081/nexus/content/repositories/releases/</url><!-- nexus 仓库路径 -->
    </repository>
    <snapshotRepository><!-- Snapshot 情景 -->
        <id>nexus-snapshots</id><!-- serverId(必须与 settings.xml 一致) -->
        <url>http://nexus:8081/nexus/content/repositories/snapshots/</url><!-- nexus 仓库路径 -->
    </snapshotRepository>
</distributionManagement>

4.3 向私服发布

在公用库 pom.xml 所在目录下执行 mvn deploy
会根据 packaging=pom/jar 自动确定上传后的 dependency.type。
会自动根据 version 中是否有 “-SNAPSHOT” 字样而选择 releases 或 snapshots。
进入 WEB 控制台,查看 Views / Repositories 下的 Releases/Snapshots 下的 Browse Index 即可看到上传的结果。

5 发布者维护第三方开发库

以阿里大于的开发包 sdk-java-2016-06-07.zip 为例,
解压出来两个文件 taobao-sdk-java-auto_1455552377940-20160607.jar(二进制) / taobao-sdk-java-auto_1455552377940-20160607-source.jar(源码)。

5.1 Maven 全局配置私服帐号

<!-- %MAVEN_HOME%\conf\settings.xml -->

<!-- 针对开发库发布者: 指定私服帐号 -->
<servers>
    <server>
        <id>nexus-thirdparty</id><!-- serverId(随便命名) -->
        <username>deployment</username><!-- nexus 实际用户名称 -->
        <password>deployment123</password><!-- nexus 实际用户密码 -->
    </server>
</servers>

5.2 向私服发布

## 其中的参数 repositoryId/url 相当于 5.2 一节中的 id/url。
mvn deploy:deploy-file^
    -DgroupId=com.taobao -DartifactId=dayu -Dversion=20160607 -Dpackaging=jar^
    -Dsources=taobao-sdk-java-auto_1455552377940-20160607-source.jar^
    -Dfile=taobao-sdk-java-auto_1455552377940-20160607.jar^
    -Durl=http://nexus:8081/nexus/content/repositories/thirdparty/^
    -DrepositoryId=nexus-thirdparty

5.3 仅向本地安装

mvn install:install-file^
    -DgroupId=com.taobao -DartifactId=dayu -Dversion=20160607 -Dpackaging=jar^
    -Dsources=taobao-sdk-java-auto_1455552377940-20160607-source.jar^
    -Dfile=taobao-sdk-java-auto_1455552377940-20160607.jar

6 普通开发者引用私服

6.1 全局设置方法

<!-- %MAVEN_HOME%\conf\settings.xml -->

<!-- 针对普通开发者: 引用私服 -->
<profiles>
    <profile>
        <repositories>
            <repository>
                <id>nexus-public</id>
                <url>http://nexus:8081/nexus/content/groups/public/</url>
            </repository>
        </repositories>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

6.2 当前项目设置方法

<!-- pom.xml -->

<!-- 针对普通开发者: 引用私服 -->
<repositories>
    <repository>
        <id>nexus-public</id>
        <url>http://nexus:8081/nexus/content/groups/public/</url>
    </repository>
</repositories>

7 发布兼开发者的 Maven 配置

<!-- %MAVEN_HOME%\conf\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">

    <!-- 针对开发库发布者: 指定私服帐号 -->
    <servers>
        <server>
            <id>nexus-thirdparty</id><!-- serverId -->
            <username>deployment</username>
            <password>deployment123</password>
        </server>
        <server>
            <id>nexus-releases</id><!-- serverId -->
            <username>deployment</username>
            <password>deployment123</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>deployment</username>
            <password>deployment123</password>
        </server>
    </servers>

    <!-- 针对普通开发者: 引用私服 -->
    <profiles>
        <profile>
            <repositories>
                <repository>
                    <id>nexus-public</id>
                    <url>http://nexus:8081/nexus/content/groups/public/</url>
                </repository>
            </repositories>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>
</settings>

猜你喜欢

转载自blog.csdn.net/kefengwang/article/details/81195777
今日推荐