Java project is divided into modules and jar packages

During this time, I received a task, which is to jar the project according to the module. The purpose is:

1. Package according to modules to facilitate upgrading

2. After the module is subcontracted, which module is modified, update the corresponding module separately. There is no need to worry about the abnormal operation of the project due to the modification of other modules after the overall update

I searched the Internet for a long time and didn't find some relevant articles, but I didn't introduce in detail the relevant articles about making jar packages according to the module. After several days of exploration, I realized the relevant functions.

First, let's talk about the following ideas:

There are basically two packaging methods we came into contact with before: 1 Package the whole project into a complete jar package and run 2 Package the project into a war package and throw it into tomcat to run.

Multi module - obviously, neither of these two methods can meet the above requirements. So I began to find another way to divide the original project into three modules (multi module core: mainly dependent packages, multi module service: various interface implementations, ulti module API: project configuration files, etc.). In this way, the project is divided into three blocks, each of which is independent of each other. The multi module service module involves the implementation of various interfaces. If necessary, this module can be further subdivided into multiple modules for dependency.

After the package is completed, the multi module service is made into a jar separately. In the subsequent development process, the local update can be completed by directly replacing this jar in the release package. Isn't it amazing? Ha ha!

The specific dependencies are shown in the following figure:

The packaged file is shown in the figure below. For subsequent updates, you only need to replace the jar packages of multi module core and multi module service:

Global pom file, mainly the modules contained in the current project that need to be declared in the < modules > tag:

<?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.multimodule</groupId>
    <artifactId>multi-module-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

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

    <modules>
        <module>multi-module-service</module>
        <module>multi-module-core</module>
        <module>multi-module-api</module>
    </modules>

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

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--Default test unit off -->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

multi-module-core:

The focus here is:

1. Add springboot related dependencies

2. Add related dependencies such as mysql

<?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">
    <parent>
        <artifactId>multi-module-parent</artifactId>
        <groupId>com.multimodule</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>multi-module-core</artifactId>

    <dependencies>

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

        <!-- add to MySQL rely on -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- add to MyBatis rely on -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

multi-module-service:

The focus here is:

1. Declare that it depends on the core module: multi module core

<?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>
        <artifactId>multi-module-parent</artifactId>
        <groupId>com.multimodule</groupId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.multimodule</groupId>
    <artifactId>multi-module-service</artifactId>
    <version>1.0.0</version>
    <name>multi-module-service</name>
    <description>multi-module-service</description>

    <properties>
        <java.version>1.8</java.version>
        <logback.version>1.2.9</logback.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.multimodule</groupId>
            <artifactId>multi-module-core</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

</project>

multi-module-api:

The focus here is:

1. The < build > < plugins > < plugin > tag needs to declare the main startup class

2. Declare dependent service module: multi module service

<?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">
    <parent>
        <artifactId>multi-module-parent</artifactId>
        <groupId>com.multimodule</groupId>
        <version>1.0.0</version>
    </parent>
    <packaging>war</packaging>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>multi-module-api</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.multimodule</groupId>
            <artifactId>multi-module-service</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- Specify this Main Class Is the only entry for the global -->
                    <mainClass>com.multi.moduleapi.MultiModuleServiceApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--You can package dependent packages into generated packages Jar In the bag-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

The above is the interdependence of configuration files. The following focuses on the startup class:

It needs to state:

1.@MapperScan("com.multi.module.mapper"): the main function of this is to declare the location of the database mapper
2.@ComponentScan(basePackages = {"com.multi.module. *"}): the main function of this declaration is to load various types

package com.multi.moduleapi;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.multi.module.mapper")
@ComponentScan(basePackages = {"com.multi.module.*"})
public class MultiModuleServiceApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MultiModuleServiceApplication.class, args);
    }

}

Interface test:

I wrote a test interface in the multi module service module:

package com.multi.module.test;

import org.springframework.web.bind.annotation.*;

/**
 * @Author: Jiang
 * @Description: Dictionary table management
 * @Date: 2022-03-04 10:29
 * @Version: 1.0
 * @Update:
 */
@RestController
@RequestMapping("/Operating")
public class MultiModuleController {

    /**
     * Test interface
     *
     * @return
     */
    @GetMapping("Test")
    public String test() {
        return "Test";
    }

}

Test results:

Packing method:

First, click mavean in the right column, then expand multi module parent and find the package button to perform packaging

Packaged files:

After that, you only need to update the multi module core and multi module service jar packages to upgrade the project:

Resource portal: Project address

Keywords: Java JavaEE jar p2p

Added by lances on Sat, 05 Mar 2022 05:41:15 +0200