Dudi
Dudi

Reputation: 2430

How to make maven put resource files of a dependancy in parent war and not dependancy jar

I have two projects A and B. B is dependant on A.

A has some resource files.

When using mvn install on B, it uses the resources that were packaged in A.jar (as that was built previously) I would like the resources to be packaged in B so I could edit them easily. (Especially since B is deployed as WAR)

Copying the resources to project B might not help since the code in A uses the following to retrieve info

InputStream stream = this.getClass().getResourceAsStream(
                "/resourceFile.properties");

do I need to change the path to relate to project B? if so how should one do it?

What is the best approach for handling this?

Upvotes: 1

Views: 52

Answers (3)

Dudi
Dudi

Reputation: 2430

Easiest way I've found it to add this to your B project POM

<build>
....
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>../A/src/main/resources</directory>
            </resource>

        </resources>
... 
</build>

Upvotes: 1

Brett Okken
Brett Okken

Reputation: 6306

As long as project B is in the same classpath as project A, that code will continue to work.

It may be clearer, however, to use ClassLoader.getResoucreAsStream rather than Class.getResourceAsStream. The method on Class by default looks for resources relative to the package name of the Class. You have provided an absolute path (leading '/'), indicating that the package relative logic is not needed.

Upvotes: 0

mystarrocks
mystarrocks

Reputation: 4088

You could use the maven-shade-plugin's shade mojo to repackage select artifacts from your project A into B. I haven't entirely looked at the jarjar-maven-plugin, but it could also assist you in doing that.

Look at the "Relocating classes" section of the shade plugin documentation in particular to understand how to do it. It should work equally well with resource files in theory.

Your code will work as it always does - it does not matter whether the resource being loaded is in the same jar or in a different jar in the B's classpath or inside B's WEB-INF/lib as long as it's in the classpath and referred by the right path in your code.

It's good that you didn't consider moving the property file into B project's codebase - had you done that, A would no longer be self-contained.

Upvotes: 0

Related Questions