6

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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are amazing Max, finally solved after spending many hours
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.
-3

Wouldn't:

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

work for you?

4 Comments

No, it's run through Eclipse IDE as a maven project
Then maybe editing .classpath file adding: <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="..../resources"> ...
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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.