使用nexus3搭建maven私有仓库

先说点理论:

仓库分类:

  1. hosted(宿主):宿主仓库主要用于存放项目部署的构件、或者第三方构件用于提供下载。
  2. proxy(代理):代理仓库就是对远程仓库的一种代理,从远程仓库下载构件和插件然后缓存在Nexus仓库中
  3. group(仓库组):仓库的一种组合策略,并不存在实在意义的依赖,只是作为一种中转站的作用存在。

 

Nexus内置仓库类型

  1. maven-central:代理中央仓库、策略为Release、只会下载和缓存中央仓库中的发布版本构件。
  2. maven-releases:策略为Release的宿主仓库、用来部署组织内部的发布版本内容。
  3. maven-snapshots:策略为Snapshot的宿主仓库、用来部署组织内部的快照版本内容。
  4. maven-public:该仓库将上述所有策略为Release的仓库聚合并通过一致的地址提供服务。
  5. nuget-hosted:用来部署nuget构件的宿主仓库
  6. nuget.org-proxy:代理nuget远程仓库,下载和缓冲nuget构件。
  7. nuget-group:该仓库组将nuget-hosted与nuget.org-proxy仓库聚合并通过一致的地址提供服务。
  8. maven-public:该仓库组将maven-central,maven-releases与maven-snapshots仓库聚合并通过一致的地址提供服务。

 

1、下载nexus3.10

官网链接:https://www.sonatype.com/nexus-repository-oss

 

第一个是mac版本, 第二个是windows版本, 第三个是unix版本.

可以选择windows版和linux版,两者区别不大,以windows为例。

 

2、配置启动

下载解压文件后:配置bin目录下nexus.vmoptions文件,适当调整内存参数,防止占用内存太大

就改这三项

 

etc目录下nexus-default.properties文件可配置默认端口和host及访问根目录。

linux:bin目录下执行sh nexus start启动服务,sh nexus stop停止服务

windows: bin目录下,执行 nexus.exe  /run就可以启动了

地址栏访问 localhost:8081,每次启动服务需要等待一会才可以打开

3、用户登录

默认登录是游客身份:anonymous,可以查看仓库并下载依赖,但不能配置nexus

使用默认管理员身份登录,帐号:admin,密码:密码:D:\mavenservice\sonatype-work\nexus3\admin.password  中

第一次进去需要修改密码. 这个文件会自动再删除的.

4.创建一个仓库

 

选择maven2(hosted)

 version policy  这边决定创建的苍鹭存放是发布版本还是快照版本的建构, 当然可以选择混合

Deployment policy  这边决定发布策略, 选择可以重新发布构建策略

这样就完成了

5.发布jar包到仓库

新建一个common-demo项目,发布jar包出去

修改本地的maven 的 配置conf 文件下 的settings.xml 文件:

添加自己的服务器. 其实就是配置登录的账号密码, 我自己手动修改了密码为123

<servers>
    
	
	<!--  自己的服务器 -->
		<server>
		<id>nexus-release</id>
		<username>admin</username>
		<password>123</password>
	  </server>
	  <server>
		<id>nexus-snapshots</id>
		<username>admin</username>
		<password>123</password>
  </server>
	
  </servers>

 

Pom配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.onyx</groupId>
    <artifactId>common-demo</artifactId>
    <version>1.0.0.release</version>
    <packaging>jar</packaging>

    <distributionManagement>
        <repository>
            <id>nexus-release</id>
            <url>http://127.0.0.1:8081/repository/demo3/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>http://127.0.0.1:8081/repository/demo3/</url>
        </snapshotRepository>
        <!-- 这边url就是仓库的路径 -->
    </distributionManagement>

    <build>
        <plugins>
            <!-- 打jar包插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- 打包源码插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

一个java工具类

package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;


/**
 * @author zk
 * @Description: CRC32 工具类, 从一个源,获取crc32编码
 * @date 2019年10月15日14:10:06
 */
public class CRC32Util {

    private static final int BUFFER_SIZE = 512;

    private static final Long DEFAULT_CODE = 0L;

    private CRC32Util() {
    }


    /**
     * 编码
     *
     * @param data
     * @return
     */
    public static long encode(String data) {
        if (data == null || data.length() == 0) {
            return DEFAULT_CODE;
        }
        return encode(data.getBytes());
    }


    /**
     * 编码
     *
     * @param data
     * @return
     */
    public static long encode(byte[] data) {
        if (data == null || data.length == 0) {
            return DEFAULT_CODE;
        }
        CRC32 crc32 = new CRC32();
        crc32.update(data, 0, data.length);
        return crc32.getValue();
    }


    /**
     * 编码
     */
    public static long encode(InputStream data) {
        if (data == null) {
            return DEFAULT_CODE;
        }
        try {
            byte[] buffer = new byte[BUFFER_SIZE];
            int read = data.read(buffer, 0, BUFFER_SIZE);
            CRC32 crc32 = new CRC32();
            while (read > -1) {
                crc32.update(buffer, 0, read);
                read = data.read(buffer, 0, BUFFER_SIZE);
            }
            return crc32.getValue();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

执行命名  mvn  clean  package  deploy

6.引用jar包

再次新建一个maven-demo的项目,引用jar包

其最后的pom文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.onyx</groupId>
    <artifactId>maven-demo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <repositories>
        <repository>
            <id>nexus-release</id>
            <name>nexus-release</name>
            <url>http://127.0.0.1:8081/repository/demo3/</url>
            <!--<layout>default</layout>-->
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.onyx</groupId>
            <artifactId>common-demo</artifactId>
            <version>1.0.0.release</version>
        </dependency>
    </dependencies>


</project>

 

测试类:

package com.onyx;


import util.CRC32Util;

/**
 * @author zk
 * @Description:
 * @date 2019-10-21 15:33
 */
public class Test {

    public static void main(String[] args) {
        CRC32Util.encode("123");
    }
}

 

当新建两个仓库一个是release的版本发布, 另外一个作为snapshot的版本发布的时候的配置

只需要修改common-demo的仓库

<distributionManagement>
    <repository>
        <id>nexus-release</id>
        <url>http://127.0.0.1:8081/repository/demo3/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://127.0.0.1:8081/repository/demo4/</url>
    </snapshotRepository>
    <!-- 这边url就是仓库的路径 -->
</distributionManagement>

 

注意是两个不同的url

在maven-demo的项目引用中, 修改为:

分别配置相关的两个配置, 千万别写错了

<repositories>
        <repository>
            <id>nexus-release</id>
            <name>nexus-release</name>
            <url>http://127.0.0.1:8081/repository/demo3/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>nexus-snapshots</id>
            <name>nexus-snapshots</name>
            <url>http://127.0.0.1:8081/repository/demo4/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

就可以了. 把 release 和 snapshot 的仓库分开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值