WSL subsystem installs maven private server through docker

WSL subsystem installs maven private server through docker

Why use maven private server

Maven simplifies project dependency management, so that we no longer need to download the specified version of the jar package and the jar package that the jar package depends on as before. You only need to configure the maven coordinates to complete the dependency reference.

Normally, it is enough for our local development to use the maven central warehouse. When building the project, if the dependent maven coordinates do not exist, then maven will automatically download from the central warehouse (the Alibaba Cloud maven warehouse is generally used in China) and save it to The local warehouse can then be copied from the local warehouse to the project to complete the project compilation and packaging and so on.

But in actual work, some companies' office networks are all intranets, so they can't access the Maven central warehouse. At this time, you need to find a machine with external network permissions in the local area network to build a maven private server. Or although the company can access the Internet, but there are more developers, then each developer has to download the jar package from the maven central warehouse (most of them are repeated). One is that it consumes bandwidth and downloads the jar package very slowly. , Second, a large number of repeated downloads can easily lead to the company's IP (or even IP segment) being pulled into the blacklist. At this time, you should also consider setting up a maven private server.

maven private server workflow

When building the project, if there is no jar package corresponding to the maven coordinates in the local warehouse, then it will go to the maven private server to download. If the maven private server does not have a corresponding jar package, then the maven private server will go to the central warehouse to download and save, and then the local warehouse can be downloaded from maven The private server has been downloaded to the corresponding jar package.

The workflow of maven private server is shown in the figure below:

 

Install maven private server through docker

#1.下载nexus3的docker镜像
docker pull sonatype/nexus3
#2.创建挂载目录(下载的jar包等数据需要保存在本地磁盘上)
mkdir /root/nexus-data
#3.启动docker容器
docker run -d -p 38081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3
#maven私服启动需要点时间,等待1分钟即可,也可以通过docker ps -a 以及docker inspect containerId来查看容器状态
#启动成功后,即可通过http://ip:38081访问了
#由于博主是在WSL子系统下安装maven私服,这里踩到了第一个坑,通过http://127.0.0.1:38081访问一直访问不了,进行了关闭防火墙,修改hosts文件等一系列操作,都没能解决问题,然后我突然间想到之前有在WSL子系统下安装mysql数据库,安装成功之后是可以正常访问的,于是就找到自己之前写的博客,发现访问数据库用的是localhost,最终把访问地址改成http://localhost:38081一下就可以访问了

Configure maven private server username and password

Before nexus3.17, the default account password was admin/admin123, but the version installed by the blogger could not log in with the default account password. I checked the Internet and found that the password of version 3.17 was changed to random and saved in a file.

The steps to check the initial password are as follows:

#1.找到maven私服nexus3的容器id
docker ps
#2.进入maven私服nexus3容器
docker exec -it containerId /bin/bash
#3.切换到admin.password文件所在目录
cd /opt/sonatype/sonatype-work/nexus3
#4.查看maven私服默认密码
cat admin.password

After the login is successful, the system prompts to set a new password immediately, and the admin.password file becomes invalid.

Create maven private server account

Click on Users in the menu bar on the left side of the page and click the +Create local user button to create a user. The user id and password are set. Note that Status is set to Active to indicate that the user is available, and for Roles to select nx-admin, it means that the user has administrator rights. You can either download the jar package from the maven private server, or publish the jar package to the maven private server.

Create a maven private server warehouse

Click Repositories in the menu bar on the left side of the page, click the +Create repository button to create a maven private server repository, select maven2 (hosted), and then enter the repository name, select Release for Version policy, Permissive for Layout policy, and Allow redeploy for deployment policy.

Version Policy:

  • Release: official version

  • Snapshot: Snapshot version

  • Mixed: Mixed mode

Layout Policy:

  • Strict: Strict

  • Permissive: loose

Deployment Policy:

  • Allow Redeploy: Allow redeployment

  • Disable Redeploy: Disable redeployment

  • Read-Only: Read only

Create a Maven private server Alibaba Cloud proxy warehouse

Click the +Create repository button to create a maven private server warehouse, select maven2 (proxy), enter the warehouse name aliyun-repository, select Release for the Version policy, select Strict for the Layout policy, and configure the remote storage address: http://maven.aliyun.com/nexus/ content/groups/public/ , just save it.

Then click maven-public under Repositories, add the Alibaba Cloud agent to the group, and adjust its priority. After the adjustment, the interface is as follows:

Appendix 1: Maven configuration using private server (download dependency)

1. Basic instructions

(1) There are two ways to configure private server download in Maven :

  • setting.xml : This file configures the global mode

  • pom.xml : The configuration of this file is a project exclusive mode

Note : If pom.xml and setting.xml are configured at the same time, pom.xml shall prevail.

(2) When we use the maven-public warehouse address in maven , we will visit in the following order: local warehouse --> private server maven-releases --> private server maven-snapshots --> remote Alibaba Cloud maven warehouse --> remote Central warehouse .

2. Configure through the setting.xml file

(1) The configuration example of the setting.xml file is as follows. After configuration , you do not need to configure the pom.xml file, you can download the jar dependency package through the private server .

<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>

(2) If we have not built a private server and belong to personal development, then we can also directly configure and use the Alibaba Cloud maven warehouse:

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

 

3. Configure through the pom.xml file

(1) The pom.xml file configuration example is as follows. If we configure pom.xml , then pom.xml shall prevail.

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

(2) If there is no private server, we can also configure the Alibaba Cloud maven warehouse, and then configure it directly in the global setting.xml .

 

Appendix 2: Maven configuration using private server (download plug-in)

The following is a sample configuration using pom.xml :

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

 

Appendix 3: Maven configuration using private server (release dependency)

(1) First modify the setting.xml file to specify the user name and password of the releases and 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) Then add the distributionManagement node in the pom.xml file of the project :

Note : The id in the repository needs to be consistent with the server id name in the previous step .

<distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://localhost:38081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://localhost:38081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

(3) Execute the mvn deploy command to release:

(4) Log in to Nexus and check that the corresponding warehouse already has related dependency packages.

Description of the warehouse posted to:

  • If the project version number has -SNAPSHOT at the end , it will be published to the snapshots snapshot version warehouse

  • If the project version number has -RELEASES or nothing at the end , it will be released to the releases official version repository

Guess you like

Origin blog.csdn.net/l229568441/article/details/113801511