Springboot使用Maven构建多模块项目

在以往javaweb开发的项目中,会为项目分层开发,一般我们会在一个功能中新建不同的package,然后把不同的类放在不同的package中,比如有api、impl、web、common等等。如下图:
在这里插入图片描述

1、建立一个Maven的parent项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

删除掉parent项目的src文件夹

在这里插入图片描述

最后生成的父项目如下图:

在这里插入图片描述

2、配置parent项目pom文件,使其变成springboot项目,一些公共的依赖可以放在这个项目里面。

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

3、新建module子项目,比如为sync项目新建一个web、一个api、一个impl、一个common

在这里插入图片描述
新建web项目,点击next
在这里插入图片描述

在这里插入图片描述

点击finish后,生成如下图结构:
在这里插入图片描述
然后在sync-web的pom.xml中添加打包配置

<build>
		<!-- 加载资源文件 -->
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.yml</include>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.yml</include>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.xls</include>
					<include>static/**</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
		<finalName>sync</finalName>
		<plugins>
			<!-- jar发布时跳过此模块 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-deploy-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

若sync-web报错则maven-update一下,同时在sync-web的目下新建com.booway.sync的package,并新增下面的.java文件
在这里插入图片描述

package com.booway.sync;

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

@SpringBootApplication
public class SyncWebApplication
{

	public static void main(String[] args)
	{
		SpringApplication.run(SyncWebApplication.class, args);
	}
}

package com.booway.sync;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SyncWebApplication.class);
	}

}

4、依照web层,接下来生成api层、impl层、common层

生成sync-historyproject的 api 和 impl 模块
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1、生成sync-historyproject-api模块

在这里插入图片描述
2、生成sync-historyproject-impl模块
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3、生成sync-common模块

同生成sync-historyproject-api一致,对sync-parent右键Maven ->New Maven Module Project,选择Create a simple project (skip archetype selection),点击Next
在这里插入图片描述
最后生成如下图,即完成一个springboot多模块项目
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一款非常流行的Java Web框架,而多模块项目构建则是在实际开发中非常常见的需求。Maven是Java应用的依赖管理工具,结合Spring Boot可以很方便地构建、运行和打包多模块项目。 首先,需要创建一个父模块,它将在编译和打包时引用所有子模块。在父模块的pom.xml文件中,需要指定每个子模块的groupId、artifactId和version等信息。 接下来,我们可以创建模块,每个子模块都可以包含一个或多个Spring Boot应用程序。在子模块的pom.xml文件中,需要指定它们的父模块坐标,以及它们自身的坐标。 在Java代码中,我们只需要使用@SpringBootApplication注解启动Spring Boot应用程序,以便使用Spring Boot的自动配置和构件功能。同时,我们也可以使用@SpringBootApplication注解打包可执行jar文件,另外需要使用Maven插件将其构建为可执行jar包,这样我们就可以使用"java -jar"命令行运行应用程序。 最后,我们需要为所有子模块构建和打包项目。在Maven的父模块pom.xml文件中,执行"mvn clean install"命令即可完成所有子模块和父模块构建和打包工作。 总结来说,Spring BootMaven的多模块构建、运行和打包实战需要以下步骤:创建模块和子模块使用@SpringBootApplication注解启动应用程序,使用Maven插件构建jar包,最后执行"mvn clean install"命令完成构建和打包。这个过程需要多次尝试和调整,但可以大大提高开发效率和项目的可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值