Search code examples
javaspring-bootmavenintellij-idea

multi-module maven spring boot application stops immediately after running


I'm creating a multi-module spring boot application using maven in IntelliJ. When I run it using IntelliJ, everything works fine. But when I use maven package (IntelliJ maven plugin), the pom file in root of project, does not work. But when I use jar file from gateway module, it has all the components. But two things:

  • It does not read my application.properties file
  • It starts the project and Started MyappGatewayApplication in 5.009 seconds (JVM running for 5.257) message appears, but immediately SpringApplicationShutdownHook triggers and stops the application.

Here are my pom.xml files (parent, gateway, service and util modules) consecutively:

//parent pom.xml

<modelVersion>4.0.0</modelVersion>

<groupId>com.mysite</groupId>
<artifactId>myapp</artifactId>
<packaging>pom</packaging>
<version>3.0.0-SNAPSHOT</version>
<name>myapp</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.mysite.gateway.MyappGatewayApplication</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.mysite.gateway.myappGatewayApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.7.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
        <version>2.7.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>2.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.7.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>2.7.8</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.11.5</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <version>0.11.5</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <version>0.11.5</version>
    </dependency>
    <dependency>
        <groupId>com.github.vladimir-bukhtoyarov</groupId>
        <artifactId>bucket4j-core</artifactId>
        <version>7.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20220924</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
</dependencies>

<modules>
    <module>myapp-gateway</module>
    <module>myapp-service</module>
    <module>myapp-util</module>
</modules>
</project>

//gateway module pom.xml

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.mysite</groupId>
    <artifactId>myapp</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</parent>

<groupId>com.mysite</groupId>
<artifactId>myapp-gateway</artifactId>
<version>3</version>
<name>myapp-gateway</name>
<description>myapp Gateway Module</description>

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>com.mysite</groupId>
        <artifactId>myapp-service</artifactId>
        <version>3</version>
    </dependency>
    <dependency>
        <groupId>com.mysite</groupId>
        <artifactId>myapp-util</artifactId>
        <version>3</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

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

//service module pom.xml

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.mysite</groupId>
    <artifactId>myapp</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</parent>

<groupId>com.mysite</groupId>
<artifactId>myapp-service</artifactId>
<version>3</version>
<name>myapp-service</name>
<description>myapp Service Module</description>

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>com.mysite</groupId>
        <artifactId>myapp-util</artifactId>
        <version>3</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

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

//util module pom.xml

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.mysite</groupId>
        <artifactId>myapp</artifactId>
        <version>3.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.mysite</groupId>
    <artifactId>myapp-util</artifactId>
    <version>3</version>
    <name>myapp-util</name>
    <description>myapp Utility Module</description>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.16.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>
    </dependencies>
</project>

Solution

  • Finally I found the issue. The problem was because of an unused and extra dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>
    

    after removing this dependency, everything worked correctly. It seems this dependency, didn't let Tomcat to be launched after running with java -jar.

    Thanks