How to build a multi-module Maven project without a main class in one of the modules
Asked Answered
D

5

10

I've been trying to find an answer to this issue in similar questions but I still don't know what is causing it.

I have a multi-module maven project and I'm trying to run mvn install or mvn package and I get the following error

[INFO] app ................................................... SUCCESS [ 1.025 s]

[INFO] module-data ..................................... FAILURE [ 0.952 s]

[INFO] module-app ...................................... SKIPPED

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) on project project-data: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:repackage failed: Unable to find main class -> [Help 1]

The project-app module has a java class in the src folder, whereas the project-data module does not have a main class.

parent pom.xml

<?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>
    <packaging>pom</packaging>
    <modules>
        <module>project-data</module>
        <module>project-app</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app</name>
    <description></description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
            </plugin>
        </plugins>
    </build>

</project>

project-data pom.xml

<?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">
    <parent>
        <artifactId>app</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project-data</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

project-app pom.xml

<?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">
    <parent>
        <artifactId>app</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project-app</artifactId>

    <dependencies>
        <dependency>
            <artifactId>project-data</artifactId>
            <groupId>com.example</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

I have something very similar to the structure of this project (sfg-pet-clinic), however, after downloading and running mvn package, it works fine. I don't see much difference between the pom files of my project and this one so what could I be missing?

Divisor answered 1/1, 2019 at 23:18 Comment(3)
if the main class is in another module than the boot plugin you can specify that main class inside the plugin config: docs.spring.io/spring-boot/docs/current/maven-plugin/…Valvate
you're welcome :)Valvate
Updated link: docs.spring.io/spring-boot/docs/current/maven-plugin/reference/…Rosenarosenbaum
D
4

Thanks to @wemu on the comments section.

Adding this:

<configuration>
    <mainClass> ${your.start.Class}</mainClass>
</configuration>

to the plugin section is required on version 2.1.1.RELEASE of the spring-boot-starter-parent dependency, as long as your class is in another module.

The project sfg-pet-clinic I referred to in my question uses an older version which does not need the <mainClass> configuration

Divisor answered 2/1, 2019 at 4:13 Comment(2)
don't know who downvoted this. thanks for documenting your answer to the question. If this answers your question go ahead an accept it too :)Valvate
not sure why the correct answer is being downvoted, but thanks again @Valvate ;) i just accepted itDivisor
T
13

Configuration is for older versions of spring.

Remove:

    <configuration>
       <skip>true</skip>
    </configuration>

Instead, add this flag below pom artifactId

   <properties>
        <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>

Like so:

 <parent>
        <artifactId>mc-pet-clinic</artifactId>
        <groupId>guru.springframework</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pet-clinic-data</artifactId>

    <properties>
        <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>

...
Theine answered 22/3, 2019 at 12:0 Comment(0)
C
5

I would move the entire

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

section from the parent to the module-app module, because that one is the single module which uses the repackage functionality.

Moreower I would delete the plugin section from the module-data too, because there is no need to repackage that module.

Combustion answered 2/1, 2019 at 0:1 Comment(0)
D
4

Thanks to @wemu on the comments section.

Adding this:

<configuration>
    <mainClass> ${your.start.Class}</mainClass>
</configuration>

to the plugin section is required on version 2.1.1.RELEASE of the spring-boot-starter-parent dependency, as long as your class is in another module.

The project sfg-pet-clinic I referred to in my question uses an older version which does not need the <mainClass> configuration

Divisor answered 2/1, 2019 at 4:13 Comment(2)
don't know who downvoted this. thanks for documenting your answer to the question. If this answers your question go ahead an accept it too :)Valvate
not sure why the correct answer is being downvoted, but thanks again @Valvate ;) i just accepted itDivisor
M
1

Okay here is how I fixed that.

My Steps were:

  1. Change the packaging of pom.xml file for project-data to pom from jar (I used overview of Eclipse)
  2. add <type>pom</type> in pom.xml file for project-app for dependency of module-data
        <dependency>
            <groupId>samee.springframework</groupId>
            <artifactId>project-data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
             <type>pom</type>
        </dependency>  

Then you can run your clean and package/install

Mytilene answered 17/1, 2019 at 22:22 Comment(0)
C
0

I just changed the Spring Boot version in the parent pom.xml from 2.1.1 to 2.0.3 and it works.

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

Would be better to make it work in 2.1.1 though, but that's a quick and simple solution.

Calamint answered 2/2, 2019 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.