How can i put my resources in the deployed WAR in a multi-module web project

Ok, so i have a multi-module web project (Spring, Hibernate and all that) consisting out of a WAR and 3 JARs. 2 of those JARs are optional extensions of the core WAR and JAR. The extentions are used by maven profiles which add them as dependencies or not. This all works fine.

However while installing the project on a Tomcat server i noticed i coulden't edit the property files of the extension modules since they are packaged in a JAR file. So i'm looking for a way to have my property files moved from my extension JAR to my deployable WAR's resources folder when i package my project using maven. This means that the resources will ONLY be in the WEB resources folder when the extension is activated.

How can i best do this?

EDIT-01

Ok so i've tried using the build-helper-maven-plugin which qza suggested.

test-war POM:

<project>
...    
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>../extTestA-jar/src/main/resources</directory>
                                <targetPath>resources</targetPath>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

My project layout is as follows:

<Parent>
--><extTestA-jar>
--><extTestB-jar>
--><test-jar>
--><test-war>

With my resources in "src/main/resources"

Can anyone spot the problem in this? I've tried with and without the targetPath but none seem to work. I've also tried to do it at the generate-resources phase but still no luck. I was thinking the path could be wrong so i tried some others aswel but still no luck.

Answers


One solution is to use Maven Build Helper Plugin and executing goal:

add-resources

Complete example would be:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <id>add-resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>add-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>../data-module/src/main/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Another way would be to use Maven Resources Plugin and goal

copy-resources

For this plugin, usage goes simillar to this:

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
    <execution>
        <id>copy-resources</id>
        <phase>generate-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>src/main/resources</outputDirectory>
            <overwrite>true</overwrite>
            <resources>
                <resource>
                    <directory>../data-module/src/main/resources</directory>
                    <includes>
                        <include>*.properties</include>
                    </includes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>

Both plugins can help with tasks like this one.


It's better to read properties from outside the project. Create a configuration folder for your application server and define it in server start script such as:

-Dconfiguration.dir=/etc/tomcatConf

Then put your configuration files/folder under that folder and scan those with spring property placeholder bean:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:${configuration.dir}/module1/auth.properties</value>
        </list>
    </property>
</bean>

Now, when you change a property, you will need to only restart the server, no recreation of war/jard is needed.

Note that you don't need to have a system argument. It's for keeping a standart configuration directory for your application server. You can just use:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:/etc/conf/auth.properties</value>
        </list>
    </property>
</bean>

Need Your Help

Parsing JSON with GSON

android json gson

I'm having some trouble with GSON, mainly deserializing from JSON to a POJO.

In ASP.Net C# would the Application end when and error occurs on the site or when Application_Error is triggered

c# asp.net

Hello brilliant guys on Stackoverflow, please I would like to know if in ASP.Net C# would the Application end when an error occurs or when Application_Error is triggered? From my understanding, it