Search code examples
spring-boot

Run mvn spring-boot:run from parent module?


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


Solution

  • 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: