内网搭建maven私库

部署maven私库

(1)由于安全问题,有个项目需要在内网开发(无法连接外网),因此无法下载相关的 maven 依赖,导致项目无法编译启动。之前我介绍了通过在内网搭建 Maven 私服来解决这个问题:

使用Nexus搭建Maven私服教程(附:配置并使用私服教程)
或者
无网络搭建linux私服
(2)如果觉得搭建私服麻烦的话,还有更简单的方法,就是直接将在外网下载好的 maven 依赖拷贝到内网使用。下面通过样例进行演示。

Nexus 服务的配置

1、Nexus服务启动以后,我们通过浏览器访问http://IP:端口,进去界面后点击右上角登录按钮:
在这里插入图片描述
2、首次登录后会提示密码保存在/usr/local/sonatype-work/nexus3/admin.password 文件中,我们查看服务器上这个文件内容,然后作为密码登录,然后修改密码
3、登录界面,说明如下

  1. 默认仓库说明:
    maven-central:maven 中央库,默认从 https://repo1.maven.org/maven2/ 拉取 jar
    maven-releases:私库发行版 jar,初次安装请将 Deployment policy 设置为 Allow redeploy
    maven-snapshots:私库快照(调试版本)jar
    maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 或项目 pom.xml 中使用
  2. 仓库类型说明:
    group:这是一个仓库聚合的概念,用户仓库地址选择 Group 的地址,即可访问 Group 中配置的,用于方便开发人员自己设定的仓库。maven-public 就是一个 Group 类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x 默认为 Releases、Snapshots、Central,当然你也可以自己设置。
    hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的 jar 文件
    snapshots:本地项目的快照仓库
    releases: 本地项目发布的正式版本
    proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的 Configuration 页签下 Remote Storage 属性的值即被代理的远程仓库的路径),如可配置阿里云 maven 仓库
    central:中央仓库
    在这里插入图片描述
    4、建立maven库
    在这里插入图片描述

在这里插入图片描述

更新maven私库

批量上传(推荐)

因为没有外面,所以只能在有网的情况下将工程所要的包都下载下来(一般外面直接配置maven,然后得到本地的仓库即可),然后传递到内网中。
当前将外网的本地仓库copy到nexus的服务器上,然后运行一下脚本即可批量上传jar包

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params

while getopts ":r:u:p:" opt; do
	case $opt in
		r) REPO_URL="$OPTARG"
		;;
		u) USERNAME="$OPTARG"
		;;
		p) PASSWORD="$OPTARG"
		;;
	esac
done

find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

运行如下命令进行执行,但是该脚本需要放到repository文件夹中

#权限添加
chmod a+x mavenimport.sh
#执行
./mavenimport.sh -u admin -p admin123 -r http://ip:8081/repository/maven-releases/

windows通过git导入

使用git工具,在git Bash here中运行 ./mavenimport.sh -u admin -p admin123 -r http://ip:8081/repository/maven-releases/ 能达到同样的效果

windows下通过java代码上传

当前只记录下,未验证通过

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

