How to include/exclude resources in maven war package

4.7k views Asked by At

Within my MAVEN project I'm trying to build a war package with certain resources based on a profile (defined in my settings.xml).

pom.xml:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
            <!-- archiveClasses>true</archiveClasses Enable this line will remove compiled classes from package -->
            <!-- packagingExcludes>view/test/**,WEB-INF/classes/**</packagingExcludes Does not work -->
            <packagingExcludes>view/test/**</packagingExcludes>
            <webResources>
                <resource>
                    <directory>src/main/webapp/</directory>
                    <filtering>false</filtering>
                    <includes>
                        <include>**/**</include>
                    </includes>
                </resource>
                <resource>
                    <directory>resources/</directory>
                    <targetPath>WEB-INF/classes</targetPath>
                    <filtering>false</filtering>
                    <!-- excludes><exclude>**</exclude></excludes Does not work -->
                    <includes>
                        <include>ehcache.xml</include>
                        <include>${include.files}</include>
                    </includes>
                </resource>
            </webResources>
            <includeEmptyDirectories>true</includeEmptyDirectories>
        </configuration>
    </plugin>

My src/main/resources folder is empty, so in my package there are no config files that are not expected. But when I move my resources folder into src/main/resources then the profiles are not working anymore and the package always contains all files from the resources folder.

How to alter my pom.xml so that resources folder can be moved into src/main/resources as to my understanding that is where you store resources like configuration files etc ('best-practice')?

1

There are 1 answers

5
Henry Hoendervangers On BEST ANSWER

See if you can reorganize your resources in a way so that you can apply a filter to the resources and that most files are always present for every configuration. Suppose you have a properties file that need different values for different configurations. You can substitute the value with a variable like this:

url=${url}
mode=${mode}

Now you can use profiles to set the values for the files that need to be filtered and the ones that need to be excluded entirely for that configuration:

<profile>
  <id>production</id> 
     <build>
         <resources>
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>                  
                 <excludes>
                     <exclude>[non-resource file #1]</exclude>
                     <exclude>[non-resource file #2]</exclude>
                     <exclude>[non-resource file #3]</exclude>
                 </excludes>
             </resource>
         </resources>
     </build>      
     <properties>
         <url>www.something.com</url>
         <mode>production</mode>
     </properties>
 </profile>
 <profile>
     <id>development</id>
     <build>
         <resources>
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>                  
                 <excludes>
                     <exclude>[non-resource file #1]</exclude>
                     <exclude>[non-resource file #2]</exclude>
                     <exclude>[non-resource file #3]</exclude>
                 </excludes>
             </resource>
         </resources>
     </build>      
     <properties>
         <url>localhost:8080/something</url>
         <mode>development</mode>
     </properties>
 </profile>

Finally you can delete the webResources tag in your maven-war-plugin. It wil pick up src/main/resources now because it's a configured resource.