构建聚合springboot项目

IDEA创建springboot聚合项目

原创置顶 kongkxy 发布于2018-08-21 17:34:28 阅读数 8115  收藏

展开

使用idea创建springboot很简单,但是基于springboot的聚合工程很多小伙伴都是不会的,在这里我分享一下我自己搭建springboot聚合工程的经验

一.创建聚合父工程

1.首先使用 Spring Initializr 来快速创建好一个Maven工程。然后删除无关的文件,保留pom.xml 文件 ,将打包方式改为pom如下。

2.创建demo项目的子模块,在项目上右键单击,选择:new -> Module 分别为springboot-web,

springboot-service,springboot-dao,springboot-entity   4个子项目   创建完成后目录如下

3.删除子模块中 src/main/java、src/main/java下的所有文件,只保留web子模块的SpringBoot的Application主启动类

4.更改父工程的pom.xml   文件为如下  注意<modules>标签是否指定了子模块

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>demo</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>pom</packaging>

  10.  
  11. <name>demo</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <modules>

  15. <module>springboot-web</module>

  16. <module>springboot-service</module>

  17. <module>springboot-dao</module>

  18. <module>springboot-entity</module>

  19. </modules>

  20.  
  21. <parent>

  22. <groupId>org.springframework.boot</groupId>

  23. <artifactId>spring-boot-starter-parent</artifactId>

  24. <version>2.0.4.RELEASE</version>

  25. <relativePath/> <!-- lookup parent from repository -->

  26. </parent>

  27.  
  28. <properties>

  29. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  30. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  31. <java.version>1.8</java.version>

  32. </properties>

  33.  
  34. <dependencies>

  35. <dependency>

  36. <groupId>org.springframework.boot</groupId>

  37. <artifactId>spring-boot-starter</artifactId>

  38. </dependency>

  39.  
  40. <dependency>

  41. <groupId>org.springframework.boot</groupId>

  42. <artifactId>spring-boot-starter-test</artifactId>

  43. <scope>test</scope>

  44. </dependency>

  45. </dependencies>

  46.  
  47. <!--指定使用maven打包-->

  48. <build>

  49. <plugins>

  50. <plugin>

  51. <groupId>org.apache.maven.plugins</groupId>

  52. <artifactId>maven-compiler-plugin</artifactId>

  53. <version>3.1</version>

  54. <configuration>

  55. <source>${java.version}</source>

  56. <target>${java.version}</target>

  57. </configuration>

  58. </plugin>

  59.  
  60. <plugin>

  61. <groupId>org.apache.maven.plugins</groupId>

  62. <artifactId>maven-surefire-plugin</artifactId>

  63. <version>2.19.1</version>

  64. <configuration>

  65. <skipTests>true</skipTests> <!--默认关掉单元测试 -->

  66. </configuration>

  67. </plugin>

  68. </plugins>

  69. </build>

  70.  
  71.  
  72. </project>

5.web子模块pom.xml(依赖service、dao、entity子模块)

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>springboot-web</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>springboot-web</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>org.springframework.boot</groupId>

  16. <artifactId>spring-boot-starter-parent</artifactId>

  17. <version>2.0.4.RELEASE</version>

  18. <relativePath/> <!-- lookup parent from repository -->

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. </properties>

  26.  
  27. <dependencies>

  28. <dependency>

  29. <groupId>com.example</groupId>

  30. <artifactId>springboot-service</artifactId>

  31. <version>0.0.1-SNAPSHOT</version>

  32. </dependency>

  33. <dependency>

  34. <groupId>com.example</groupId>

  35. <artifactId>springboot-dao</artifactId>

  36. <version>0.0.1-SNAPSHOT</version>

  37. </dependency>

  38. <dependency>

  39. <groupId>com.example</groupId>

  40. <artifactId>springboot-entity</artifactId>

  41. <version>0.0.1-SNAPSHOT</version>

  42. </dependency>

  43.  
  44. <dependency>

  45. <groupId>org.springframework.boot</groupId>

  46. <artifactId>spring-boot-starter</artifactId>

  47. </dependency>

  48.  
  49. <dependency>

  50. <groupId>org.springframework.boot</groupId>

  51. <artifactId>spring-boot-starter-test</artifactId>

  52. <scope>test</scope>

  53. </dependency>

  54. </dependencies>

  55.  
  56.  
  57.  
  58. </project>

