阅读 60

SpringBoot + Maven父子模块项目

项目
编辑器Eclipse,使用maven的父子模块功能新建一个项目student,分为3个模块:student-web, student-setting, student-util。其中student-web打包为war包,作为一个项目,student-setting, student-util打包为jar包,作为依赖引入到student-web。并且student-setting模块需要使用student-util提供的工具类
  • student-web: 项目启动类和一些配置文件
  • student-setting: 增删改查
  • student-util: 存放一些工具类,供student-setting等模块使用

1.创建父project

1.1 创建student项目

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

1.2 修改父亲项目的结构:删除src和target目录,只留下pom.xml文件

删除前

删除后

1.3 修改父亲项目的pom.xml文件

  • 把打包方式修改为pom: pom
  • 引入SpringBoot相关配置,把项目配置为SpringBoot项目: 添加
  • 把依赖放入: 

完整的pom.xml文件如下

<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.0modelVersion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
        <relativePath /> 
    parent>

    
    <groupId>com.demo.studentgroupId>
    <artifactId>studentartifactId>
    <name>studentname>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>pompackaging>

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

    
    <dependencyManagement>
        <dependencies>

        dependencies>
    dependencyManagement>
project>
View Code

 

2.创建student-web模块

2.1 创建student-web 模块

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

这是整个项目的启动类和配置文件,这里注意web模块的package, 定义最外层的package, 要把启动类放在最外层的package, 因为springboot扫描组件时,会扫描启动类所在的package和它的子package

 

 

2.2 查看 各层级 pom.xml文件 

student-web/pom.xml, 里面定义了

    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

 student-web 项目创建成功后,可以看到student/pom.xml文件会多了如下配置信息

<modules>
    <module>student-webmodule>
modules>

 

2.3 配置 各层级 pom.xml文件 

  • 配置student-web/pom.xml 文件:添加配置 war, 把student-web定义为项目启动模块

student-web/pom.xml, 添加

<packaging>warpackaging>

附上配置后的完整student-web/pom.xml 文件

xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>student-webartifactId>
    <name>student-webname>
    <packaging>warpackaging>

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

    <dependencies>
    dependencies>
project>
View Code

 

2.4 创建项目启动类

student-web/pom.xml 添加依赖

     <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
      dependency>

添加启动类

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);
    }
}
View Code

 

3.创建student-setting模块

3.1 创建student-setting模块

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

 3.2 查看 各层级 pom.xml文件 

  • student-setting/pom.xml 有节点
  • student/pom.xml 文件多了

看下student-setting/pom.xml

    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

student-setting 模块创建成功后,再看下student/pom.xml  。 这里可以手动调整下顺序

<modules>
     <module>student-settingmodule>
     <module>student-webmodule>
modules>

 

3.3 配置 各层级 pom.xml文件 

因为student-setting 打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖

  • 配置student-setting/pom.xml 文件: 添加配置 jar, 把student-setting 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用.
  • 在student/pom.xml添加管理依赖
  • 在student-web/pom 引入依赖

student-setting/pom.xml : 配置打包方式

<packaging>jarpackaging>

student/pom.xml: 管理依赖

    <properties>
        <student.version>0.0.1-SNAPSHOTstudent.version>
    properties>

    <dependency>
        <groupId>com.demo.studentgroupId>
        <artifactId>student-settingartifactId>
        <version>${student.version}version>
    dependency>

student-web/pom.xml: 添加依赖

        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-settingartifactId>
        dependency>

附上修改后完成的pom.xml

student/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.0modelVersion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
        <relativePath /> 
    parent>

    
    <groupId>com.demo.studentgroupId>
    <artifactId>studentartifactId>
    <name>studentname>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>pompackaging>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <student.version>0.0.1-SNAPSHOTstudent.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.demo.studentgroupId>
                <artifactId>student-settingartifactId>
                <version>${student.version}version>
            dependency>
        dependencies>
    dependencyManagement>

    <modules>
        <module>student-settingmodule>
        <module>student-webmodule>
    modules>
project>
View Code

student-web/pom.xml

xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>student-webartifactId>
    <name>student-webname>
    <packaging>warpackaging>

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

    <dependencies>
        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-settingartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

    dependencies>
