springboot maven多模块开发
**********************
示例
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父模块packaging:pom -->
<packaging>pom</packaging>
<modules>
<module>user-info</module>
<module>user-service</module>
<module>user-web</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<!-- 指定运行jar包的主类 -->
<mainClass>
com.example.demo.UserWebApplication
</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
******************
user-info 模块
右击项目 ==> new ==> module
maven ==> user-info
User
@Data
public class User {
private String name;
private Integer age;
}
pom.xml(build部分)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 生成可执行jar包,类似于dependency导入的jar包,不包含tomcat容器 -->
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
******************
user-service 模块
UserService
public interface UserService {
User getUser();
}
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUser() {
User user = new User();
user.setName("瓜田李下");
user.setAge(20);
return user;
}
}
pom.xml(build部分)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 生成可执行jar包 -->
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
******************
user-web 模块
UserController
@RestController
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/hello")
public User getUser(){
User user=userService.getUser();
System.out.println(user);
return user;
}
}
UserWebApplication
@SpringBootApplication
public class UserWebApplication {
public static void main(String[] args){
SpringApplication.run(UserWebApplication.class,args);
}
}
pom.xml(build部分)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.5</version>
<!-- 生成的jar包需要包含tomcat -->
</plugin>
</plugins>
</build>
**********************
使用测试
项目打包
user-info:生成exec jar包
user-service:生成exec jar包
user-web:生成original jar包
运行user-web jar 包,控制台输出:
2021-10-04 18:39:02.055 INFO 16808 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServ
let 'dispatcherServlet'
2021-10-04 18:39:02.055 INFO 16808 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherSe
rvlet'
2021-10-04 18:39:02.057 INFO 16808 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
User(name=瓜田李下, age=20)