wax_lyrical
wax_lyrical

Reputation: 942

Maven Shade External Properties file relative to final Jar

Hi all!!

Sorry if this is a dumb question, but I'm new to Maven and have reached an impasse!

  1. I have a project which uses MyTest.properties, MoreMyTest.properties.
  2. I use the Maven Shade plugin to build this project into a .jar file, which is working fine! Unfortunately, the shade plugin is packaging MyTest.properties inside my jar file.

Then, I get this error when I try to execute the jar.

java.io.FileNotFoundException: Unable to locate: properties/MyTest.properties at file:\C:\Dev\test.jar!\properties\MyTest.properties at ...

I'm wanting to access my properties both inside my IDE, and by running the jar from the command line when I deploy. I was thinking of having my properties files in a relative folder ../lib/ above the location of my jar file.

Inside my program I want to access the properties like so:

File testProperties = new File(
  ClassLoader.getSystemResource("properties/MyTest.properties").getFile()
);

I tried adding this to my POM.xml

<transformer
    implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
  <resource>*.properties</resource>
  <file>../*properties</file>
</transformer>

(Sorry the formatting got messed on the above code snippet!)

But, it's not working. I'd really appreciate any help in this. I've not posted all code as the code is really big, but I hope you can get an idea of what I'm trying to achieve.

Many thanks, Paul

Upvotes: 1

Views: 2831

Answers (1)

wax_lyrical
wax_lyrical

Reputation: 942

Ok, I have a solution. Always the way, just after posting! But thanks for your reply.

  <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>../lib</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Upvotes: 2

Related Questions