SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程

开发环境:IDEA,

                  SprngBoot 2.0.4,

                  Maven 2.19.1

工程结构:

                             父工程father

                                                   子模块  dao      (用于持久化数据跟数据库交互)

                                                   子模块  entity    (实体类)

                                                   子模块  service (处理业务逻辑)

                                                   子模块  web       (页面交互接收、传递数据,唯一有启动类的模块)

                                关系:         web依赖 service、dao、entity

                                                    service依赖 dao、entity

                                                    dao依赖 entity

                                                    entity谁都不依赖,独立的

这里我用比较常见的工程结构举例说明,有些公司的项目可能会把模块分的很细,或者会有两个程序入口,也就是两个可以启动的模块!这个我在文章最后会做说明!缕清了思路其实没那么复杂!

一,创建Maven多模块项目

先建立外层父工程         File →new →project  选择Spring Initializr          Next下一步到以下页面

工程结构如下

接下来,把src整个删掉,父工程不需要,因为父工程你就当它只有一个外壳就完了

接下来创建子模块  工程上右键 → new → Module  选择Spring Initaializr  下一步

重复以上动作,创建dao模块,service模块,web模块

service模块和entity模块一样什么都不需要引入

dao模块和web模块可以根据实际需求选择引入mysql,mybatis,redis,web这些,我把我的贴出来

删除每个子模块中没用的文件,.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml

删除除了web模块以外其它模块中的Applicatin启动项,和resources目录下的application.properties配置文件

以上动作操作完成以后如果你发现你的子模块变成了文件夹,没关系,找到Maven Projects刷新一下就好了

整理过后的项目结构是这样的

以上项目的基本结构就完成了,接下来建立各自依赖

二、依赖关系

打开父pom.xml修改打包方式jar为pom,注意:build内容也需要做替换,因为默认的spring-boot-maven-plugin这种方式,等到后期打包的时候他会一直提示你,你引入的依赖不存在!代码如下

 


 
 
  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. <!--父pom.xml-->
  6. <groupId>com.miu</groupId>
  7. <artifactId>father</artifactId>
  8. <version> 0.0.1-SNAPSHOT</version>
  9. <packaging>pom</packaging>
  10. <name>father</name>
  11. <description>Demo project for Spring Boot</description>
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version> 2.0.4.RELEASE</version>
  16. <relativePath/> <!-- lookup parent from repository -->
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
  21. <java.version> 1.8</java.version>
  22. </properties>
  23. <!--声明你有四个儿子 -->
  24. <modules>
  25. < module>entity</ module>
  26. < module>dao</ module>
  27. < module>service</ module>
  28. < module>web</ module>
  29. </modules>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.apache.maven.plugins</groupId>
  45. <artifactId>maven-compiler-plugin</artifactId>
  46. <version> 3.1</version>
  47. <configuration>
  48. <source>${java.version}</source>
  49. <target>${java.version}</target>
  50. </configuration>
  51. </plugin>
  52. <plugin>
  53. <groupId>org.apache.maven.plugins</groupId>
  54. <artifactId>maven-surefire-plugin</artifactId>
  55. <version> 2.19.1</version>
  56. <configuration>
  57. <skipTests> true</skipTests> <!--默认关掉单元测试 -->
  58. </configuration>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

这里有个坑需要注意,dao、service、entity这三个模块的pom.xml文件中不需要build 内容,直接干掉

entity 的 pom.xml 内容


 
 
  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. <groupId>com.miu</groupId>
  6. <artifactId>entity</artifactId>
  7. <version> 0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>entity</name>
  10. <description>Demo project for Spring Boot</description>
  11. <!--声明父模块-->
  12. <parent>
  13. <groupId>com.miu</groupId>
  14. <artifactId>father</artifactId>
  15. <version> 0.0.1-SNAPSHOT</version>
  16. <relativePath>../pom.xml</relativePath>
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
  21. <java.version> 1.8</java.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. </project>

