使用Docker搭建Maven私服

一、前言

别再傻傻的使用软件包的形式去安装Nexus3了,现在是容器化的时代了。


二、安装过程

一条命令安装好Nexus3

docker run -d -p 8081:8081 --name nexus -v /usr/local/maven/apache-maven-3.6.0:/usr/local/maven sonatype/nexus3

容器启动完成后,等待Nexus3初始化好后,进入localhost:8081端口查看,出现以下的界面,证明我们安装好了。Nexus3默认的管理员账号密码是admin/admin123


三、推送我们的jar包

使用idea开发完成后,在pom文件进行如下的配置。

<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.exmaple</groupId>
    <artifactId>testNexus</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>maven-releases</name>
            <url>http://ip:port/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <name>maven-snapshots</name>
            <url>http://ip:port/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

</project>

将两个url中的ip和port改成自己的即可。

并修改maven安装目录中的conf/settings.xml,添加nexus的server。

注意:这两个repository中的id要和接下来配置的server的id一致。

    <servers>
        <server>
            <id>nexus</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>

接下来,在Terminal中输入mvn deploy,即可将jar包推送至maven-releases仓库中。

注意:此时的jar包的version为1.0-SNAPSHOT,最后就会推送至maven-snapshots。

若修改为1.0,或修改成1.0-RELEASE,则最后会推送到maven-releases仓库中。


四、从远程仓库拉取jar包

首先在pom中声明我们要拉取的包,即为刚才我们推送的包

    <dependencies>
        <dependency>
            <groupId>com.exmaple</groupId>
            <artifactId>testNexus</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

接着我们配置setting.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">

    <localRepository>本地maven仓库地址</localRepository>
    <pluginGroups>
    </pluginGroups>
    <proxies>
    </proxies>
    <servers>
        <server>
            <id>nexus</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>

    <mirrors>
        <mirror>
            <id>nexus</id>
            <name>nexus</name>
            <url>http://ip:port/repository/maven-public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>

	    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>maven-public</id>
                    <url>http://ip:port/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>jdk-1.8</id>
            <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>
        <activeProfile>nexus</activeProfile>
        <activeProfile>jdk-1.8</activeProfile>
    </activeProfiles>


</settings>

最后右键pom文件-Maven-reimport,则会拉取下来,如图所示。


五、批量推送本地Maven仓库的所有jar包

一般来说,我们在windows进行开发,Maven的本地仓库也是在windows上,而Nexus容器则运行在linux上。

我们先把本地Maven仓库里面的内容通过xftp复制到linux上的一个目录中,比如复制到repo中。

然后也是在repo中,新建脚本文件mavenImportBatch.sh文件,内容如下:

#!/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 './mavenImportBatch\.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}/{} ;

赋予可执行权限:

chmod 777 mavenImportBatch.sh

执行脚本:

./mavenImportBatch.sh  -u admin -p admin123 -r http://ip:port/repository/maven-releases/

最后可以在maven-releases中看到,我们已经将本地所有的jar包都已经批量上传至远程仓库中了。

大功告成!

发布了165 篇原创文章 · 获赞 533 · 访问量 109万+

猜你喜欢

转载自blog.csdn.net/qq_33591903/article/details/103199673