eclipse搭建springboot多模块项目

时间:2024-03-18 12:27:55

1、新建文件夹all-project,拷贝进来一个pom.xml文件

eclipse搭建springboot多模块项目

<?xml version="1.0" encoding="UTF-8"?>
<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.ddp</groupId>
	<artifactId>all-project</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>
	<name>all-project</name>

	<modules>
		 <!-- WEB打包 -->
		<module>all-web</module> 
		<module>all-controller</module>
	</modules>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<!-- 版本说明:这里 -->
	<dependencies>
		<!--支持全栈式Web开发,包括Tomcat和spring-webmvc。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

2、在all-project文件夹下新建all-web,all-controller文件夹

eclipse搭建springboot多模块项目

1、all-controller

src

可拷贝一个maven项目中的放进来

pom.xml

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<packaging>jar</packaging>
	<artifactId>all-controller</artifactId>

	<parent>
		<groupId>com.ddp</groupId>
		<artifactId>all-project</artifactId>
		<version>1.0.0</version>
	</parent>
	
</project>

2、all-web

eclipse搭建springboot多模块项目

src

可拷贝一个maven项目中的放进来

pom.xml

eclipse搭建springboot多模块项目

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>com.ddp</groupId>
		<artifactId>all-project</artifactId>
		<version>1.0.0</version>
	</parent>
	
	<artifactId>all-web</artifactId>
	<packaging>jar</packaging>


	<dependencies>
		<dependency>
			<groupId>com.ddp</groupId>
			<artifactId>all-controller</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>
	
	<!-- pom加入打包插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

3、eclipse将项目import进去

eclipse搭建springboot多模块项目

1、新建红框中的类和文件

Application(启动类)

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

application.properties

#项目名
server.context-path=/all-web
#端口号
server.port=8080

HelloWorldController (测试controller)

@RestController
@RequestMapping("/controller")
public class HelloWorldController {
	Logger log = LoggerFactory.getLogger(HelloWorldController.class);

	@RequestMapping("/testController")
	public void testController() {
		System.out.println("进来");
	}
}

2、注意蓝框中的包路径

其他模块中的bean必须在启动类Application包的子包下

4、启动Application

5、访问

http://localhost:8080/all-web/controller/testController

eclipse搭建springboot多模块项目