dao 的 pom.xml 内容


 
 
  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. <!--dao 模块 pom.xml-->
  6. <groupId>com.miu</groupId>
  7. <artifactId>dao</artifactId>
  8. <version> 0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <name>dao</name>
  11. <description>Demo project for Spring Boot</description>
  12. <!--声明父模块-->
  13. <parent>
  14. <groupId>com.miu</groupId>
  15. <artifactId>father</artifactId>
  16. <version> 0.0.1-SNAPSHOT</version>
  17. <relativePath>../pom.xml</relativePath>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
  22. <java.version> 1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-data-redis</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.mybatis.spring.boot</groupId>
  31. <artifactId>mybatis-spring-boot-starter</artifactId>
  32. <version> 1.3.2</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <!--dao 模块 引入entity模块-->
  45. <dependency>
  46. <groupId>com.miu</groupId>
  47. <artifactId>entity</artifactId>
  48. <version> 0.0.1-SNAPSHOT</version>
  49. </dependency>
  50. </dependencies>
  51. </project>

service 模块的 pom.xml 内容


 
 
  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. <groupId>com.miu</groupId>
  6. <artifactId>service</artifactId>
  7. <version> 0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>service</name>
  10. <description>Demo project for Spring Boot</description>
  11. <!--声明父模块-->
  12. <parent>
  13. <groupId>com.miu</groupId>
  14. <artifactId>father</artifactId>
  15. <version> 0.0.1-SNAPSHOT</version>
  16. <relativePath>../pom.xml</relativePath>
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
  21. <java.version> 1.8</java.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <!--service模块 引入entity模块-->
  34. <dependency>
  35. <groupId>com.miu</groupId>
  36. <artifactId>entity</artifactId>
  37. <version> 0.0.1-SNAPSHOT</version>
  38. </dependency>
  39. <!--service模块 引入dao模块-->
  40. <dependency>
  41. <groupId>com.miu</groupId>
  42. <artifactId>dao</artifactId>
  43. <version> 0.0.1-SNAPSHOT</version>
  44. </dependency>
  45. </dependencies>
  46. </project>

web模块的 pom.xml 内容

          注意build部分,因为web模块作为程序的入口启动,所以它需要打包,并且要指定Main Class


 
 
  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. <groupId>com.miu</groupId>
  6. <artifactId>web</artifactId>
  7. <version> 0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>web</name>
  10. <description>Demo project for Spring Boot</description>
  11. <!--声明父模块-->
  12. <parent>
  13. <groupId>com.miu</groupId>
  14. <artifactId>father</artifactId>
  15. <version> 0.0.1-SNAPSHOT</version>
  16. <relativePath>../pom.xml</relativePath>
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
  21. <java.version> 1.8</java.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-data-redis</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.mybatis.spring.boot</groupId>
  34. <artifactId>mybatis-spring-boot-starter</artifactId>
  35. <version> 1.3.2</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <scope>runtime</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. <!--web模块 引入entity模块-->
  48. <dependency>
  49. <groupId>com.miu</groupId>
  50. <artifactId>entity</artifactId>
  51. <version> 0.0.1-SNAPSHOT</version>
  52. </dependency>
  53. <!--web模块 引入service模块-->
  54. <dependency>
  55. <groupId>com.miu</groupId>
  56. <artifactId>service</artifactId>
  57. <version> 0.0.1-SNAPSHOT</version>
  58. </dependency>
  59. <!--web模块 引入dao模块-->
  60. <dependency>
  61. <groupId>com.miu</groupId>
  62. <artifactId>dao</artifactId>
  63. <version> 0.0.1-SNAPSHOT</version>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. <configuration>
  72. <!-- 指定该Main Class为全局的唯一入口 -->
  73. <mainClass>com.miu.web.WebApplication</mainClass>
  74. <layout>ZIP</layout>
  75. </configuration>
  76. <executions>
  77. <execution>
  78. <goals>
  79. <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
  80. </goals>
  81. </execution>
  82. </executions>
  83. </plugin>
  84. </plugins>
  85. </build>
  86. </project>

到此为止所有的依赖全部完成!接下来就是测试!这里只用简单的测试来实验!

三、代码测试

entity模块中创建  EntiyTest类

