Yannis Lionis
Yannis Lionis

Reputation: 75

Spring boot maven plugin with a main class in a dependency

I have a perhaps unusual case where I want to use the spring-boot maven plugin to perform 'mvn spring:start' (before integration tests) on a project with spring services, but the class with the main method is in a jar file. The reason for this is that there will be a number of these spring services that require some common structures in place when started up for testing purposes, so the idea is that there will be a common jar with the common stuff and the class with the main method and each individual service project will simply reuse that.

Unfortunately I am getting ClassNotFoundException for the class with the main method - spring boot clearly isn't looking in the jar files but only in the compiled classes of this project.

The pom in question (trimmed):

<?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>something</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    ...
    <dependency>
        <groupId>com.something</groupId>
        <artifactId>artifact-with-main-method</artifactId>
        <version>0.0.1-SNAPSHOT</version
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.4.2.RELEASE</version>
            <dependencies>
                <dependency>
                    <groupId>com.something</groupId>
                    <artifactId>artifact-with-main-method</artifactId>
                    <version>0.0.1-SNAPSHOT</version
                </dependency>
            </dependencies>
            <configuration>
               <mainClass>com.something.Application</mainClass>
                <requiresUnpack>
                    <dependency>
                        <groupId>com.something</groupId>
                        <artifactId>artifact-with-main-method</artifactId>
                    </dependency>
                </requiresUnpack>
            </configuration>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
</project>

Output:

java.lang.ClassNotFoundException: com.something.Application
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:501)
    at java.lang.Thread.run(Thread.java:745)

UPDATE: I gave up trying to get this to work. I ended up using the mvn:exec plugin to execute a java command doing what I needed it instead. It works well enough.

Upvotes: 2

Views: 1998

Answers (1)

Hasan Can Saral
Hasan Can Saral

Reputation: 3288

Do you have:

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

in the dependency too? If yes, then it will make the dependency module impossible to import to the current module/project. So, remove it from the dependency, and add it to the current module that you are importing the main class into.

Upvotes: 1

Related Questions