0

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.

pconrey
  • 5,805
  • 7
  • 29
  • 38

2 Answers2

3

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>
prunge
  • 22,460
  • 3
  • 73
  • 80
  • 1
    This seems like it would be a good approach, but the directory would need to be src/main/webapp-weblogic/WEB-INF/spring/appServlet. Would this just copy the file in question, keeping the rest of the webapp directory in tact, or would I have to create a duplicate of everything in src/main/webapp? – pconrey Sep 27 '12 at 17:39
2

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

rlp
  • 46
  • 5
  • I tried this approach, but the original file is not there yet, so the copy fails. If I try to copy from the src/main/webapp tree, they the copy is overwritten during the "package" phase. – pconrey Sep 27 '12 at 17:51