project>
View Code

student-setting/pom.xml

xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>student-settingartifactId>
    <name>student-settingname>
    <packaging>jarpackaging>

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

    <dependencies>
    dependencies>
project>
View Code

 3.4 测试:

在student-setting/pom.xml添加依赖

        
            org.springframework.boot
            spring-boot-starter-web
        

student-setting项目 添加测试类

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";
    }
}
View Code

启动student-web项目,访问http://localhost:8080/api/setting/save 

4.创建student-util模块

4.1 创建student-util 模块

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

 

 

 

4.2 查看各层级pom.xml

student/pom.xml 多了如下配置

    <modules>
        <module>student-utilmodule>
        <module>student-settingmodule>
        <module>student-webmodule>
    modules>

student-util/pom.xml 有如下配置

  <parent>
    <groupId>com.demo.studentgroupId>
    <artifactId>studentartifactId>
    <version>0.0.1-SNAPSHOTversion>
  parent>

4.3 修改各层级pom.xml 配置

因为student-utils 需要打包成jar包,所以把它作为依赖包交给student父亲管理,并且在student-web 项目中引入依赖

  • 配置student-utils/pom.xml 文件: 添加配置 jar, 把student-utils 打包为一个jar包,作为依赖被student父亲项目管理,然后被其它模块引用.
  • 在student/pom.xml添加管理依赖
  • 在student-web/pom 引入依赖

student-util/pom.xml 添加

<packaging>jarpackaging>

 student/pom.xml 添加

            <dependency>
                <groupId>com.demo.studentgroupId>
                <artifactId>student-utilartifactId>
                <version>${student.version}version>
            dependency>

student-web/pom.xml 添加

        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-utilartifactId>
        dependency>

附上修改后的各级pom.xml

student/pom.xml

<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.0modelVersion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
        <relativePath /> 
    parent>

    
    <groupId>com.demo.studentgroupId>
    <artifactId>studentartifactId>
    <name>studentname>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>pompackaging>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <student.version>0.0.1-SNAPSHOTstudent.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.demo.studentgroupId>
                <artifactId>student-settingartifactId>
                <version>${student.version}version>
            dependency>
            <dependency>
                <groupId>com.demo.studentgroupId>
                <artifactId>student-utilartifactId>
                <version>${student.version}version>
            dependency>
        dependencies>
    dependencyManagement>
    <modules>
        <module>student-utilmodule>
        <module>student-settingmodule>
        <module>student-webmodule>
    modules>
project>
View Code

student-web/pom.xml

xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>student-webartifactId>
    <name>student-webname>
    <packaging>warpackaging>

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

    <dependencies>
        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-utilartifactId>
        dependency>
        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-settingartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

    dependencies>
project>
View Code

student-uitl/pom.xml

xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.demo.studentgroupId>
        <artifactId>studentartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <artifactId>student-utilartifactId>
    <name>student-utilname>
    <packaging>jarpackaging>

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

    <dependencies>

    dependencies>
project>
View Code

4.4 测试 

student-util 模块 添加一个工具类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;
    }

}
View Code

student-setting/pom.xml 添加依赖, 就可以把student-util模块引入到student-setting模块 ,这样在student-setting模块可以使用这个工具类DateUtil,

        <dependency>
            <groupId>com.demo.studentgroupId>
            <artifactId>student-utilartifactId>
        dependency>

修改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;
    }
}
View Code

测试

启动student-web项目,访问http://localhost:8080/api/setting/save 

 

5.编译运行 

student/pom.xml 配置plugin

    <build>
        <pluginManagement>
            <plugins>

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

student-web/pom.xml 配置plugin

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

        plugins>
    build>

在student/pom.xml同级目录打开cmd,执行命令mvn clean package -Dmaven.test.skip=true,最后的编译过程如下,打好的war包在  student\student-web\target\ 文件夹内

在 student\student-web\target\  cmd 执行 java -jar student-web-0.0.1-SNAPSHOT.war

 

 访问 http://localhost:8080/api/setting/save

原文:https://www.cnblogs.com/dreamstar99/p/15292884.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