tags:

views:

7244

answers:

4

I have to create test war and production war, which will have a different log4j.properties file in the WEB-INF directory. I have these files log4j.properties (test war) and dev.log4j.properties (for production enivorment).

How to copy the dev.log4j.properties file into log4j.properties file for production war?

+4  A: 

I solved this using the maven-resources plugin, where i created the prod directory which has the resources for production environment and copied them to WEB-INF/classes directory in process-resources phase.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-resources-plugin</artifactId>
 <version>2.3</version>
 <executions>
  <execution>
   <id>copy-prod-resources</id>
   <phase>process-resources</phase>
   <goals>
    <goal>copy-resources</goal>
   </goals>
   <configuration>
    <outputDirectory>webapp/WEB-INF/classes</outputDirectory>
    <resources>
     <resource>
      <directory>src/main/resources/prod</directory>
      <filtering>true</filtering>
     </resource>
    </resources>
   </configuration>
  </execution>
 </executions>
</plugin>
Spring Monkey
+10  A: 

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
      <resources>
        <resource>
          <directory>src/main/resources/dev</directory>
        </resource>
      </resources>
    </build>
  </profile>
  <profile>
    <id>prod</id>
    <build>
      <resources>
        <resource>
          <directory>src/main/resources/prod</directory>
        </resource>
      </resources>
    </build>
  </profile>
</profiles>

  • Build using the desired profile via: mvn install -Pdev or mvn install -Pprod
Matthew McCullough
This seems much elegant than what i had before!!
Spring Monkey
So glad to hear it. Maven may seem tricky, but when used along best practice lines, folks like you are heard to say "beautiful!"
Matthew McCullough
Very useful. For a slightly more complex version you can add extra "resource" directories: <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources/common</directory> </resource> <resource> <directory>src/main/resources/dev</directory> </resource> </resources> </build> </profile>
John Meagher
This is the best answer when you have the ability to simply take all of the contents of one of the alternatives, but there are times when one needs to start with a base resources folder, and then merge in overrides. In that case Build Monkey's solution, along with some filters in the plugin config is a good way to go.
StevenC
A: 

The code above didn't work for me - had to change it to the following:

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-prod-resources</id>
      <phase>process-resources</phase>
      <goals>
         <goal>copy-resources</goal>
      </goals>
      <configuration>
        <!-- this is important -->
        <overwrite>true</overwrite>
        <!-- this as well (target/ was missing) -->
        <outputDirectory>${basedir}/target/classes</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/resources/prod</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>
stephanos
A: 

Last response is working. But you need to give the version to make it work.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <id>copy-prod-resources</id>
      <phase>process-resources</phase>
      <goals>
         <goal>copy-resources</goal>
      </goals>
      <configuration>
        <!-- this is important -->
        <overwrite>true</overwrite>
        <!-- target -->
        <outputDirectory>${basedir}/target/classes</outputDirectory>
        <resources>
          <resource>
            <!-- source -->
            <directory>src/main/resources/prod</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>
mylas