Hasan Can Saral
Hasan Can Saral

Reputation: 3288

Cannot package Spring Boot application properly in a multi module project

Basically, I have a multi module project as follows:

parent-project
├── core-app
└── web-app

core-app and web-app are both Spring Boot projects, and their parent is parent-project instead of spring-boot-starter-parent. So because of it, they both have:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <type>pom</type>
                <version>${spring.boot.version}</version>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

in their pom.xml, as well as:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.version}</version>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

This setup perfectly runs within IntelliJ, yet, if I package the application, I get a very small .jar and basically if I try to run it executable, I get several command not found errors.

If I run it with java -cp my.jar com.Main I get SpringApplication not found. So from this, I understand that the core-app and web-app doesn't have all the dependencies within the jar.

If I build it into a standalone .jar with spring-boot-starter-parent as its parent; it works.

What am I doing wrong? Thanks.

Update:

Now that was what's happening when I mvn package. However, when I actually execute mvn spring-boot:repackage I am getting:

Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Source must refer to an existing file

As suggested here this seems to solve the issue, yet I doubt that it might not be the standard way. For example:

mvn clean package spring-boot:repackage does the trick, however, I cannot mvn clean && mvn package && mvn spring-boot:repackage. I would appreciate an explanation on this. Thanks!

Upvotes: 1

Views: 1694

Answers (1)

Maven projects can only have one parent, and your "modules" can't be modules unless parent-project is the parent. You can theoretically make project-parent extend the Boot parent, but that can get complicated in a hurry.

Instead, simply go ahead and use the Boot dependencies and inclusions selectively. Use the dependencyManagement and BOM import in parent-project, pull in only the dependencies actually needed for each module (probably including spring-boot-starter-web for your fat jar), and add the Maven plugin for standalone far-jar modules only:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then you'll get standard-sized (usually a few K) dependency jars and the whole treatment specifically where it's required.

Upvotes: 1

Related Questions