Search code examples
mavenresourcesfilteringpom.xmlwro4j

Can I defer, in Maven, the resources filtering after the execution of a plugin?


I'm using wro4j for js and css aggregation. So, I work with my js files and in maven build time, wro4j executes and creates the unique js files I need.

There is one js file that has a couple of variables based on the profile from pom.xml (related to the environment, such as url to find something).

When I build the application, I can see inside the war file the replaced variables in the js file. But, when the plugin for wro4j make the aggregated js file, takes the files from webapps/js and not from target folder (which it is right), so, the aggregated js file has the expression variables instead of the replaced ones.

I want to defer the filtering execution to run after the wro4j plugin. It is possible?

These are the variables on the js file (called global.js):

var mvnProfileEnvName = "${environment.name}";
var mvnProfileFullSearchUrl = "${full.search.url}";

And I have the following code in pom.xml

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <environment.name>dev</environment.name>
      <full.search.url>url/for/dev</full.search.url>
    </properties>
  </profile>
  <profile>
    <id>test</id>
    <properties>
      <environment.name>test</environment.name>
      <full.search.url>url/for/test<full.search.url>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <environment.name>prod</environment.name>
      <full.search.url>url/for/prod</full.search.url>
    </properties>
  </profile>
</profiles>
<build>
  <resources>
    <resource>
      <directory>${basedir}/src/main/webapp/js</directory>
      <filtering>true</filtering>
      <includes>
        <include>global.js</include>
      </includes>
    </resource>
    <resource>
      <directory>${basedir}/src/main/webapp/wro</directory>
      <filtering>true</filtering>
    </resource>
  </resources>

  </plugins>
    <plugin>
      <groupId>ro.isdc.wro4j</groupId>
      <artifactId>wro4j-maven-plugin</artifactId>
      <version>${wro4j.version}</version>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <minimize>true</minimize>
        <jsDestinationFolder>${basedir}/src/main/webapp/wro/js/</jsDestinationFolder>
        <cssDestinationFolder>${basedir}/src/main/webapp/wro/css/</cssDestinationFolder>
      </configuration>
    </plugin>
  </plugins>
<build>

Solution

  • You could try setting <filtering>false</filtering> and then manually configure an execution of maven-resources-plugin to occur at a later phase, with filtering enabled. Place this after your wro4j plugin declaration:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>filter-resources</id>
          <phase>prepare-package</phase>
          <goals><goal>resources</goal></goals>
        </execution>
      </executions>
    </plugin>
    

    http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html