SpringBoot+Maven 多模块项目的构建、运行、打包

男娘i 2022-05-30 01:44 358阅读 0赞

项目使用的工具:

  • IntelliJ IDEA
  • JDK 1.8

  • apache-maven-3.3.9

项目的目录:

  • 主项目 springboot-multi
  • 子模块 entity、dao、service、web

一、使用IDEA创建一个SpringBoot项目 : File -> new -> Project 项目名称为springboot-multi

二、删除项目中的src目录,把pom.xml中的项目打包方式改为pom,如下:

  1. <groupId>com.example</groupId>
  2. <artifactId>springboot-multi</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <!-- 此处改为pom -->
  5. <packaging>pom</packaging>

三、创建springboot-multi项目的子模块,在项目上右键单击,选择:new -> Module。

四、创建四个子模块后,删除子模块中 src/main/java、src/main/java下的所有文件(如果没有文件跳过此操作),只保留web子模块的SpringBoot的Application主启动类。

五、主项目pom.xml (注意标签是否指定了子模块)

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.example</groupId>
  3. <artifactId>springboot-multi</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <!-- 此处改为pom -->
  6. <packaging>pom</packaging>
  7. <name>springboot-multi</name>
  8. <description>Demo project for Spring Boot</description>
  9. <modules>
  10. <module>web</module>
  11. <module>service</module>
  12. <module>dao</module>
  13. <module>entity</module>
  14. </modules>
  15. <parent>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-parent</artifactId>
  18. <version>1.5.10.RELEASE</version>
  19. <relativePath/> <!-- lookup parent from repository -->
  20. </parent>
  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. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. </dependencies>
  32. <!--指定使用maven打包-->
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.apache.maven.plugins</groupId>
  37. <artifactId>maven-compiler-plugin</artifactId>
  38. <version>3.1</version>
  39. <configuration>
  40. <source>${java.version}</source>
  41. <target>${java.version}</target>
  42. </configuration>
  43. </plugin>
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-surefire-plugin</artifactId>
  47. <version>2.19.1</version>
  48. <configuration>
  49. <skipTests>true</skipTests> <!--默认关掉单元测试 -->
  50. </configuration>
  51. </plugin>
  52. </plugins>
  53. </build>

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

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.example</groupId>
  3. <artifactId>web</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <packaging>jar</packaging>
  6. <name>web</name>
  7. <description>Demo project for Spring Boot</description>
  8. <parent>
  9. <groupId>com.example</groupId>
  10. <artifactId>springboot-multi</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <relativePath>../pom.xml</relativePath>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.example</groupId>
  17. <artifactId>service</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.example</groupId>
  22. <artifactId>dao</artifactId>
  23. <version>0.0.1-SNAPSHOT</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>com.example</groupId>
  27. <artifactId>entity</artifactId>
  28. <version>0.0.1-SNAPSHOT</version>
  29. </dependency>
  30. </dependencies>
  31. <!--spring boot打包的话需要指定一个唯一的入门-->
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. <configuration>
  38. <!-- 指定该Main Class为全局的唯一入口 -->
  39. <mainClass>com.example.WebApplication</mainClass>
  40. <layout>ZIP</layout>
  41. </configuration>
  42. <executions>
  43. <execution>
  44. <goals>
  45. <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
  46. </goals>
  47. </execution>
  48. </executions>
  49. </plugin>
  50. </plugins>
  51. </build>

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

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.example</groupId>
  3. <artifactId>service</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <packaging>jar</packaging>
  6. <name>service</name>
  7. <description>Demo project for Spring Boot</description>
  8. <parent>
  9. <groupId>com.example</groupId>
  10. <artifactId>springboot-multi</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <relativePath>../pom.xml</relativePath>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.example</groupId>
  17. <artifactId>dao</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.example</groupId>
  22. <artifactId>entity</artifactId>
  23. <version>0.0.1-SNAPSHOT</version>
  24. </dependency>
  25. </dependencies>

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

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.example</groupId>
  3. <artifactId>dao</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <packaging>jar</packaging>
  6. <name>dao</name>
  7. <description>Demo project for Spring Boot</description>
  8. <parent>
  9. <groupId>com.example</groupId>
  10. <artifactId>springboot-multi</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <relativePath>../pom.xml</relativePath>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.example</groupId>
  17. <artifactId>entity</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. </dependency>
  20. </dependencies>

九、entity子模块

  1. <modelVersion>4.0.0</modelVersion>
  2. <groupId>com.example</groupId>
  3. <artifactId>entity</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. <packaging>jar</packaging>
  6. <name>entity</name>
  7. <description>Demo project for Spring Boot</description>
  8. <parent>
  9. <groupId>com.example</groupId>
  10. <artifactId>springboot-multi</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. <relativePath>../pom.xml</relativePath>
  13. </parent>

十、pom.xml文件中需要注意的就是:

  • 主项目的modules标签指定的子模块是否正确
  • 子模块之间的依赖
  • 子模块的parent标签

十一、web子模块的Application启动类:

  1. @RestController
  2. @SpringBootApplication
  3. public class WebApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(WebApplication.class, args);
  6. }
  7. @RequestMapping(value = "/test",method = RequestMethod.GET)
  8. public String test(){
  9. return "test success";
  10. }
  11. }

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

20180301160308434

十三、项目打包命令: mvn clean package 或者 使用右边工具栏的图形化界面打包也可以:

20180301160558554

十四、打包成功日志:

20180301160728976

发表评论

表情:
评论列表 (有 0 条评论,358人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot模块项目打包

    springboot多模块项目打包 今天在项目开发中,项目经理说实现以下APP和web端单独打包功能实现。因为app和web端是单独的modules,这样的话,app的代