dao模块中创建  DaoTest类

service模块中创建ServiceTest类

 

Web模块中创建WebTest类

最后把web模块中的application.properties文件补充一下就OK了,因为引入了mysql,redis等配置,所以数据源是要配的,不然运行起来会报错找不到数据源!


 
 
  1. server.port= 8080
  2. #-----------------------------------数据库配置----------------------------------------
  3. spring.datasource.driver- class-name=com.mysql.jdbc.Driver
  4. spring.datasource.url=jdbc:mysql: //127.0.0.1:3306/test?characterEncoding=utf8
  5. spring.datasource.username=root
  6. spring.datasource.password= 123
  7. #------------------------------------redis配置---------------------------------------
  8. spring.redis.database= 0
  9. spring.redis.host= 127.0.0.1
  10. spring.redis.port= 6379
  11. spring.redis.password=
  12. spring.redis.jedis.pool.max-active= 8
  13. spring.redis.jedis.pool.max-idle= 8
  14. spring.redis.jedis.pool.max-wait=- 1ms
  15. spring.redis.jedis.pool.min-idle= 0
  16. spring.redis.timeout= 10000ms

       一切准备就绪,开始运行web模块下的启动类进行测试

四、打包可执行jar

看到上面的页面就证明模块之间的依赖没有问题,调用正常,我这里是用简单的创建对象的这种方式来操作的,实际开发并不是这种操作,大部分都是通过 @Autowired 注解 来实现的注入,这里我就不做演示了,只要模块之间调用没问题,剩下的就是铺代码的事了,接下来还有最后一个打包问题,为什么要啰嗦那么多还要说打包问题呢,因为我建议在项目架构之初,除了搭框架以外,最好是在最开始的时候就测试一下打包,尤其是这种多模块项目之间各种依赖的这种工程的打包,如果等你代码写的铺天盖地的时候你在去想怎么打包,到时候有你头疼的!如果你是按照我本章的流程一步步下来的话,那么你完全不用担心打包问题,因为所有的pom.xml有已经配置好了,只需要动手运行 package打包动作就行了,第一次打包不需要clean,记住以后每次打包之前clean一下,关于为什么打jar包,不打war包这个问题,还有其它会遇到的问题,在文章最后会做说明!

双击运行package,看到BUILD SUCCESS  就证明打包成功了,如此简单?告诉你就是这么简单,前提是你的每一个模块下的pom.xml要配置好,谁需要打包,谁不需要打包,谁依赖谁,父工程是否声明了子模块,子模块是否声明了父工程是谁,这些是重点!

接下来去找你工程目录,web文件夹下的target文件夹,刚才打包好的jar文件,就放在这里了

然后我把这个jar文件上传到我的测试服务器,使用 java -jar  web-0.0.1-SNAPSHOT.jar 命令来测试运行打包的可执行jar文件到底行不行!

运行成功,输入我测试服务器地址测试也没问题,到此为止全部搞定


 

聚合工程举一个简单的例子,

整个工程你就当作一个公司,父工程(退休了什么也不干)只需要声明有几个儿子(子模块)就完事了,

子模块web声明父工程是谁,就当他是大儿子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它三个兄弟帮助

其它子模块声明父工程是谁,之间关系都是兄弟,不需要打包,哪里需要去哪里!

在此我说一下重点和需要注意的地方!

1.父pom.xml 打包方式,jar要更改为pom,build 需要更改

2.不需要打包的模块pom.xml文件中不要写<build>,全删掉,例如有些工程中的common模块,utils模块,entity模块,service模  块都不需要打包

3.声明父工程时,填写父工程位置<relativePath>../pom.xml</relativePath>

4.关于applicatin.properties配置文件,只需要在启动的模块中配置就可以了,

5.关于打包为什么打包jar包,不打war包,打war包目的是war包可以运行在tomcat下,但是SpringBoot是内置tomcat,如果你打war包,前提是干掉内置的tomcat,然后才能打包,各种麻烦,直接打包可执行jar包,使用java -jar 命令就可以完美的运行起来很方便!

