7

I have a multi-module project with Spring Boot.

My root pom.xml only contains this:

<?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>

    <groupId>com.something</groupId>
    <artifactId>my-application</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

</project>

My spring-boot-maven-plugin is on application module as shown below:

<?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>

    <groupId>com.something.tcc</groupId>
    <artifactId>my-application-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <jsf.version>2.2.11</jsf.version>
    </properties>

    <dependencies>
        <!-- use this for automatic hot deployment on code changes -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

So today, from my root directory, I have to execute my app like this:

mvn spring-boot:run -pl application

If I try mvn spring-boot:run, I receive the error:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project my-application-library: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]

I'd like to know what I need to do in order to be able to execute exactly the following command in my root directory:

mvn spring-boot:run

EDIT: as per comment's suggestion I tried moving configuration from application module to root pom.xml, but I still receive the same error:

<?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>

    <groupId>com.something</groupId>
    <artifactId>my-application</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.something.tcc.Main</start-class>
</properties>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>  
    </dependencies>


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

</project>

Thanks

qxlab
  • 1,506
  • 4
  • 20
  • 48
  • move the configuration from application `pom.xml` to the parent's `pom.xml`, did you try that? – Naman Sep 20 '17 at 05:19
  • @nullpointer are you suggesting that I move everything including my `Main` class? If not I would receive the same error – qxlab Sep 20 '17 at 05:25
  • Not the code, but the configuration in the pom.xml that you must have specified in application module's pom.xml to parent pom.xml. – Naman Sep 20 '17 at 05:26
  • i guess below link will help you: https://stackoverflow.com/questions/11091311/maven-execjava-goal-on-a-multi-module-project – Nidhi257 Sep 20 '17 at 05:27
  • @nullpointer I tried and received the same error, please see edited post... – qxlab Sep 20 '17 at 05:33
  • @Nidhi257 as I said I want to execute exactly `mvn spring-boot:run` – qxlab Sep 20 '17 at 05:33
  • try adding tag in pom.xml ` com.abc.Application ` – Nidhi257 Sep 20 '17 at 05:35
  • @qxlab Ya, you need to configure a MainClass or a property in the parent pom.xml to work that way. – Naman Sep 20 '17 at 05:37
  • @nullpointer I tried and received the same error. Please note this part of the error `on project my-application-library` maybe I should do something on the child modules? – qxlab Sep 20 '17 at 05:41

2 Answers2

7

To address this issue you only need to change configuration in spring-boot-maven-plugin in pom.xml.

Considering you have a spring boot multi-module project your project structure should be something like this

spring-boot-projcet  
├─service  
└─webapp  

The straightforward and the simplest example which I have found was the project which was provided by Burkhard Graves
https://github.com/drahkrub/spring-boot-multi-module

To solve your problem add changes to pom.xml in parent module

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

Add chnages to pom.xml in webapp module

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

With this setup the usual mvn clean spring-boot:run is possible in the root directory of the project.

1

you can define your main class with either start-class property or mainClass configuration xml tag in your pom.xml Find the description here.

Akash Mishra
  • 682
  • 1
  • 5
  • 13
  • In which `pom.xml` are you suggesting that I do that? From `library` module? – qxlab Sep 20 '17 at 05:36
  • parent pom @qxlab – Naman Sep 20 '17 at 05:36
  • Do you want to try moving all configurations back to `application` module's `pom.xml` once. And with `spring-boot-maven-plugin` on your `application` module, run the `mvn spring-boot:run` when in `application` module directory? If not already tried. – Akash Mishra Sep 20 '17 at 05:58
  • That works @AkashMishra, but I want to be able to execute it from root directory, if possible – qxlab Sep 20 '17 at 06:02