configurationmaven-2release-builds

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


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:


Solution

  • 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.