Spring Boot Jar Resources


License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.ulisesbocchio
ArtifactId

ArtifactId

spring-boot-jar-resources
Last Version

Last Version

1.3
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Boot Jar Resources
Spring Boot Jar Resources

Download spring-boot-jar-resources

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ulisesbocchio/spring-boot-jar-resources/ -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>spring-boot-jar-resources</artifactId>
    <version>1.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ulisesbocchio/spring-boot-jar-resources/
implementation 'com.github.ulisesbocchio:spring-boot-jar-resources:1.3'
// https://jarcasting.com/artifacts/com.github.ulisesbocchio/spring-boot-jar-resources/
implementation ("com.github.ulisesbocchio:spring-boot-jar-resources:1.3")
'com.github.ulisesbocchio:spring-boot-jar-resources:jar:1.3'
<dependency org="com.github.ulisesbocchio" name="spring-boot-jar-resources" rev="1.3">
  <artifact name="spring-boot-jar-resources" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ulisesbocchio', module='spring-boot-jar-resources', version='1.3')
)
libraryDependencies += "com.github.ulisesbocchio" % "spring-boot-jar-resources" % "1.3"
[com.github.ulisesbocchio/spring-boot-jar-resources "1.3"]

Dependencies

compile (3)

Group / Artifact Type Version
com.google.guava : guava jar 20.0
org.apache.commons : commons-vfs2 jar 2.1
commons-io : commons-io jar 2.5

provided (3)

Group / Artifact Type Version
org.springframework : spring-core jar
org.slf4j : slf4j-api jar
org.projectlombok : lombok jar 1.16.6

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Build Status Gitter Maven Central

spring-boot-jar-resources

When using Spring Boot out of the box, resources from classpath are jarred, and while they can be accessed through input streams, they cannot be accessed as Files. Some libraries require Files as input instead of input streams or Spring Resources. This library deals with that limitation by allowing you to do resource.getFile() on any jarred resource. It does so by extracting the files from the jar to a temporary location transparently to you.

How to use this library?

Simply add the following dependency to your project:

<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
	<artifactId>spring-boot-jar-resources</artifactId>
	<version>1.3</version>
</dependency>

And the following configuration to your Spring Boot app:

new SpringApplicationBuilder()
            .sources(Application.class)
            .resourceLoader(new JarResourceLoader())
            .run(args);

Alternatively, provide a path to the JarResourceLoader where jarred resources will be extracted when accessed through a File handle.

new SpringApplicationBuilder()
            .sources(Application.class)
            .resourceLoader(new JarResourceLoader("/path/to/extract"))
            .run(args);

If you want to expose the path to be configurable, since version 1.2 you can do this:

public static void main(String[] args) {
        StandardEnvironment environment = new StandardEnvironment();
        new SpringApplicationBuilder()
            .sources(SpringBootJarResourcesDemoApplication.class)
            .environment(environment)
            .resourceLoader(new JarResourceLoader(environment, "resources.extract.dir"))
            .build()
            .run(args);
    }

With this you can run you app.jar this ways:

  • java -Dresources.extract.dir=/some/path -jar app.jar
  • java -jar app.jar --resources.extract.dir=/some/path
  • export RESOURCES_EXTRACT_DIR=/some/path && java -jar app.jar

Or put resources.extract.dir in application.properties

Basically this new constructor takes the environment from which the property (with the name provided, i.e. resources.extract.dir) will be retrieved to get the extract directory. Notice that we initialize a StandardEnvironment on the first line of the main method, that we also provide to the SpringApplicationBuilder.environment(ConfigurableEnvironment) method so that Spring can populate this object. That same environment is also passed as first argument to the JarResourceLoader constructor. This is required so that both Spring and the JarResourceLoader can share the same properties.

Demo App

For more information and sample implementation check out the Demo App

How this library works?

Internally, this library simply wraps existing resources loaded by DefaultResourceLoader with a custom JarResource implementation that deals with the details of extracting the resource from the Jar. The implementation only extracts resources from jars if they need to be extracted, i.e. if actually being inside a jar. If for some reason, such as when running within an IDE or using an absolute path to load resources, the resources are not inside a jar, then the actual file is used instead.

Versions

Version
1.3
1.2
1.1
1.0