35

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.

heldt
  • 4,166
  • 7
  • 39
  • 67

2 Answers2

56

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>
Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • 2
    slight edit - you need to add a element below , otherwise it seems to work for me too. – hoserdude Jan 06 '14 at 18:59
  • @Dave What does "or equivalently the parent" mean exactly? Could you include that in the example, too? – Rop Jun 06 '14 at 01:37
  • 1
    I meant that (as far as dependencyManagement goes) scope=import or using that POM as a parent are equivalent. (When you use it as a parent you get other features, like plugin configuration.) – Dave Syer Jun 06 '14 at 06:27
  • 2
    When I replaced declaration of spring-boot-starter-parent with depdendency to "spring-boot-dependencies" artifact then I get the following error: no main manifest attribute, in target/my-app-0.1.0.jar Does it mean that I have to copy all the plugins from spring-boot-starter-parent? – Konrad Jun 30 '14 at 13:15
  • I guess it means you have to copy at least one of them :-). If it's a regular jar then maybe just the spring-boot plugin. – Dave Syer Jun 30 '14 at 13:53
  • This gets me "halfway"? What's the rest? I add this, but it doesn't work for me, that is, it doesn't build a standalone jar as I expected. – Joel May 31 '16 at 19:23
  • The spring-boot plugin can be used to build an executable jar (the goal is called "repackage" I think). – Dave Syer Jun 02 '16 at 06:38
9

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)

Community
  • 1
  • 1
saljuama
  • 2,906
  • 1
  • 20
  • 42