SpringBoot+Maven多模块项目(完整例子)

SpringBoot+Maven多模块项目

工程结构:
    父模块 eUdstand-parent:
        子模块 eUdstand-core
        子模块 eUdstand-site(web,唯一有启动类的模块
    关系:
        eUdstand-site 依赖 eUdstand-core (需要把eUdstand-core 包打到eUdstand-site中)


一.创建Maven多模块项目
1.创建eUdstand-parent模块
1.1创建父模块,用于管理各个模块

在这里插入图片描述
  接下来,把src整个删掉,父工程不需要,因为父工程你就当它只有一个外壳就完了
在这里插入图片描述

1.2创建子模块eUdstand-core

  创建子模块eUdstand-core:右键eUdstand-parent,选择new maven module project
在这里插入图片描述
在这里插入图片描述

1.3 创建子模块eUdstand-site

  创建子模块eUdstand-site:右键eUdstand-parent,选择new maven module project
在这里插入图片描述

二.各个模块的pom.xml文件
2.1 eUdstand-parent模块
<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.personal.eudstand</groupId>
  <artifactId>eUdstand-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
	 <dependency>
	    <groupId>commons-io</groupId>
	    <artifactId>commons-io</artifactId>
	    <version>2.6</version>
	</dependency>
  </dependencies>
  
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.2.3.RELEASE</version>
       
  </parent>

  
  <modules>
  	<module>eUdstand-core</module>
  	<module>eUdstand-site</module>
  </modules>
</project>
2.2 eUdstand-core模块
<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>
    <groupId>com.personal.eudstand</groupId>
    <artifactId>eUdstand-parent</artifactId>
    <version>1.0.0</version>
  </parent>
  <artifactId>eUdstand-core</artifactId>
</project>
2.3 eUdstand-web模块
<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>
    <groupId>com.personal.eudstand</groupId>
    <artifactId>eUdstand-parent</artifactId>
    <version>1.0.0</version>
  </parent>
    <dependencies>
     <dependency>
	       <groupId>org.springframework.boot</groupId>
	       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
      <dependency>
	       <groupId>com.personal.eudstand</groupId>
	       <artifactId>eUdstand-core</artifactId>
	       <version>1.0.0</version>
     </dependency>
  </dependencies>
  <artifactId>eUdstand-site</artifactId>
  
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.personal.eudstand.web.WebApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
     </build>


</project>

三.代码测试
3.1 eUdstand-core模块下新建CoreTest类

在这里插入图片描述

3.2.1 eUdstand-site模块下新建WebApplication类

在这里插入图片描述

3.2.2 eUdstand-site模块下新建WebTestController类

   spring-boot-starter-web 模块, 默认集成了 SpringMVC,因此只需要编写一 个 Controller,来进行简单的mvc流程验证web项目是否搭建成功。
在这里插入图片描述

四.打包运行测试
4.1Maven install

   右键eUdstand-parent项目,选择Maven install
在这里插入图片描述

4.2 打包成功后会生成一个jar包

   复制打包成功的eUdstand-site-1.0.0.jar,把它拷贝到一个目录
在这里插入图片描述

4.3 java -jar 运行jar包

在这里插入图片描述
   访问http://localhost:8080/test 地址(默认不带项目名),返回结果如下,证明最后的eUdstand-site-1.0.0.jar包把要依赖的eUdstand-core包,也打进去,并且整个springboot 项目打包成功
在这里插入图片描述

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
适合初学 springboot 的同学 --------------------------- maven配置:pom.xml --------------------------- <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> --------------------------- src/main/resources/application.yml --------------------------- spring: # 指定静态资源的路径 resources: static-locations: classpath:/static/,classpath:/views/ thymeleaf: prefix: classpath:/templates/ server: port:8080 context-path:"/" --------------------------- DemoApplication 运行 main 方法即可启动 springboot --------------------------- package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.example.*"}) //指定扫描包路径 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class HelloWorldController { // http://localhost:8080/hello 返回的是文本"Hello World" @RequestMapping("/hello") public String index() { return "Hello World"; } /** * 本地访问内容地址 :http://localhost:8080/hello3 ;返回的是文本 */ @RequestMapping("/hello3") public String helloHtml(HashMap<String, Object> map) { map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- HelloWorldController --------------------------- package com.example.controller; import java.util.HashMap; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SpringBootController { /** * 本地访问内容地址 :http://localhost:8080/hello2 ; 可访问到 * src/main/resources/views/index.html */ @RequestMapping("/hello2") public String helloHtml(HashMap<String, Object> map) { // 传参数hello到html页面 map.put("hello", "欢迎进入HTML页面"); return "/index"; } } --------------------------- src/main/resources/views/index.html --------------------------- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> aaaaaaaaaaaaaaaaaaaaaaacccccccccccccc <p th:text="${hello}">dddd</p> </body> </html> --------------------------- 直接访问静态页面 --------------------------- http://localhost:8080/index.html 可直接访问到 src/main/resources/templates/index.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值