6.service子模块pom.xml(依赖 dao 、entity子模块)

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>springboot-service</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>springboot-service</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>com.example</groupId>

  16. <artifactId>demo</artifactId>

  17. <version>0.0.1-SNAPSHOT</version>

  18. <relativePath>../pom.xml</relativePath>

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. </properties>

  26.  
  27. <dependencies>

  28. <dependency>

  29. <groupId>com.example</groupId>

  30. <artifactId>springboot-entity</artifactId>

  31. <version>0.0.1-SNAPSHOT</version>

  32. </dependency>

  33. <dependency>

  34. <groupId>com.example</groupId>

  35. <artifactId>springboot-dao</artifactId>

  36. <version>0.0.1-SNAPSHOT</version>

  37. </dependency>

  38. <dependency>

  39. <groupId>org.springframework.boot</groupId>

  40. <artifactId>spring-boot-starter</artifactId>

  41. </dependency>

  42.  
  43. <dependency>

  44. <groupId>org.springframework.boot</groupId>

  45. <artifactId>spring-boot-starter-test</artifactId>

  46. <scope>test</scope>

  47. </dependency>

  48. </dependencies>

  49.  
  50.  
  51.  
  52.  
  53. </project>

7.dao子模块pom.xml (依赖entity子模块)

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>springboot-dao</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>springboot-dao</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>com.example</groupId>

  16. <artifactId>demo</artifactId>

  17. <version>0.0.1-SNAPSHOT</version>

  18. <relativePath>../pom.xml</relativePath>

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. </properties>

  26.  
  27. <dependencies>

  28. <dependency>

  29. <groupId>com.example</groupId>

  30. <artifactId>springboot-entity</artifactId>

  31. <version>0.0.1-SNAPSHOT</version>

  32. </dependency>

  33.  
  34. <dependency>

  35. <groupId>org.springframework.boot</groupId>

  36. <artifactId>spring-boot-starter</artifactId>

  37. </dependency>

  38.  
  39. <dependency>

  40. <groupId>org.springframework.boot</groupId>

  41. <artifactId>spring-boot-starter-test</artifactId>

  42. <scope>test</scope>

  43. </dependency>

  44. </dependencies>

  45.  
  46.  
  47.  
  48. </project>

8.entity子模块

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>springboot-entity</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>springboot-entity</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>com.example</groupId>

  16. <artifactId>demo</artifactId>

  17. <version>0.0.1-SNAPSHOT</version>

  18. <relativePath>../pom.xml</relativePath>

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. </properties>

  26.  
  27. <dependencies>

  28. <dependency>

  29. <groupId>org.springframework.boot</groupId>

  30. <artifactId>spring-boot-starter</artifactId>

  31. </dependency>

  32.  
  33. <dependency>

  34. <groupId>org.springframework.boot</groupId>

  35. <artifactId>spring-boot-starter-test</artifactId>

  36. <scope>test</scope>

  37. </dependency>

  38. </dependencies>

  39.  
  40.  
  41.  
  42.  
  43. </project>

9.注意 打包方式已经更改 所以 

 

 

图中红色框圈中的部分一定要删除 或者复制我上面的pom 

10.打包方式如下图

 

打包成功如下图

11.web子模块的Application启动类:

 
  1. package com.example.springbootweb;

  2.  
  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

  5. import org.springframework.boot.autoconfigure.SpringBootApplication;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RequestMethod;

  8. import org.springframework.web.bind.annotation.RestController;

  9.  
  10. @EnableAutoConfiguration

  11. @SpringBootApplication

  12. @RestController

  13. public class SpringbootWebApplication {

  14.  
  15. public static void main(String[] args) {

  16.  
  17. SpringApplication.run(SpringbootWebApplication.class, args);

  18. }

  19. @RequestMapping(value = "/test",method = RequestMethod.GET)

  20. public String test(){

  21. return "test success";

  22. }

  23.  
  24. }

12.执行main方法启动项目,访问localhost:8080/test,出现如下页面表示项目搭建成功

至此springboot聚合项目搭建成功 ,刚开始写博客有不足之处大家多多体谅

  • 点赞 3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值