Spring Boot uses Maven to customize a parent to simplify development

1/Scene

There are many small applications and some common features, such as relying on spring-boot, log4j2, lombok, fastjson, etc. For ease of development, you can use Maven to customize a parent to simplify development and unify the management of third-party dependent versions.(Prepare for future microservices!)

2/Build parent

2.1. Create a new Maven project, any-root.

2.2. Modify the pom.xml to add dependencies of the parent project that are essentially used by the child 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>

    <groupId>cn.ictgu</groupId>
    <artifactId>any-root</artifactId>
    <version>1.0.RELEASE</version>
    <packaging>pom</packaging>

    <properties>
        <spring-boot.version>1.5.3.RELEASE</spring-boot.version>
        <java.version>1.8</java.version>
        <lombock.version>1.16.16</lombock.version>
        <fastjson.version>1.2.32</fastjson.version>
        <commons-lang3.version>3.5</commons-lang3.version>
        <jsoup.version>1.10.2</jsoup.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Web container undertow -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

        <!-- Journal Log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

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

        <!-- Code Simplification Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombock.version}</version>
        </dependency>

        <!-- JSON fastJSON -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <!-- Generic Tool Class Apache Commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>${jsoup.version}</version>
        </dependency>

    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Attach an article: Maven Actual Warfare (VI)--- Differences between dependencies and dependency management

2.3. Dependent on log4j2, you need to add the log4j2 configuration file in the resources directory

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="PID">????</Property>
        <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
        <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
        <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />

        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

2.4 Install parent project to local warehouse Mode 1: Use IDEA

Mode 2: Use the command line, enter the directory where pom.xml is located, and execute

mvn clean install

Now that the parent project has been installed, other projects can add all the dependencies of the parent project by relying on it.

3/Quickly build sub-projects

3.1 Create a new Maven project: avatar

3.2 Modify pom.xml, inherit any-root from parent 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>

    <groupId>cn.ictgu</groupId>
    <artifactId>avatar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>cn.ictgu</groupId>
        <artifactId>any-root</artifactId>
        <version>1.0.RELEASE</version>
    </parent>

</project>

IDEA automatically imports dependencies in the parent project:

3.3 Add a startup class to test lombok/log4j2

4/Subprojects add other dependencies, for example, spring-boot-actuator:

4.1 On the basis of the avatar project, modify pom.xml to add actuator dependencies

<?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.ictgu</groupId>
    <artifactId>avatar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>cn.ictgu</groupId>
        <artifactId>any-root</artifactId>
        <version>1.0.RELEASE</version>
    </parent>

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

</project>

4.2 Add a configuration file, add application.yml under resources

management:
  security:
    enabled: false

The effect is that all endpoints under the actuator are freely accessible

4.3 Startup Project

4.4 Verify endpoints under actuator, access http://localhost:8080/beans View the dependencies of all bean s across the application:

5/Summary

Customizing public dependencies as parent projects has the following benefits:

  • Avoid erroneous operations caused by adding dependencies manually;
  • Unified management of dependent version numbers;
  • Accelerate the construction of sub-projects, and a large number of public services can be obtained from one inheritance.

Tags: Maven Spring Apache xml

Posted on Sun, 10 May 2020 00:16:32 -0700 by corporateboy