/**
 * 上传依赖到 Maven 私服
 *  * @author liuzenghui
 * @since
public class Deploy
    /**
     * mvn -s F:\.m2\settings.xml
     * org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file
     * -Durl=http://IP:PORT/nexus/content/repositories/thirdpart
     * -DrepositoryId=thirdpart
     * -Dfile=antlr-2.7.2.jar
     * -DpomFile=antlr-2.7.2.pom
     * -Dpackaging=jar
     * -DgeneratePom=false
     * -Dsources=./path/to/artifact-name-1.0-sources.jar
     * -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
     */
    public static final String BASE_CMD = "cmd /c mvn " +
            "-s F:\\.m2\\settings.xml " + 
            "deploy:deploy-file " +
            "-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
            "-DrepositoryId=thirdpart " +
            "-DgeneratePom=false";

    public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");

    public static final Runtime CMD = Runtime.getRuntime();

    public static final Writer ERROR;

    public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);

    static {
        Writer err = null;
        try {
            err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
        ERROR = err;
    }

    public static void main(String[] args) {
        deploy(new File("F:\\.m2\\repository").listFiles());
//        if(checkArgs(args)){
//            File file = new File(args[0]);
//            deploy(file.listFiles());
//        }
        EXECUTOR_SERVICE.shutdown();
        try {
            ERROR.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void error(String error){
        try {
            System.err.println(error);
            ERROR.write(error + "\n");
            ERROR.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean checkArgs(String[] args){
        if (args.length != 1) {
            System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
            return false;
        }
        File file = new File(args[0]);
        if (!file.exists()) {
            System.out.println(args[0] + " 目录不存在!");
            return false;
        }
        if (!file.isDirectory()) {
            System.out.println("必须指定为目录!");
            return false;
        }
        return true;
    }

    public static void deploy(File[] files) {
        if (files.length == 0) {
            //ignore
        } else if (files[0].isDirectory()) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deploy(file.listFiles());
                }
            }
        } else if (files[0].isFile()) {
            File pom = null;
            File jar = null;
            File source = null;
            File javadoc = null;
            //忽略日期快照版本,如 xxx-mySql-2.2.6-20170714.095105-1.jar
            for (File file : files) {
                String name = file.getName();
                if(DATE_PATTERN.matcher(name).find()){
                    //skip
                } else if (name.endsWith(".pom")) {
                    pom = file;
                } else if (name.endsWith("-javadoc.jar")) {
                    javadoc = file;
                } else if (name.endsWith("-sources.jar")) {
                    source = file;
                } else if (name.endsWith(".jar")) {
                    jar = file;
                }
            }
            if(pom != null){
                if(jar != null){
                    deploy(pom, jar, source, javadoc);
                } else if(packingIsPom(pom)){
                    deployPom(pom);
                }
            }
        }
    }

    public static boolean packingIsPom(File pom){
        BufferedReader reader = null;
        try {
            BufferedReader reader = 
              new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
            String line;
            while((line = reader.readLine()) != null){
                if(line.trim().indexOf("<packaging>pom</packaging>")!=-1){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
          try{reader.close();}catch(Exception e){}
        }
        return false;
    }

    public static void deployPom(final File pom) {
        EXECUTOR_SERVICE.execute(new Runnable() {
            @Override
            public void run() {
                StringBuffer cmd = new StringBuffer(BASE_CMD);
                cmd.append(" -DpomFile=").append(pom.getName());
                cmd.append(" -Dfile=").append(pom.getName());
                try {
                    final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
                    InputStream inputStream = proc.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String line;
                    StringBuffer logBuffer = new StringBuffer();
                    logBuffer.append("\n\n\n==================================\n");
                    while((line = reader.readLine()) != null){
                        if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
                            logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
                        }
                    }
                    System.out.println(logBuffer);
                    int result = proc.waitFor();
                    if(result != 0){
                        error("上传失败:" + pom.getAbsolutePath());
                    }
                } catch (IOException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }

    public static void deploy(final File pom, final File jar, final File source, final File javadoc) {
        EXECUTOR_SERVICE.execute(new Runnable() {
            @Override
            public void run() {
                StringBuffer cmd = new StringBuffer(BASE_CMD);
                cmd.append(" -DpomFile=").append(pom.getName());
                if(jar != null){
                    //当有bundle类型时,下面的配置可以保证上传的jar包后缀为.jar
                    cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
                } else {
                    cmd.append(" -Dfile=").append(pom.getName());
                }
                if(source != null){
                    cmd.append(" -Dsources=").append(source.getName());
                }
                if(javadoc != null){
                    cmd.append(" -Djavadoc=").append(javadoc.getName());
                }

                try {
                    final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
                    InputStream inputStream = proc.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String line;
                    StringBuffer logBuffer = new StringBuffer();
                    logBuffer.append("\n\n\n=======================================\n");
                    while((line = reader.readLine()) != null){
                        if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
                            logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
                        }
                    }
                    System.out.println(logBuffer);
                    int result = proc.waitFor();
                    if(result != 0){
                        error("上传失败:" + pom.getAbsolutePath());
                    }
                } catch (IOException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                }
             }
         });
     }
}

idea直接运行即可

私服使用

使用有两个方式

  • setting.xml: 该文件配置的是全局模式
  • pom.xml:该文件的配置的是项目独享模式
    当pom.xml和setting.xml同时配置,以pom.xml为主

当我们在 maven 使用 maven-public 仓库地址的时候,会按照如下顺序访问:本地仓库 --> 私服 maven-releases --> 私服 maven-snapshots --> 远程阿里云 maven 仓库 --> 远程中央仓库。

setting.xml文件配置

配置文件吐下,配置后不需要再配置pom.xml文件,即可通过私服下载依赖包

<mirrors>
    <mirror>
        <!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
        <id>maven-public</id>
        <!--镜像名称 -->
        <name>maven-public</name>
        <!--*指的是访问任何仓库都使用我们的私服-->
        <mirrorOf>*</mirrorOf>
        <!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
        <url>http://192.168.60.133:8081/repository/maven-public/</url>     
    </mirror>
</mirrors>

阿里云maven仓库

<mirror>
  <id>nexus-aliyun</id>
  <name>Nexus aliyun</name>
  <mirrorOf>*</mirrorOf>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

pom.xml文件配置

pom.xml 文件配置样例如下。如果我们配置了 pom.xml,则以 pom.xml 为准。

<repositories>
    <repository>
        <id>maven-nexus</id>
        <name>maven-nexus</name>
        <url>http://192.168.60.133:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

如果没有私服,我们同样也可以配置阿里云 maven 仓库:

<repositories>
   <repository>
      <id>maven-aliyun</id>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>true</enabled>
         <updatePolicy>always</updatePolicy>
         <checksumPolicy>fail</checksumPolicy>
      </snapshots>
   </repository>
</repositories>

Maven 配置使用私服(下载插件)

<pluginRepositories>
    <pluginRepository>
        <id>maven-nexus</id>
        <name>maven-nexus</name>
        <url>http://10.172.0.201:8081/nexus/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Maven 配置使用私服(发布依赖)

1、首先修改 setting.xml 文件,指定 releases 和 snapshots server 的用户名和密码:

<servers>
    <server>
        <id>releases</id>
        <username>admin</username>
        <password>123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>123</password>
    </server>
</servers>

2、接着在项目的 pom.xml 文件中加入 distributionManagement 节点:
注意:repository 里的 id 需要和上一步里的 server id 名称保持一致。

<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Releases</name>
        <url>http://192.168.60.133:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Snapshot</name>
        <url>http://192.168.60.133:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

3、执行 mvn deploy 命令发布:
在这里插入图片描述

资料
[1]: https://www.hangge.com/blog/cache/detail_2844.html
[2]: https://blog.csdn.net/m0_44968817/article/details/127306177
[3]: https://blog.csdn.net/AI_STUDENT_QYB/article/details/121178262
[4]: https://blog.51cto.com/isea533/5594504

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值