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 项目打包成功
在这里插入图片描述

发布了56 篇原创文章 · 获赞 1 · 访问量 1168

猜你喜欢

转载自blog.csdn.net/atu1111/article/details/105265704
今日推荐