5

How do I add an extra classpath entry into my spring boot run from maven?

I think I need to add something like this to my pom.xml:

            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>C:/resources</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>

However, I do not know what plugin is applicable.

Trant
  • 3,461
  • 6
  • 35
  • 57
  • Possible duplicate of [Adding classpath to SpringBoot command line start when using maven-spring-boot-plugin](https://stackoverflow.com/questions/45710051/adding-classpath-to-springboot-command-line-start-when-using-maven-spring-boot-p) – Chris DaMour Apr 25 '19 at 21:08

2 Answers2

7

I assume what you want is to add your additional resources/configuration files to the classpath in the generated executable jar of Spring Boot. The only off the shelf solution I have found was to use the maven-jar-plugin in addition to the spring-boot-maven-plugin to add the classpath to the manifest, for example:

<!-- setup jar manifest to executable with dependencies -->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.3.RELEASE</version>
    <configuration>
        <fork>true</fork>
        <mainClass>Application</mainClass>
        <classifier>executable</classifier>
        <layout>JAR</layout>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- add configuration to manifest -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

Note that the class-path will be relative to the generated executable JAR file.

  • You are amazing Max, finally solved after spending many hours – Prateek Mehta Mar 10 '20 at 13:38
  • 1
    Great! I don't know why, when starting a Spring Boot application with `java -jar`, it ignores both the `-classpath` switch and the `CLASSPATH` envionment variable. The only way to make it find the class is to add the external jar to the Manifest's classpath through this plugin. – PJ_Finnegan Jan 10 '22 at 14:24
-3

Wouldn't:

java -cp <path to classpath entry> -jar <path to program.jar>

work for you?

ootero
  • 3,235
  • 2
  • 16
  • 22
  • No, it's run through Eclipse IDE as a maven project – Trant Apr 18 '17 at 17:34
  • Then maybe editing .classpath file adding: ... – ootero Apr 18 '17 at 18:39
  • what is .classpath? Is that for the whole IDE? That won't work for me because when I package the spring boot jar I would lose that classpath entry. – Trant Apr 19 '17 at 19:34
  • 2
    As far as I can tell and from info I read the -cp or -classpath option only work if you don't use the -jar option, which is how I run my spring boot app – Trant May 22 '17 at 20:18