6.真实开发中使用@Autowired 注解 来实现注入,而不是new对象这种方式,所以可能会产生注入以后报错,是因为你的启动类上没有配置扫描,使用

@ComponentScan(basePackages = "你的路径")注解来解决,如果你使用的持久层是Mybatis,那么你的mapper也需要扫描,在启动类上使用
@MapperScan("你的mapper文件地址")注解来解决,算了还是贴个图片吧

不罗嗦了,就到这里吧,一个文章写了两个小时可见我的良苦用心,(关键是我被这个多模块打包问题困扰了好长时间,网上各种找解决办法,说的天花乱坠,狗屁不通,服的五体投地)

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 我可以为您提供一些关于如何使用Maven来构建Spring Boot项目的建议:1.在您的计算机上安装Maven;2.使用Maven创建Spring Boot项目;3.使用Maven来配置Spring Boot项目;4.使用Maven来编译和运行Spring Boot项目。 ### 回答2: Maven是一个Java项目构建工具,可以方便地管理项目依赖、构建和部署等工作。而Spring Boot是一个基于Spring框架的快速开发框架,能够简化Spring应用程序的搭建和部署过程。 下面是使用Maven搭建Spring Boot项目的步骤: 1. 安装Maven:首先需要在本地计算机上安装Maven工具,可以从官方网站下载安装,并按照指引进行安装。 2. 创建项目:打开终端或命令提示符,进入要创建项目的目录,然后执行以下命令创建一个新的Maven项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 上述命令将根据Maven的`maven-archetype-quickstart`模板创建一个新项目。 3. 导入Spring Boot依赖:在项目的`pom.xml`文件中,添加Spring Boot的依赖,可以根据需要添加不同的模块,例如Web模块、数据访问模块等。 4. 编写Spring Boot应用程序:创建一个Java类,作为Spring Boot应用的入口点,使用Spring Boot的注解和配置来定义应用程序的行为和特性。 5. 打包项目执行`mvn clean package`命令,将项目打包成可执行的jar文件。 6. 运行项目:使用`java -jar`命令来启动Spring Boot应用程序,例如`java -jar my-project.jar`。 通过以上步骤,就可以使用Maven快速搭建一个Spring Boot项目了。在项目构建过程中,Maven会自动下载和管理项目所需的依赖,简化了项目配置和管理的工作。同时,Spring Boot框架提供了丰富的功能和约定,使得开发者能够快速地构建出高效、可靠的Java应用程序。 ### 回答3: Maven是一种基于Java的项目管理工具,可以用来管理项目的构建、依赖关系和发布等方面。搭建Spring Boot项目时,可以使用Maven来简化项目的管理和构建过程。 首先,需要在本地安装好Maven,并确保Maven的环境变量配置正确。 接下来,可以使用Maven的命令行工具或者使用集成开发环境(IDE)来创建一个新的Spring Boot项目。在命令行中,可以使用`mvn archetype:generate`命令来生成一个基础的Spring Boot项目。 在生成项目时,可以选择相应的Spring Boot版本、项目的groupId和artifactId等信息。生成项目后,可以使用IDE打开项目,并将其导入为Maven项目。 在项目的pom.xml文件中,可以定义项目依赖关系和插件配置。通过在dependencies标签中添加需要的依赖,可以引入Spring Boot及其相关的第三方库。同时,也可以配置Maven打包时的插件,以及其他项目的构建参数。 在完成依赖关系的配置后,可以使用Maven的命令行工具或IDE提供的Maven插件来构建、运行和发布Spring Boot项目。 通过运行`mvn clean install`命令,可以使用Maven编译项目、运行测试并将可执行jar包安装到本地的Maven仓库中。 通过运行`mvn spring-boot:run`命令,可以直接在开发环境中运行Spring Boot应用。 通过运行`mvn package`命令,可以将项目打包为可执行jar包或war,用于部署到服务器上。 总而言之,使用Maven搭建Spring Boot项目可以简化项目的管理、依赖关系的维护和项目的构建过程。通过合理配置pom.xml文件,可以方便地添加所需的依赖和插件,提高项目的开发效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值