1.为什么使用nexus

Without private server, all the components we needed would have to be downloaded locally through Maven’s central repository and third-party Maven repository, and the repeated downloading of components from Maven repository by all members of a team would undoubtedly increase the load of the repository and waste the bandwidth of the external network. If the Internet speed is slow, it would also affect the progress of the project. In many cases, project development takes place on the Intranet. What if you can’t connect to the Maven repository? How can developed common artifacts be used by other projects? At this time we have to set up our own maven private servers in your team, this saves network bandwidth will also accelerate the process of project building, premise condition is, of course, your private servers have projects in all the necessary components, the second a little big companies have their own internal architecture components, these jar package will not be open, The Nexus is also a good choice

Nexus official website

2. The nexus of installation

2.1 Installation Environment

Software environment Introduction to the version RAM
centos Linux Operating system 7.8 8G
docker Docker hypervisor 20.10.6
docker-compose Docker container choreography 1.29.1

2.2 Installing the Nexus Application

  1. New Nexus Directory
mkdir nexus
cd nexus
touch docker-compose.yaml
vim docker-compose.yaml
Copy the code
  1. Configure the docker-comemage. yaml file
version: "3.7"
services:
  nexus:
    image: sonatype/nexus3
    container_name: nexus
    environment:
      - "INSTALL4J_ADD_VM_PARAMS=-Xms1g -Xmx1g -XX:MaxDirectMemorySize=1g"
    ports:
      - 8764: 8081
    volumes:
      - ./data:/nexus-data
    restart: always
Copy the code

Matters needing attention

  • INSTALL4J_ADD_VM_PARAMS: Sets the memory occupied by nexus. Default is 2g
  1. Run the docker-compose file
docker-compose up -d
Copy the code

If done is displayed, the installation is complete

2.3 connect the nexus

Visit HTTP //:127.0.0.1:8764 and the Nexus visualization page appears

The default user name is admin. The password is admin

2.4 Setting maven Private Library Connections

We set up the domestic Ali cloud Maven source, through nexus proxy Ali cloud Maven source

  1. Set up ali Cloud warehouse agent
  • To create a warehouse, select Maven2 (Proxy). The page for adding a warehouse is displayed

  • Set the name, proxy source address, and version type

Ali cloud maven warehouse address: maven.aliyun.com/nexus/conte…

  • After the creation, see the figure

  1. Follow the steps above to set up the Maven repository agent source
  2. After setting up maven repository and central repository source, continue to set up default Maven-public repository

  1. This concludes the basic Nexus repository setup

3. Use the Nexus repository

3.1 Modifying settings. XML file in maven directory

As is shown in

  1. Go to the localRepository TAB and set the Maven repository directory that you want to store locally, or not by default

2. To findserversNode label: Set the account and password of Nexus. This one needs to be set, because Maven project needs to read the account and password of server node for nexus data connection

<servers>
    <server>
      <id>maven-releases</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>maven-snapshots</id>
      <username>admin</username>
      <password>123456</password>
    </server>
</servers>
Copy the code
  1. Find the mirrors TAB and set the Maven mirror source. Here is the address of the Maven repository mirror

<mirrors>
  <mirror>
    <id>nexus-public</id>
    <mirrorOf>central</mirrorOf>
    <name>central repository</name>
    <url>http://27.0.0.1:8764/repository/maven-public/</url>
  </mirror>
</mirrors>
Copy the code

Set the basis to seemaven-publicWarehouse connection address:

3.2 Setting the Maven project pom. XML file

3.2.1 PoM structure of internal Upload Components

  1. Add the distributionManagement node
<distributionManagement>
    <repository>
        <id>maven-releases</id>
        <url>http://127.0.0.1:8764/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id>
        <url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
Copy the code

The ID tag corresponds to the ID field in the previous server tag

2. Add and upload the Nexus configuration

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>/ * * *</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>The < target1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <! -- Resources plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                    <encoding>UTF-8</encoding>
                    <! Certificate file with suffix pem, PFX
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                        <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                        <nonFilteredFileExtension>key</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                        <nonFilteredFileExtension>db</nonFilteredFileExtension>
                        <nonFilteredFileExtension>txt</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
            <! -- Config build source package -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <! Build plugins -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
        <! -- Resource Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
        </plugin>
        <! -- Config build source package -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <! -- packaged -p arguments -->
        <id>release</id>
        <build>
            <plugins>
                <! -- Source -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <! -- Javadoc -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        <distributionManagement>
            <repository>
                <id>maven-releases</id>
                <url>http://127.0.0.1:8764/repository/maven-releases/</url>
            </repository>
            <snapshotRepository>
                <id>maven-snapshots</id>
                <url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
            </snapshotRepository>
        </distributionManagement>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>
Copy the code

SnapshotRepository and Repository set the repository address you need to upload:

  1. To complete the configuration of uploading the JAR package to maven’s nexus repository, click Deploy

3.2.2 Maven POM Settings using Nexus

New repository node, which connects to the Nexus specific repository

<repositories>
    <repository>
        <id>maven-releases</id>
        <url>http://127.0.0.1:8764/repository/maven-releases/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>maven-snapshots</id>
        <url>http://127.0.0.1:8764/repository/maven-snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
Copy the code

The Maven repository is ready for normal use

4 summarizes

Maven private library in the daily development occupies a large role, I hope this article can give small white people less step pit, better improve work efficiency, happy touch fish ~