2

I am trying to use Maven to package Spring Boot with multi modules,Here my main module pom.xml :

<modules>
    <module>my-data-services</module>
    <module>my-message-services</module>
    <module>my-common</module>
</modules>
<groupId>com.my</groupId>
<artifactId>my-multi-services</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencyManagement>....</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

my-common pom.xml:

<parent>
    <artifactId>my-multi-services</artifactId>
    <groupId>com.my</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-common</artifactId>
<packaging>jar</packaging>
<dependencies>....</dependencies>

and my-data-services pom.xml:

<parent>
    <artifactId>my-multi-services</artifactId>
    <groupId>com.my</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-data-services</artifactId>

<dependencies>

    <dependency>
        <groupId>com.my</groupId>
        <artifactId>my-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

my-common module is just a common utils lib not a runnable module,but when i trying to mvn clean package,exception throws below:

Execution default of goal org.springframework.boot:spring-boot-maven-p
lugin:1.4.2.RELEASE:repackage failed: Unable to find main class 

then i add a main class,this module can package,but its not a lib jar, its like a runnable spring boot jar

 -BOOT-INF
 |
 -META-INF
 |
 -org

and the exception throws

Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:3.1:compile (default-compile) on project my-data-services: Compilation failure: Compilation failure: package com.my.common.utils does not exist;

com.my.common.utils is in module my-common

How do i fix this problem, and in spring boot multi modules project,how to package a common utils lib without BOOT-INF

user3522022
  • 57
  • 1
  • 5
  • By don't making it a Spring Boot application. Due to your inheritance in the poms everything is a spring boot application and you shouldn't include a spring boot application in another spring boot application. – M. Deinum Jan 03 '17 at 14:46
  • 1
    This basically is a duplicate of http://stackoverflow.com/questions/40367589/import-spring-boot-app-into-another-project – M. Deinum Jan 03 '17 at 14:49
  • @M.Deinum No it's not. He is talking about a multi-module project not importing a spring-boot app into another one. – Lakatos Gyula Jan 03 '17 at 15:49
  • Yes it is... Because his projects are all Spring Boot projects (which he probably didn't do on purpose but that is what his current approach is due to his parent pom which contains the spring boot maven plugin). – M. Deinum Jan 06 '17 at 06:56

1 Answers1

6

This happens because your modules will have the 'spring-boot-maven-plugin' added to them because it's defined in their parent.

What you need to do is move everything into submodules, even your application starter class. How I do this usually:

  • my-parent-module
    • my-service-module
    • my-common-module
    • my-web-module (or my-runtime-module in a non-web app)

The my-parent-module will have 'spring-boot-starter-parent' as it's parent but it not going to have an src folder because everything is moved into submodules.

The my-web-module will depend on the other modules and will have the 'spring-boot-maven-plugin'. You will be able to run the app with 'mvn spring-boot:run' in the my-web-module folder.

Lakatos Gyula
  • 3,949
  • 7
  • 35
  • 56