24
votes

I have a multi-module maven project like this:

my-parent
--my-domain
--my-service
--my-app <<< this is a Spring Boot module

I want to run mvn spring-boot:run command directly from the parent module without having to cd into the 'my-app' directory first.

I figure this is related to configuration of the spring-boot-maven-plugin but I can't seem to get it right.

I have tried the following:

  1. Use spring-boot-starter-parent and otherwise default config with spring-boot-maven-plugin included in plugins section of my-app.
    Running mvn spring-boot:run from the parent results in:
    Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) on project my-parent: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1] in the parent module

  2. Do NOT use spring-boot-starter-parent.
    Define spring-boot-dependencies in depManagement as described elewhere.
    Define spring-boot-maven-plugin in pluginManagement section of my-parent and include the plugin in plugins section of my-app module.
    Running mvn spring-boot:run from the parent results in same error as #1:
    Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) on project my-parent: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]

  3. Do NOT use spring-boot-starter-parent.
    Define spring-boot-dependencies in depManagement as described elewhere.
    Define spring-boot-maven-plugin in plugins section of my-app.
    Running mvn spring-boot:run from the parent results in:
    No plugin found for prefix 'spring-boot' in the current project and in the plugin groups

In all cases described above, running mvn spring-boot:run from the my-app directory works fine.

I figure there should be a way to make this work. In a traditional non-Boot Spring project it was fairly simple to configure tomcat7 plugin such that i could run mvn tomcat7:run-war from the parent and the webapp sub-module would start as expected

2

2 Answers

38
votes

You can do it by adding following In parent pom:

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>

And in your In my-app (Spring Boot module) pom:

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <fork>true</fork>
      <skip>false</skip>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Now you can do execute in project root:

mvn -pl my-app -am spring-boot:run

Additional references:

2
votes

spring-boot maven plugin configuration options:

skip: skips the execution of sprint-boot:run  [default: false]

fork: Flag to indicate if the run processes should be forked [default: true]

see spring-boot maven plugin parameters

maven options:

-pl, --projects: Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path   

-am, --also-make: If project list is specified, also build projects required by the list

see maven CLI options