(二)Alian 的 Spring Cloud Parent(父工程)

一、创建parent工程

  平常我们总是看到看到我们创建一个springboot工程,总是能看到如下

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.6.0</version>
</parent>

其实这个就是一个父工程,管理了springboot依赖的版本,因为业务和架构原因我们有自己的要求,需要自己去定义创建一个Parent工程方便集中管理依赖关系和配置构建。

1.1、第一步

在这里插入图片描述

1.2、第二步

在这里插入图片描述

1.3、第三步

在这里插入图片描述
  总体来说也就是一个maven工程。

二、pom文件配置

pom.xml

<?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>cn.alian.microservice</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!--下面这三个不能少-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

        <!--spring-boot版本-->
        <spring.boot.plugins.version>2.3.12.RELEASE</spring.boot.plugins.version>
        <!--apache.maven.plugins版本-->
        <apache.maven.plugins.version>3.2.0</apache.maven.plugins.version>
        <!--私服地址-->
        <nexus.url>http://192.168.0.210:8081</nexus.url>
        <!--java源码路径-->
        <src.dir>${project.basedir}/src/main/java</src.dir>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>cn.alian.microservice</groupId>
                <artifactId>bom</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--依赖的下载地址-->
    <repositories>
        <repository>
            <id>nexus-repos</id>
            <name>Team Nexus Repository</name>
            <url>${nexus.url}/nexus/content/groups/public</url>
        </repository>
        <repository>
            <id>nexus-repos-snapshots</id>
            <name>Team Nexus Repository Snapshots</name>
            <url>${nexus.url}/nexus/content/groups/public-snapshots</url>
        </repository>
    </repositories>

    <!--插件的下载地址-->
    <pluginRepositories>
        <pluginRepository>
            <id>nexus-repos</id>
            <name>Team Nexus Repository</name>
            <url>${nexus.url}/nexus/content/groups/public</url>
        </pluginRepository>
        <pluginRepository>
            <id>nexus-repos-snapshots</id>
            <name>Team Nexus Repository Snapshots</name>
            <url>${nexus.url}/nexus/content/groups/public-snapshots</url>
        </pluginRepository>
    </pluginRepositories>

    <!--打包成库文件的上传地址-->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Nexus Release Repository</name>
            <url>${nexus.url}/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>${nexus.url}/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <profiles>
        <profile>
            <id>springboot-run</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>build.properties</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>

        <profile>
            <!-- 此处是给子工程用的记得加上参数(-Ppack),当然spring-boot:run -Ppack 可使用本机swagger信息生成相关java调用客户端 -->
            <id>pack</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <excludes>
                            <!--jar包里排除以下配置文件-->
                            <exclude>application.properties</exclude>
                            <exclude>bootstrap.properties</exclude>
                            <exclude>logback.xml</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <!--生成文档包含以下配置-->
                            <include>build.properties</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>

                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>${apache.maven.plugins.version}</version>
                        <configuration>
                            <archive>
                                <manifestEntries>
                                    <!-- 生成客户端时使用 -->
                                    <pack>true</pack>
                                </manifestEntries>
                            </archive>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>${apache.maven.plugins.version}</version>
                        <executions>
                            <execution>
                                <!--拷贝资源文件-->
                                <id>copy-resources</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <!--配置文件放到config目录-->
                                    <outputDirectory>${project.build.directory}/${project.artifactId}/config
                                    </outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>src/main/resources</directory>
                                            <includes>
                                                <!--主要包含的文件-->
                                                <include>application.properties</include>
                                                <include>bootstrap.properties</include>
                                                <include>logback.xml</include>
                                            </includes>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>${src.dir}</sourceDirectory>
        <!--pluginManagement和dependencyManagement差不多可以被子工程继承、当然你也可以在子工程覆盖-->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.plugins.version}</version>
                    <executions>
                        <execution>
                            <id>spring-boot-repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!--打包的路径-->
                        <outputDirectory>${project.build.directory}/${project.artifactId}</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <!--子工程会继承此插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.plugins.version}</version>
                <!--inherited:是否传递execution到子pom里-->
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>spring-boot-repackage</id>
                        <!--取消某个phase plugin binding-->
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

  这里对 plugins pluginManagement 做一个说明

类型作用
plugins 直接引入一个plugin,而且可以绑定到Maven相关的生命周期上
pluginManagement 一般是用来在父POM中定义,表示插件声明,Maven不会加载该插件,可以被继承,提供给子POM使用,主要是为了统一管理插件,确保所有子POM使用的插件版本保持一致

三、安装到本地或发布到私服

  具体的脚本在上一篇文章里有提供,比如我这里安装到本地。

安装到本地

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building parent 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ parent ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ parent ---
[INFO] Installing C:\workspace\study\spring-cloud-microservices\parent\pom.xml to C:\Users\admin\.m2\repository\cn\alian\microservice\parent\1.0.0-SNAPSHOT\parent-1.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.492 s
[INFO] Finished at: 2022-04-06T14:18:47+08:00
[INFO] Final Memory: 10M/243M
[INFO] ------------------------------------------------------------------------
Press any key to continue . . .

发布到私服

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building parent 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ parent ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ parent ---
[INFO] Installing C:\workspace\study\spring-cloud-microservices\parent\pom.xml to C:\Users\admin\.m2\repository\cn\alian\microservice\parent\1.0.0-SNAPSHOT\parent-1.0.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ parent ---
Downloading: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/1.0.0-SNAPSHOT/maven-metadata.xml
Uploading: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/1.0.0-SNAPSHOT/parent-1.0.0-20220323.061946-1.pom
Uploaded: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/1.0.0-SNAPSHOT/parent-1.0.0-20220323.061946-1.pom (9 KB at 79.0 KB/sec)
Downloading: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/maven-metadata.xml
Uploading: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/1.0.0-SNAPSHOT/maven-metadata.xml
Uploaded: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/1.0.0-SNAPSHOT/maven-metadata.xml (604 B at 11.3 KB/sec)
Uploading: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/maven-metadata.xml
Uploaded: http://192.168.0.210:8081/nexus/content/repositories/snapshots/cn/alian/microservice/parent/maven-metadata.xml (287 B at 4.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.083 s
[INFO] Finished at: 2022-04-06T14:19:46+08:00
[INFO] Final Memory: 13M/243M
[INFO] ------------------------------------------------------------------------
Press any key to continue . . .

   这里的parent工程,相当于一个聚合工程,对远程仓库进行配置,子工程就不用在每一个模块中都配置一遍,同时对插件进行统一管理,确保所有子POM使用的插件版本保持一致。并且工程通过profile打包的时候会把配置文件打包到一个config的目录里。需要注意的是,如果你在父工程里直接使用了依赖和插件,那么子工程就会自动继承该依赖和插件。

四、父工程依赖使用

  后面我创建所有的项目都会引入这个唯一的parent,小伙伴后面就不要问这个是什么了,这个是我自己写的发布到私服的一个父工程。你看 groupId 都是 cn.alian.microservice 就知道是我自己写的了,当然你可以命名为你个人或者公司的。所以最终我们的父工程就使用如下方式引入即可:

<parent>
    <groupId>cn.alian.microservice</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

  是不是和文章开始springboot父工程差不多?接下里,开始我们的工程模块吧,可能会有很多,但是一定会让你有收获的,至少这个规范你一定会了解。然后看其他大型项目在结构上不会一头雾水。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值