Selecting the certain resource files into WAR from the default src/main/resources location with Maven
Asked Answered
P

2

7

I am trying to control which files make into the WAR package that is created by mvn package goal. Specifically, I want to exclude some files from the default src/main/resources folder for each package (I am trying to do builds/package for different environments).

I tried using maven-war-plugin but failed. If I add this configuration (for testing):

<webResources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>WEB-INF/classes</targetPath>
        <excludes>
            <exclude>*.xml</exclude>
        </excludes>
    </resource>
</webResources>

...my WEB-INF/classes will still contain the XML files. This is because webResources parameter seems to duplicate the copying process (the above configuration actually works, an files are not copied... but they get copied in some other process instead).

Maven-war-plugin documentation states:

The default resource directory for all Maven 2 projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.

The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.

This is a bit confusing. Does it mean that:

  • The webResources parameter is a feature in maven-war-plugin that allows files to be included only from outside src/main/resources folder? If so, how can we alter the copied files from inside src/main/resources?
  • The webResources parameter is a feature in maven-war-plugin that allows files to be included also from outside src/main/resources folder? If so, how can it be configured to do this?
  • Some other option?
Pyrology answered 24/8, 2010 at 15:36 Comment(1)
Are you trying to build for different environments with the same mvn package or do you control that by using profiles ?Pernell
B
13

I am trying to control which files make into the WAR package that is created by mvn package goal. Specifically, I want to exclude some files from the default src/main/resources folder for each package

Resources from the default resource directory (src/main/resources) are copied into the build output directory (target/classes) during the process-resources phase. Then, during the package phase, the content of target/classes is taken and packaged into a distributed archive, like a WAR, and end up in WEB-INF/classes in that case.

So, if you want to control what resources end up in WEB-INF/classes, you need to control what resources will end up in target/classes i.e to somehow alter the behavior of the goal bound to the process-resources phase, specifically the resources:resources goal of the Maven Resources Plugin.

And to do so, (this is probably not intuitive), you can declare exclude or include elements inside resources element of the pom, as shown in Including and excluding files and directories. Applied to the default resource directory:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <excludes>
        <exclude>**/*.xml</exclude>
      </excludes>
    </resource>
  </resources>
</build>

And if you want to do use custom exclusion rules for different environments, combine this with profiles. For example:

<project>
  ...
  <profiles>
    <profile>
      <id>env-uat</id>
      <activation>
        <property>
          <name>env</name>
          <value>uat</value>
        </property>
      </activation>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <excludes>
              <exclude>**/*.xml</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
</project>

And when using this profile, the xml files won't end up in target/classes and consequently they won't end up in WEB-INF/classes in the final war.

I tried using maven-war-plugin but failed. If I add this configuration (for testing) (...) my WEB-INF/classes will still contain the XML files

What you're doing here is adding an additional resources directory, which appears to be the already included default resource directory. So, whatever you'll exclude, this has no effect since the files get copied to target/classes during process-resources anyway and thus still end-up in WEB-INF/classes.

In other words, use webResources when you want to add extra resources that are not part of the default resource directory:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>extra-resources</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

But I don't think that this is what you're looking for here and suggest using the approach suggested above.

Brythonic answered 25/8, 2010 at 5:29 Comment(4)
I actually tried excluding files in the "global" resources element. The result was that the excluded files didn't get into target/classes, however, they still got into target/mypackage-1.0.0-SNAPSHOT. But I will check this again just to make sure, maybe I made some typo/mistake there :/Pyrology
@Tuukka Hmm... Yes, please, double check (with a clean build).Brythonic
@ Pascal: after re-trying it that way, it seems to be working. Maybe I did a mistake earlier, being a bit confused about the whole process :/ So your suggestion is definitely the right one. I still have a problem of getting Compilation failure due to illegal start of expression because Maven doesn't "recognize" annotations I use in tests. However, this seems like a common Maven issue and not related to exclusion of files by any mean. I will try to resolve that next :) Thanks for your support!Pyrology
Fortunately, the compilation issue was quick to resolve, it occured due to this: jroller.com/peter_pilgrim/entry/eclipse_compiler_versus_the_sun. So I managed to verify that the packaging goes now as supposed, files are excluded as supposed. Picking this as the accepted answer.Pyrology
P
0

See Pascal's answer for right configuration and good explanation of the process. I just give a direct answer to this question here:

  • The webResources parameter is a feature in maven-war-plugin that allows files to be included only from outside src/main/resources folder? If so, how can we alter the copied files from inside src/main/resources?
  • The webResources parameter is a feature in maven-war-plugin that allows files to be included also from outside src/main/resources folder? If so, how can it be configured to do this?

It's the former, the webResources parameter in maven-war-plugin only allows one to include extra resources from outside src/main/resources. It does not allow one to configure includes/excludes for src/main/resources itself.

Pyrology answered 25/8, 2010 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.