Multi-Module maven with Spring Download

I want to know how I can create a project with multiple modules using maven using Spring Boot.

Can someone show me an example of โ€œparent-pomโ€ and โ€œbaby-pomโ€?

thanks

+6
source share
3 answers

This question is too simple to answer or too complex to be understood, otherwise I cannot explain why there are no answers.

I would choose a solution that uses a folder structure as shown below.

project folder |---pom.xml |---module1 |---pom.xml |---src |---module2 |---pom.xml |---src |---module3 |---pom.xml |---src 

I would define a pom.xml project with pom packaging, for example:

 <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.7-RELEASE</version> <relativePath/> </parent> <groupId>com.myproject</groupId> <artifactId>project</artifactId> <packaging>pom</packaging> <version>0.0.1-SNAPSHOT</version> <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> <name>MySupercoolProject</name> <description>What else?</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Put here dependencies shared by your project --> </dependencies> </project> 

As you can see, the spring-boot-starter-parent will be the parent of your project.

Then pom.xml one of your modules will be, for example:

 <?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> <parent> <groupId>com.myproject</groupId> <artifactId>project</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.myproject</groupId> <artifactId>module1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Application</name> <description>SudenGut application</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <start-class>com.myproject.MyApplication</start-class> <java.version>1.8</java.version> <tomcat.version>8.0.24</tomcat.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--- this will not be enough to provide a cool app :) --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins </build> </project> 

where the main application is MyApplication.java in the package com.myproject module1 .

I would use jar packaging for other modules without the main application if you don't have other submodules (i.e. pom packaging again).

+2
source

@juanhl is so simple. You can create a multi-module maven project using Spring Boot.

Steps: 1) Create a Maven project. The parent project pom.xml is similar to the following,

  <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.springmultumoduleweb</groupId> <artifactId>multimodule-web-app</artifactId> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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> <modules> <module>multimodule-api</module> <module>multimodule-service</module> <module>EarModule</module> </modules> <version>0.0.1-SNAPSHOT</version> </project> 

2) to add the maven module to the parent modules, child modules can have one or more dependencies on other modules, and the type of packaging can be jar or war.

3) create another module for creating an ear file (packaging type: ear) which contains the dependencies of all modules, as shown below,

 <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.springbootparent.demo</groupId> <artifactId>com.springbootparent.demo</artifactId> <version>0.0.1-SNAPSHOT</version </parent> <artifactId>com.springboot.ear</artifactId> <packaging>ear</packaging> <dependencies> <dependency> <groupId>com.springbootparent.demo</groupId> <artifactId>com.springboot.service1</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.springbootparent.demo</groupId> <artifactId>com.springboot.service2</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.springbootparent.demo</groupId> <artifactId>com.springboot.war1</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> <dependency> <groupId>com.springbootparent.demo</groupId> <artifactId>com.springboot.war2</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> </dependencies> </project> 

4) clean and install the parent maven project.

5) deploy the generated ear / war file on the server. (for ear files that need to be deployed to the Enterprise Application server)

-Good luck

0
source

Modules as spring boot project

Spring boot usually works with the parent. But if you are creating a project with several modules, and if you want to use spring loading on any of your module, the important question arises, which one will be the parent? Of course, your main project must be a parent. Therefore, you should use spring boot as a dependency in your module.

 <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 

For more information, review the following documentation;

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent

0
source

Source: https://habr.com/ru/post/971778/


All Articles