Maven – Files got overwritten in maven project when building a war

mavenmaven-war-pluginyui-compressor

I'm building a web application project using maven, and packaging is set to "war". I also use YUI compressor plugin to compress javascript codes in the webapp directory. I've set up the YUI compressor like this:

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
        <phase>process-resources</phase>
        <goals>
            <goal>compress</goal>
        </goals>
        </execution>
    </executions>
    <configuration>
        <excludes>
        <exclude>**/ext-2.0/**/*.js</exclude>
        <exclude>**/lang/*.js</exclude>
        <exclude>**/javascripts/flot/*.js</exclude>
        <exclude>**/javascripts/jqplot/*.js</exclude>
        </excludes>
        <nosuffix>true</nosuffix>
        <force>true</force>
        <jswarn>false</jswarn>
    </configuration>
</plugin>

If I do: mvn process-resources, src/main/webapp will get copied over to target/webapp-1.0/ directory, and javacripts are compressed. However, when I run mvn install, all the compressed javascripts are overwritten, apparently the packaging process copies the content from main/webapp one time before building the war file.

How can I get around this?

Best Answer

As you noticed, the /src/main/webapp dir (aka warSourceDirectory) contents is not copied into the project dir for packaging until the war plugin executes during the package phase. When the war plugin completes the archive is already built; too late to modify those resources. If the .js files you want to compress were moved into another directory (outside of /src/main/webapp) then you could do something like the below.

To test, I created a ${basedir}/src/play directory with a couple of files in it. I used the resource plugin for the example; you'd replace that config with the YUI compressor plugin config you needed and simply add the <webResource> element to your war plugin config as shown below; more info in the war plugin examples. My war ended up with the additional files right where I wanted them.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>process-resources</phase>
      <goals><goal>copy-resources</goal></goals>
      <configuration>
        <outputDirectory>${project.build.directory}/tmpPlay</outputDirectory>
        <resources>
          <resource>
             <directory>${project.basedir}/src/play</directory>
             <includes>
                <include>**/*</include>
             </includes>
          </resource>
        </resources>
       </configuration>
    </execution>
  </executions>
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <executions>
    <execution>
      <id>default-war</id>
      <configuration>
        <webResources>
          <resource>
            <directory>${project.build.directory}/tmpPlay</directory>
            <targetPath>WEB-INF/yourLocationHere</targetPath>
            <includes>
              <include>**/*</include>
            </includes>
          </resource>
        </webResources>
      </configuration>
    </execution>
  </executions>
</plugin>
Related Topic