SpringBoot + Maven parent-child module project

Keywords: Maven Spring Boot

project

Eclipse editor, create a new project student using maven's parent-child module function, which is divided into three modules: student web,   student-setting, student-util. Student web is packaged as a war package. As a project, student setting and student util are packaged as jar packages and introduced into student web as dependencies. And the student setting module needs to use the tool class provided by student util

  • student-web:   Project startup class and some configuration files
  • student-setting:   Add, delete, modify and query
  • student-util:   Store some tool classes for student setting and other modules

1. Create parent project

1.1 create student project

File -> New -> Maven Project -> maven-archetype-quickstart

 

one point two   Modify the structure of the parent project: delete the src and target directories, leaving only the pom.xml file

Before deletion

After deletion

 

1.3 modify pom.xml file of parent project

  • Change the packaging method to POM:  < packaging>pom</packaging>
  • Introduce SpringBoot related configurations and configure the project as a SpringBoot project: add < parent > < / parent >
  • Put dependencies into:  < dependencyManagement></dependencyManagement>

The complete pom.xml file is as follows

<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 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <!-- project -->
    <groupId>com.demo.student</groupId>
    <artifactId>student</artifactId>
    <name>student</name>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- properties -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- dependencyManagement -->
    <dependencyManagement>
        <dependencies>

        </dependencies>
    </dependencyManagement>
</project>

2. Create student web module

2.1 create student web module

File -> New -> Maven Module -> maven-archetype-quickstart

This is the startup class and configuration file of the whole project. Here, pay attention to the package of the web module, define the outermost package, and put the startup class in the outermost package, because when springboot scans components, it will scan the package where the startup class is located and its sub packages

 

 

2.2 view pom.xml files at all levels  

student-web/pom.xml, which defines < parent > < / parent >

    <parent>
        <groupId>com.demo.student</groupId>
        <artifactId>student</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

  After the student web project is created successfully, you can see that the following configuration information will be added to the student/pom.xml file

<modules>
    <module>student-web</module>
</modules>

two point three   Configure pom.xml files at all levels  

  • Configure student-web/pom.xml file: add configuration  < Packaging > war < / packaging >, student web is defined as the project startup module

student-web/pom.xml, adding

<packaging>war</packaging>

2.4 create project startup class

student-web/pom.xml add dependency

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

Add startup class

package com.demo.student;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class StudentApplication {

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

3. Create student setting module

3.1 create student setting module

File -> New -> Maven Module -> maven-archetype-quickstart

  three point two   View pom.xml files at all levels  

  • Student setting / pom.xml has a < parent > node
  • student/pom.xml file is more < module >

Take a look at student-setting/pom.xml

    <parent>
        <groupId>com.demo.student</groupId>
        <artifactId>student</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

  After the student setting module is created successfully, look at student/pom.xml   .   Here you can manually adjust the next sequence

<modules>
     <module>student-setting</module>
     <module>student-web</module>
</modules>

3.3 configure pom.xml files at all levels  

Because student setting is packaged into a jar package, it is managed by the student parent as a dependency package, and dependency is introduced into the student web project

  • To configure the student setting / pom.xml file:   Add the configuration < packaging > jar < / packaging >, package the student setting into a jar package, which is managed by the student's parent project as a dependency, and then referenced by other modules
  • Add management dependencies in student/pom.xml
  • Introducing dependencies in student web / POM

Student setting / pom.xml: configure the packaging method

<packaging>jar</packaging>

student/pom.xml: manage dependencies

    <properties>
        <student.version>0.0.1-SNAPSHOT</student.version>
    </properties>

    <dependency>
        <groupId>com.demo.student</groupId>
        <artifactId>student-setting</artifactId>
        <version>${student.version}</version>
    </dependency>

student-web/pom.xml: add dependency

        <dependency>
            <groupId>com.demo.student</groupId>
            <artifactId>student-setting</artifactId>
        </dependency>

  3.4 test:

Add dependencies in student setting / pom.xml

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

Add a test class to the student setting project

package com.demo.student.setting;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/setting")
public class StudentSettingController {
    @GetMapping("/save")
    public String save() {
        System.out.println("This is student-setting save function");
        return "This is student-setting save function";
    }
}

Start the student web project and visit http://localhost:8080/api/setting/save  

4. Create student util module

4.1 create student util module

File -> New -> Maven Module -> maven-archetype-quickstart

 

4.2 view pom.xml of each level

student/pom.xml has the following configurations

    <modules>
        <module>student-util</module>
        <module>student-setting</module>
        <module>student-web</module>
    </modules>

Student util / pom.xml has the following < parent > configuration

  <parent>
    <groupId>com.demo.student</groupId>
    <artifactId>student</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

4.3 modify pom.xml configuration at all levels

Because student utils needs to be packaged into a jar package, it is managed by the student parent as a dependency package, and dependency is introduced into the student web project

  • To configure the student utils / pom.xml file:   Add configuration < packaging > jar < / packaging >, package student utils into a jar package, which is managed by the student's parent project as a dependency, and then referenced by other modules
  • Add management dependencies in student/pom.xml
  • Introducing dependencies in student web / POM

Student util / pom.xml add

<packaging>jar</packaging>

  student/pom.xml add

            <dependency>
                <groupId>com.demo.student</groupId>
                <artifactId>student-util</artifactId>
                <version>${student.version}</version>
            </dependency>

student-web/pom.xml add

        <dependency>
            <groupId>com.demo.student</groupId>
            <artifactId>student-util</artifactId>
        </dependency>

four point four   test  

Student util module adds a tool class DateUtil

package com.demo.student.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String yesterday() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        String yesterday = simpleDateFormat.format(calendar.getTime());
        return yesterday;
    }

    public static String today() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String yesterday = simpleDateFormat.format(new Date());
        return yesterday;
    }

}

By adding dependencies to student setting / pom.xml, you can introduce the student util module into the student setting module, so that the tool class DateUtil can be used in the student setting module,

        <dependency>
            <groupId>com.demo.student</groupId>
            <artifactId>student-util</artifactId>
        </dependency>

Modify StudentSettingController

package com.demo.student.setting;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.student.util.DateUtil;

@RestController
@RequestMapping("/api/setting")
public class StudentSettingController {
    @GetMapping("/save")
    public String save() {
        System.out.println("This is student-setting save function");
        String today = DateUtil.today();
        String yesterday = DateUtil.yesterday();
        return "This is student-setting save function today is " + today + " yesterday is " + yesterday;
    }
}

test

Start the student web project and visit http://localhost:8080/api/setting/save  

 

5. Compile and run  

student/pom.xml configuration plugin

    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

student-web/pom.xml configuration plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

Open cmd in the same level directory of student/pom.xml and execute the command mvn clean package -Dmaven.test.skip=true. The final compilation process is as follows. The war package is   Student \ student web \ target \ folder

stay   student\student-web\target\   cmd execution   java -jar student-web-0.0.1-SNAPSHOT.war

  visit http://localhost:8080/api/setting/save

Posted by FaT3oYCG on Mon, 20 Sep 2021 10:36:11 -0700