Search code examples
mavenmaven-resources-plugin

Why are my resources not getting copied in the "validate" phase?


I am trying to make sure that the correct resources are moved to WEB-INF/config/ no matter if I call

mvn gcloud:deploy

or

mvn tomcat7:run 

which is why I am enforcing -Dpackage-mode=<value> to be set. I am using the maven-resource-plugin for the copy part like this:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/assembly/${package-mode}/config</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

However, if I run

mvn validate tomcat7:run -Dpackage-mode=dev 

for starting the server the files are not getting copied.

E:\java\mz\mz-server\mz-web-server>mvn validate tomcat7:run -Denv=dev -Dpackage-mode=dev
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mz-web-server 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-maven) @ mz-web-server ---
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-property) @ mz-web-server ---
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ mz-web-server >>>
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-maven) @ mz-web-server ---
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-property) @ mz-web-server ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mz-web-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mz-web-server ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ mz-web-server <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ mz-web-server ---
[INFO] Running war on http://localhost:8080/
[INFO] Using existing Tomcat server configuration at E:\java\mz\mz-server\mz-web-server\target\tomcat
[INFO] setting SystemProperties:
[INFO]  gwt.codeserver.port=9876
[INFO] create webapp with contextPath:
Jun 03, 2016 5:24:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 03, 2016 5:24:57 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jun 03, 2016 5:24:57 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
...

What am I doing wrong here? I actually don't know if validate is a good choice here and I am not really bound to a certain phase. All I want is to make sure that the configuration files are getting copied according to package-mode.

But:

If I just run

mvn validate -Dpackage-mode=dev 

then everything is working as expected - the files are getting copied.


Solution

  • You shouldn't use the maven-resources-plugin to copy webapp resources, this plugin is only for application resources. Furthermore, you should never generate or copy files under src, like what you're doing with <outputDirectory>src/main/webapp/WEB-INF/config</outputDirectory>. Generated files should always be placed under the target folder.

    Instead, use the maven-war-plugin. You can configure it to filter a specific directory and output it in the WAR like this:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.6</version>
      <configuration>
        <webResources>
          <resource>
            <directory>src/main/assembly/${package-mode}/config</directory>
            <filtering>true</filtering>
            <targetPath>WEB-INF/config</targetPath>
          </resource>
        </webResources>
      </configuration>
    </plugin>
    

    This will filter the directory src/main/assembly/${package-mode}/config and place the files under it in the target folder WEB-INF/config in the WAR.

    With this change, you will be able to run your webapp with mvn clean tomcat7:run-war (and not tomcat7:run since that wouldn't package the WAR).