pconrey
pconrey

Reputation: 6005

Use Maven to Copy Files to WEB-INF Before WAR is Packaged

I have two servlet config files in my webapp, one for our normal environment (Heroku) and the other for WebLogic. I have a Maven profile for our WebLogic builds that should copy "servlet-context.xml.weblogic" to "servlet-context.xml". Everything appears to be working, except that the copy takes place AFTER the war file is built, so the correct servlet context doesn't get included in the package. What is the right build phase to use in the maven-antrun-plugin to get the copying done correctly?

Here is the relevant section of my POM.xml file:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
    <phase>test</phase>
    <goals>
      <goal>run</goal>
    </goals>
    <configuration>
      <tasks>
        <move
          file="${project.build.directory}/${project.build.finalName}/WEB-INF/spring/appServlet/radio-context.xml.weblogic"
          tofile="${project.build.directory}/${project.build.finalName}/WEB-INF/spring/appServlet/radio-context.xml"/>
      </tasks>
    </configuration>
    </execution>
  </executions>
</plugin>

This fails with the following error:

Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (default) on project radio: An Ant BuildException has occured: Warning: Could not find file C:\workspace\radio\target\radio-1.0.0-BUILD-SNAPSHOT\WEB-INF\spring\appServlet\radio-context.xml.weblogic to copy. -> [Help 1]

However, if I change the <phase> to package, the copy works, but after the war is built.

Any help would be appreciated.

Upvotes: 0

Views: 9067

Answers (2)

prunge
prunge

Reputation: 23248

As a reference, this page lists the Maven lifecycle phases.

The key phases you need to think about in your build are:

  • process-resources - where most of the files are placed into ${project.build.directory}
  • package - this is the phase where the WAR is built

BUT...

Looking at the documentation of the WAR plugin, the copying of WAR resources to ${project.build.directory}/${project.build.finalName} occurs in the war:war goal, which executes in the package phase also.

So let's take a step back. What you want to achieve is to use a different radio-context.xml file depending on your profile. Perhaps a better approach would be to configure the WAR plugin differently in your weblogic profile.

Have a separate webapp resource directory for your weblogic, put your custom radio-context.xml file in there, and only include that directory in the weblogic profile. e.g.

<project>
  ...

  <profiles>
    <profile>
            <id>weblogic</id>
            ...
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <webResources>
                                <resource>
                                    <directory>src/main/webapp-weblogic</directory>
                                </resource>
                            </webResources>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Upvotes: 2

rlp
rlp

Reputation: 46

Try using process-resources, this copies your file before the war is built.

Upvotes: 2

Related Questions