Spring Boot multi-module and Maven private repository

foreword

The system is complex, and it is almost necessary to extract modules with a single responsibility; if you need to maintain multiple projects, it is almost necessary to extract public packages and upload private warehouse management. Its advantages need not be repeated, and the operation process will be recorded below.

1. Multi-module split

The implementation is a bit more natural in .NET due to its uniformity. It is not troublesome for Spring Boot to build multi-module projects through Maven, if my project contains the following packages:

I need to split them into independent modules. The first thing to modify is the pom.xml in the root directory, change the packaging type to pom, and add the modules node:

<?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.youclk.multi-package</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>api</module>
        <module>service</module>
        <module>dao</module>
    </modules>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <lombok>1.16.20</lombok>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok}</version>
        </dependency>
    </dependencies>

</project>

Then create a new Module and transplant the corresponding code:

It should be noted that the startup class needs to be specified in the pom.xml of the startup module:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.youclk.multipackage.api.MultiApplication</mainClass>
                <layout>ZIP</layout>
            </configuration>
        </plugin>
    </plugins>
</build>

Unified upgrade version command: mvn versions:set -DnewVersion=0.0.1-SNAPSHOT, this is almost complete, the reference method is the same as the ordinary dependency package:

<dependency>
    <groupId>com.youclk.multi-package</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2. Nexus3 private warehouse construction

In the Docker era, everything became very simple, and the Compose configuration is as follows:

version: '3.5'

services:

  nexus:
    image: sonatype/nexus3:3.10.0
    networks:
      - proxy
      - youclk
    volumes:
      - /mnt/nas/db/nexus-data:/nexus-data
    deploy:
      mode: replicated
      labels:
        - com.df.notify=true
        - com.df.port=8081
        - com.df.serviceDomain=nexus.youclk.com
      restart_policy:
        condition: any
        max_attempts: 3
      update_config:
        delay: 5s
        order: start-first
      resources:
        limits:
          cpus: '0.50'
          memory: 1g

networks:
  proxy:
    external: true
  youclk:
    external: true

The boot process takes a minute or so:

It should be noted that if your ssl is on a load balancer or other reverse proxy, you must specify the X-Forwarded-Proto transport protocol as HTTPS in the HTTP header, and then you can have fun.

3. Upload and Citation

3.1 Upload

First, you need to create a private repository in Nexus, such as mine:

Secondly, add the server node in the local maven settings, the idea is generally in ~/.m2/settings.xml:

<servers>
  <server>   
    <id>youclk</id>   
    <username>admin</username>
    <password>youclk</password>   
  </server>
</servers>

Add the upload address to pom.xml:

<distributionManagement>
    <repository>
        <id>nexus</id>
        <name>Releases Repository</name>
        <url>https://nexus.youclk.com/repository/youclk-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus</id>
        <name>Snapshot Repository</name>
        <url>https://nexus.youclk.com/repository/youclk-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

The final execution mvn clean deploywill upload to the private warehouse, and the separate package command is as follows:

mvn deploy:deploy-file -DgroupId=com.youclk -DartifactId=utils -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=target/utils-0.0.1-SNAPSHOT.jar -Durl=https://nexus.youclk.com/repository/youclk/ -DrepositoryId=youclk

Manage and view:

3.1 References

Finally, the last thing is how to use it~ If you need a global reference, you need to add and activate the repository in settings.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<settings>
    <mirrors>  
        <mirror>
            <id>aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>central mirror</name>
            <url>http://maven.aliyun.com/mvn/repository</url>
        </mirror>
        <mirror>
            <id>nexus</id>
            <mirrorOf>maven-public</mirrorOf>
            <name>private mirror</name>
            <url>http://local-nexus.youclk.com/repository/maven-public/</url>
        </mirror>
    </mirrors> 
    
    <servers>
        <server>   
            <id>nexus</id>   
            <username>admin</username>
            <password>youclk</password>   
        </server>
    </servers>

     <profiles>  
        <profile>  
            <id>nexus</id>   
            <repositories>  
                <repository>  
                    <id>maven</id>  
                    <name>local private nexus</name>  
                    <url>http://local-nexus.youclk.com/repository/maven-public/</url>  
                    <releases>  
                        <enabled>true</enabled>  
                    </releases>  
                    <snapshots>  
                        <enabled>true</enabled>  
                    </snapshots>  
                </repository>  
            </repositories>  
                
            <pluginRepositories>  
                <pluginRepository>  
                    <id>maven</id>  
                    <name>local private nexus</name>  
                    <url>http://local-nexus.youclk.com/repository/maven-public/</url>  
                    <releases>  
                        <enabled>true</enabled>  
                    </releases>  
                    <snapshots>  
                        <enabled>true</enabled>  
                    </snapshots>  
                </pluginRepository>  
            </pluginRepositories>  
        </profile>
    </profiles>  

    <activeProfiles>  
        <activeProfile>nexus</activeProfile>  
    </activeProfiles>   
</settings>  

However, this is generally not recommended. The settings.xml should be kept as concise as possible, and the configuration should be simplified. The proxy and authority authentication can be left here, and the rest can be ported to pom.xml:

<repositories>
    <repository>
        <id>aliyun</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository>
    <repository>
        <id>nexus</id>
        <url>http://local-nexus.youclk.com/repository/maven-public/</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>
            http://maven.aliyun.com/nexus/content/groups/public/
        </url>
    </pluginRepository>
    <pluginRepository>
        <id>maven-public</id>
        <url>http://local-nexus.youclk.com/repository/maven-public/</url>
    </pluginRepository>
</pluginRepositories>

summary

I recently opened a subscription account, and I have already seen it here. Let’s pay attention to it by the way~ There is a moment, we will grow together, I wish Jinan :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341376&siteId=291194637