Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to spring-boot:run on root pom.xml from multi-module project

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

like image 734
qxlab Avatar asked Sep 20 '17 05:09

qxlab


2 Answers

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.

like image 112
Hossein Mohammadi Avatar answered Nov 01 '22 13:11

Hossein Mohammadi


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

like image 2
Akash Mishra Avatar answered Nov 01 '22 13:11

Akash Mishra