Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven module using spring-boot

I like to configure my applications in maven by creating modules like;

<groupId>com.app</groupId> <artifactId>example-app</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>  <modules>     <module>app-api</module>     <module>app-impl</module>     <module>app-web</module> </modules> 

The modules then use the 'example-app' as the parent.

Now I want use 'spring-boot' for my web application.

Is there a way to configure maven so that my 'app-web' is a spring-boot application?

The problem I'm facing is that you have to use spring-boot as a parent.

like image 205
heldt Avatar asked Dec 22 '13 15:12

heldt


People also ask

What is module in spring boot project?

The application module is the main module of the project. It contains the application class in which the main method is defined that is necessary to run the Spring Boot Application. It also contains application configuration properties, Controller, views, and resources.

How does Maven work with spring boot?

The Spring Boot Maven Plugin provides Spring Boot support in Apache Maven. It allows you to package executable jar or war archives, run Spring Boot applications, generate build information and start your Spring Boot application prior to running integration tests.


2 Answers

You don't have to use the spring-boot-starter-parent, it's just a way to get started quickly. All it provides are dependency management and plugin management. You can do both yourself, and you can use the spring-boot-dependencies (or equivalently the parent) to manage dependencies if you want a halfway step. To do that, use scope=import like this

<dependencyManagement>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-dependencies</artifactId>             <type>pom</type>             <version>1.0.2.RELEASE</version>             <scope>import</scope>         </dependency>     </dependencies> </dependencyManagement> 
like image 112
Dave Syer Avatar answered Oct 06 '22 20:10

Dave Syer


Another alternative, is to include in the parent pom, the parent declaration for spring boot, as shown in this post

example-app pom.xml:

<project>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>1.2.5.RELEASE</version>     </parent>     <modelVersion>4.0.0</modelVersion>     // rest of the example-app pom declarations </project> 

After that, in the modules poms (app-web, app-impl, etc.), you declare example-app as parent, but now you can include the starter dependencies as you would normally do in a regular project.

app-web pom.xml:

<project>     <parent>         <groupId>org.demo</groupId>         <artifactId>example-app</artifactId>         <version>1.0-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>     <name>app-web</name>     <artifactId>app-web</artifactId>     <version>1.0-SNAPSHOT</version>      <packaging>war</packaging>      <dependencies>         <dependency>             <groupId>org.demo</groupId>             <artifactId>app-api</artifactId>             <version>1.0-SNAPSHOT</version>          </dependency>         <dependency>             <groupId>org.demo</groupId>             <artifactId>app-impl</artifactId>             <version>1.0-SNAPSHOT</version>          </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-tomcat</artifactId>             <scope>provided</scope>         </dependency>     </dependencies>     // rest of the app-web pom declarations </project> 

Regarding version management, what i used in these examples aren't exactly the best practices, but since is out of the scope of the question i skipped dependencyManagement and parent properties usage.

Also, if there is a starter that is used in every module, you can declare the dependency in the parent pom and then all the modules will inherit it (for example spring-boot-starter-test)

like image 21
saljuama Avatar answered Oct 06 '22 20:10

saljuama