前言

系统复杂了,抽离单一职责的模块几乎是必须的;若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的。其优点无需赘述,以下将记录操作过程。

1. 多模块拆分

在.NET 中由于其统一性,实现上更自然一点。Spring Boot 通过 Maven 构建多模块工程也不麻烦,假如我的项目中包含以下几个包:

我需要将他们分别拆分成独立模块,首先要修改的是根目录下的 pom.xml,packaging 类型改为 pom,并添加 modules 节点:

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

之后新建一个个 Module,将对应的代码移植过去:

需要注意的是在启动模块的 pom.xml 中需要指定启动类:

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

统一升级版本命令:mvn versions:set -DnewVersion=0.0.1-SNAPSHOT,到此差不多完成了,引用方式与普通的依赖包一致:

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

补充

外层 pom :

<?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</groupId>
    <artifactId>et2</artifactId>
    <packaging>pom</packaging>
    <version>${own-project.version}</version>

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

    <modules>
        <module>start</module>
        <module>service</module>
        <module>client</module>
    </modules>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.youclk</groupId>
                <artifactId>start</artifactId>
                <version>${own-project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.youclk</groupId>
                <artifactId>service</artifactId>
                <version>${own-project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

内层 pom

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

    <parent>
        <groupId>com.youclk</groupId>
        <artifactId>et2</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>service</artifactId>
    <packaging>jar</packaging>
    <version>${own-project.version}</version>

</project>
<?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>

    <parent>
        <groupId>com.youclk</groupId>
        <artifactId>et2</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>start</artifactId>
    <packaging>jar</packaging>
    <version>${own-project.version}</version>

    <dependencies>
        <dependency>
            <groupId>com.youclk</groupId>
            <artifactId>service</artifactId>
        </dependency>
    </dependencies>

</project>

注意 start 模块下的包结构为根结构,不要加 .start,否则要指定需要扫描的包。

2. Nexus3 私有仓库搭建

Docker 时代一切都变得异常简单,Compose 配置如下:

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

启动过程需要一分钟左右:

需要注意的是如果你的 ssl 是在负载均衡或者其他的反向代理之上,那么必须在 HTTP 头中指定 X-Forwarded-Proto 传输协议为 HTTPS,然后,就可以愉快地玩耍了。

3. 上传与引用

3.1 上传

首先需要在 Nexus 创建私有仓库,例如我的:

其次在本地 maven 设置中添加 server 节点,默认在 ~/.m2/settings.xml

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

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>

最后执行 mvn clean deploy 便会上传至私有仓库,单独传包命令如下:

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

管理和查看:

3.1 引用

Finally,最后的最后就是怎么使用啦~ 如果需要全局引用的话需要在 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>  

不过一般不推荐这么写,settings.xml 应该尽可能保持简洁,精简配置,此处留下代理和权限认证即可,其余的可以移植到 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>

小结


我的公众号《有刻》,我们共同成长!

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部