This article mainly introduces the construction, operation and packaging of SpringBoot+Maven multi-module projects. It is shared with you. The details are as follows:
Tools used by the project:
Project Directory:
1. Create a SpringBoot project using IDEA: File -> new -> Project The project name is springboot-multi
2. Delete the src directory in the project and change the packaging method of the project in pom.xml to pom, as follows:
<groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Change to pom --> <packaging>pom</packaging>
3. Create a submodule of the springboot-multi project, right-click on the project, and select: new -> Module.
4. After creating four submodules, delete all files under src/main/java and src/main/java in the submodule (skip this if no files are skipped), and only retain the main application startup class of the web submodule.
5. Main project pom.xml (note whether the <modules> tag specifies a submodule)
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Change to pom --> <packaging>pom</packaging> <name>springboot-multi</name> <description>Demo project for Spring Boot</description> <modules> <module>web</module> <module>service</module> <module>dao</module> <module>entity</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--Specify using maven-package--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> <!--Switch off unit tests by default-> </configuration> </plugin> </plugins> </build> 6. Web submodule pom.xml (depends on service, dao, entity submodule)
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>web</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <!--Spring boot package requires a unique starter --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- Specify the Main Class as the global unique entry --> <mainClass>com.example.WebApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--You can package all the dependencies into the generated Jar package--> </goals> </execution> </executions> </plugin> </plugins> </build>
7. Service submodule pom.xml (depends on dao and entity submodules)
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependency> </dependency>
8. dao submodule pom.xml (depend on entity submodule)
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dao</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
9. entity submodule
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>entity</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>springboot-multi</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent>
10. What you need to pay attention to in the pom.xml file is:
11. Application startup class of web submodule:
@RestController @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @RequestMapping(value = "/test",method = RequestMethod.GET) public String test(){ return "test success"; } }12. Execute the main method to start the project, visit localhost:8080/test, and the following page appears to indicate that the project is successfully built:
13. Project packaging command: mvn clean package or use the graphical interface of the toolbar on the right to package:
14. Packaging success log:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.