Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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?
like image 906
Tuukka Mustonen Avatar asked Aug 24 '10 15:08

Tuukka Mustonen


People also ask

What is true regarding the src main resources in Maven?

The folder src/main/resources is supposed to contain the resource files like xmls and property files and is normally created at the time of project creation. Therefore, you should add the resources under src/main/resources path.

What goes in src main resources?

src/main/resources – configuration files and others such as i18n files, per-environment configuration files, and XML configurations. src/main/webapp – for web applications, contains resources like JavaScript, CSS, HTML files, view templates, and images.

Where is war file in Maven project?

Now, once we execute the mvn install command, the WAR file will be generated inside the target folder. Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory.


1 Answers

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.

like image 194
Pascal Thivent Avatar answered Sep 27 '22 17:09

Pascal Thivent