Add external library .jar to Spring boot .jar internal /lib
you can set 'includeSystemScope' to true.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
How to run a spring-boot jar with external jars which compile with maven dependency system?
For including a jar from a path into your Spring boot classpath see
External library folder for Spring Boot.
This means that you can safely manage your dependency in your pom by a repo (avoiding sytemPath dependencies).
Spring Boot Executable Jar with Classpath
If you just want add external libraries you can use the loader.path
property.
java -Dloader.path="your-lib/" -jar your-app.jar
UPDATE
If you also need to read additional files from the classpath you have to create/change the manifest file of your application.
Lets assume that your are initializing your Spring Boot context from the class de.app.Application
. Your MANIFEST.MF
should looks as follows:
Manifest-Version: 1.0
Main-Class: de.app.Application
Class-Path: your-lib/
And the you can simply start your app with java -Dloader.path="your-lib/" -jar MyApp.jar
.
For more information about the MANIFEST.MF please see Working with Manifest Files: The Basics.
SpringBoot run-time dynamic external configuration with Jar files
I managed to answer my own question. I documented the approach I took at medium:
https://medium.com/@Jeef/dynamically-loading-libraries-into-a-springboot-application-at-run-time-80639ee5aab?sk=5851d25c7106307ff0f2b0a4171ab613
Also I built a sample project demonstrating the implementation on GitHub: https://github.com/jeeftor/SpringBoot-Dynamic-JarLoad
Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?
I managed to load an application.properties file in external path while using -jar option.
The key was PropertiesLauncher.
To use PropertiesLauncher, pom.xml file must be changed like this:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!-- added -->
<layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
</configuration>
</plugin>
</plugins>
</build>
For this, I referenced the following StackOverflow question: spring boot properties launcher unable to use . BTW, In Spring Boot Maven Plugin document(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html), there is no mention that specifying ZIP triggers that PropertiesLauncher is used. (Perhaps in another document?)
After the jar file had been built, I could see that the PropertiesLauncher is used by inspecting Main-Class property in META-INF/MENIFEST.MF in the jar.
Now, I can run the jar as follows(in Windows):
java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar
Note that the application jar file is included in loader.path.
Now an application.properties file in C:\My\External\Dir\config is loaded.
As a bonus, any file (for example, static html file) in that directory can also be accessed by the jar since it's in the loader path.
As for the non-jar (expanded) version mentioned in UPDATE 2, maybe there was a classpath order problem.
How can I @Autowire a spring bean that was created from an external jar?
You have to scan at least the package containing the class you want to inject. For example, with Spring 4 annotation:
@Configuration
@ComponentScan("com.package.where.my.class.is")
class Config {
...
}
It is the same principle for XML configuration.
How to add a dependency to a Spring Boot Jar in another project?
By default, Spring Boot repackages your JAR into an executable JAR, and it does that by putting all of your classes inside BOOT-INF/classes
, and all of the dependent libraries inside BOOT-INF/lib
. The consequence of creating this fat JAR is that you can no longer use it as a dependency for other projects.
From Custom repackage classifier:
By default, the
repackage
goal will replace the original artifact with the repackaged one. That's a sane behaviour for modules that represent an app but if your module is used as a dependency of another module, you need to provide a classifier for the repackaged one.The reason for that is that application classes are packaged in
BOOT-INF/classes
so that the dependent module cannot load a repackaged jar's classes.
If you want to keep the original main artifact in order to use it as a dependency, you can add a classifier
in the repackage
goal configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
With this configuration, the Spring Boot Maven Plugin will create 2 JARs: the main one will be the same as a usual Maven project, while the second one will have the classifier appended and be the executable JAR.
Related Topics
String.Replaceall Single Backslashes With Double Backslashes
Loop Over All Fields in a Java Class
Check If One List Contains Element from the Other
How to Get Http Response Code Using Selenium Webdriver
Convert String With Dot or Comma to Float Number
Java 8 Streams - How to Merge Elements from the List With the Same Fields to One Element and Sum Up
Tomcat Server Error - Port 8080 Already in Use
Replace Whitespace in Json Keys
Checking If a Document Exists in a Firestore Collection
Reverse a String Without Affecting Special Characters
How to Write a User Input Based Constructor
Retrieving Data from Biometric Fingerprint Attendance Device
Java:Using Apache Poi How to Convert Ms Word File to Pdf
Gradle: How to Exclude a Particular Package from a Jar
Null Can Not Be Compared to Double - Why
How to Insert Null in MySQL Especially Int Datatype
Warning the Requested Profile "Pom.Xml" Could Not Be Activated Because It Does Not Exist