Gray
Gray

Reputation: 116828

How to exclude certain resources from the maven war plugin war file?

We've been using war:exploded for a while now and I'm trying to do away with it. I've added the following to my pom:

<resource>
    <directory>src/main/webapp</directory>
</resource>

This copies files such as the in src/main/webapp/keystore into target/classes/keystore so my local UI launcher works and sees everything. Score!

However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong). It also means there there is a WEB-INF/classes/WEB-INF directory (blah). I'm trying to exclude the resource files from src/main/webapp resource since src/main/webapp/WEB-INF is already a resource.

I'm trying not to specifically exclude keystore and other files since we add/delete from that list semi-often. I've tried to add the following (and a number of other variants) to the war plugin configuration without results:

<webResources>
   <resource>
      <directory>src/main/webapp</directory>
      <excludes>
         <exclude>**</exclude>
      </excludes>
   </resource>
</webResources>

I've also read a number of other SO questions and I've spent at least an hour reading docs on from the maven war plugin page and tried other configs without success.

Any idea what magic I need to do here? Thanks in advance.

Upvotes: 11

Views: 27057

Answers (3)

Bibin Chorickanpara
Bibin Chorickanpara

Reputation: 81

warSourceExcludes worked for me. for example.To exclude js folder and scss folder from my war i tried...

 <configuration>
   <warSourceExcludes>js/*.*,scss/*.*</warSourceExcludes>      
   <webResources>
     <resource>
     <directory>src/main/webapp</directory>
     <filtering>true</filtering>
        <includes>
            <include>WEB-INF/web.xml</include>
        </includes>
     </resource>
   </webResources>
</configuration>

Upvotes: 5

Gray
Gray

Reputation: 116828

However, by adding to the resources list, this means that the same files also show up in the war file as keystore (correct) and WEB-INF/classes/keystore (wrong).

I was not able to get the war-plugin to properly exclude the webapp directory while at the same time allowing the webapp/WEB-INF directory. Instead of adding the following to the pom:

<!-- bad idea, this didn't work -->
<resource>
    <directory>src/main/webapp</directory>
</resource>

I switched to using the resources-plugin to be able to copy certain resources over during the validate phase:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/webapp</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/webapp</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

I initially copied the webapp directory into target/classes but that also seemed to make it into the resulting war file which was what I was trying to avoid. I have the feeling that I could have solved this if necessary.

Instead I decided to copy the webapp files into the target/webapp directory and change my program to look for the resources there:

String RESOURCE_BASE = "target/webapp";
System.setProperty("server.jetty.webapp.resourceBase", RESOURCE_BASE + "/");
System.setProperty("server.jetty.ssl.keystore", RESOURCE_BASE + "/keystore");
System.setProperty("server.jetty.ssl.truststore", RESOURCE_BASE + "/keystore");
...

This seems to be working well. Testing locally now works (without war-exploded!!) and the files are not doubly included in the resulting war file. In addition, I added a symlink at runtime from target/webapp/views/cms to point to our CMS hierarchy which is in another project.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

You need

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <packagingExcludes>**/keystore</packagingExcludes>
    </configuration>
</plugin>

Also See

Upvotes: 14

Related Questions