SpringBoot builds aggregation project


1. Front

1. Introduce the building method of the aggregation project, and a little bit of my personal understanding
2. I have tested the code
3. I use IDEA
4. I will introduce the creation of the project first, and finally the pom file

2. Create the parent project

Insert picture description here
Insert picture description here
Finally, the parent project structure:
Insert picture description here
no need to select a package, just finish next.

3. Create a subproject

Insert picture description here
To create this sub-project, just select the corresponding package according to your own needs and create it. Just create it as we created a normal springboot project. I won’t say more about this.

4. Project structure

This place is just a demonstration, I removed the test and other things, did not keep it, it is a very simple demo
Insert picture description here

5. Introducing the POM file

Parent project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- Maven模型的版本号 -->
    <modelVersion>4.0.0</modelVersion>
    <!-- Spring Boot的父级依赖 参考:https://segmentfault.com/a/1190000018854658 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <!-- 设定一个空值将始终从仓库中获取,不从本地路径获取 -->
        <relativePath/>
    </parent>
    <!-- 组织名 -->
    <groupId>com.test</groupId>
    <!-- 项目名称 -->
    <artifactId>test-demo</artifactId>
    <!-- 版本号 -->
    <version>0.0.1-SNAPSHOT</version>
    <!-- 项目名称 -->
    <name>test-demo</name>
    <description>Demo project for Spring Boot</description>
    <!-- 指定父工程打包方式:pom-->
    <packaging>pom</packaging>

    <!-- 指定版本 -->
    <!-- 对应的包可以通过这种方式获取版本,便于版本的统一管理 <version>${java.version}</version> -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- 指定子项目模块 -->
    <modules>
        <module>base</module>
        <module>common</module>
        <module>controller</module>
    </modules>

    <dependencies>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- jackson序列化 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <!-- 环境切换配置. 如果有几百个子工程,每个工程所需的环境都不一样,就需要单独进行配置. 右边Maven Profiles进行选择当前的环境 -->
    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <!-- 默认本地环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 线上运行环境 -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>

</project>

Subproject base 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 指定我们的父工程, test-demo -->
    <!-- 这个地方是没有<relativePath/>它的 -->
    <parent>
        <groupId>com.test</groupId>
        <artifactId>test-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.test</groupId>
    <artifactId>base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>base</name>
    <description>项目入口,核心包</description>
    <!-- 指定子项目打包方式为JAR -->
    <packaging>jar</packaging>

    <dependencies>
        <!-- 该模块依赖 controller 模块 -->
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>controller</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 该模块依赖 common 模块 -->
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    
	<build>
		<plugins>
		   <plugin>
		       <groupId>org.springframework.boot</groupId>
		       <artifactId>spring-boot-maven-plugin</artifactId>
		   </plugin>
		</plugins>
	    <!-- 生成jar包的名称 -->
	    <finalName>testDemo</finalName>
	</build>
</project>

Subproject common 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>test-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.test</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>


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

Subproject controller 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>test-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.test</groupId>
    <artifactId>controller</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>controller</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

6. Personal understanding of aggregation projects

Insert picture description here
If it's a little more complicated, this is also possible
Insert picture description here

7, miscellaneous talk

  1. I feel that building a cohesive project will be greatly affected by personal work experience, because when dealing with dependencies, you need to have a clear understanding of the overall project. If the project structure is completed, most of the code has been written. At this time, I think Modifying the relationship between the dependencies will be a little troublesome. I feel that you need to think about it before building it, or leave enough room for modification. Because my own project is made with aggregation, I now feel that the relationship between the dependencies is somewhat Not very beautiful, very messy, but it will be very troublesome if it is modified, now I will bite the bullet and continue to do it

  2. It seems that the difficulty of building a polymerization project is average. Just do it like a normal single project. There is nothing wrong with handling the dependencies.

  3. Just some of the problems I personally encountered in the aggregation project.

    这个最好放在启动的工程下面,当然放在common里面也行:
    <build>
    	<plugins>
    	   <plugin>
    	       <groupId>org.springframework.boot</groupId>
    	       <artifactId>spring-boot-maven-plugin</artifactId>
    	   </plugin>
    	</plugins>
        <!-- 生成jar包的名称 -->
        <finalName>testDemo</finalName>
    </build>
    
    2: 子项目没有 <relativePath/>
    
    3: 依赖关系处理好,就是继承关系处理好,包与包之间的引用关系
    
    4: 比如像这个包,就可以把它放到最基本的子工程里面.上面贴的两个图,我就放在common里面在,这样整个项目都可以引用到.你问我为什么不放在父工程,我没有试过,不知道
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    
  4. I will post the startup class:

    package com.test.base;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    // 告诉Spring从哪里找到bean, 定义哪些包需要被扫描
    @ComponentScan(basePackages = {
          
          "com.test"})
    public class BaseApplication {
          
          
    
        public static void main(String[] args) {
          
          
            SpringApplication.run(BaseApplication.class, args);
        }
    
    }
    
    
  5. Like database links, Redis links, they are exactly the same as usual, there is no difference

  6. The next article will write packaged docker deployment, portal: https://blog.csdn.net/qq_38637558/article/details/105154483

Guess you like

Origin blog.csdn.net/qq_38637558/article/details/105167120