33

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?

aalku
  • 2,860
  • 2
  • 23
  • 44
Spring Monkey
  • 4,988
  • 7
  • 32
  • 34

5 Answers5

61
  • Use Maven profiles (http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  • Create a "dev" and "prod" profile, selecting an alternate set of resources for each profile. Make Dev active by default.

    <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

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
Matthew McCullough
  • 17,160
  • 7
  • 39
  • 38
  • This seems much elegant than what i had before!! – Spring Monkey Feb 24 '09 at 02:18
  • 2
    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 Feb 25 '09 at 03:26
  • Very useful. For a slightly more complex version you can add extra "resource" directories: dev true src/main/resources/common src/main/resources/dev – John Meagher Jun 04 '09 at 16:32
  • 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 Aug 20 '09 at 14:57
  • 2
    @StevenC in such case you can provide one additional directory with common resources and configure it in the main pom tag. Resources declared in profiles will be merged. This is the perfect answer for me since it works nice with gwt compiler plugin while other solutions failed (however these 'other solutions' works in other cases, without gwt plugin involved). – omnomnom Dec 09 '11 at 11:06
  • Isn't that a concern that you're putting credentials in your source? – monksy Aug 19 '14 at 22:52
10

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
  • 4,988
  • 7
  • 32
  • 34
5

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
  • 3,319
  • 7
  • 33
  • 47
3

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
  • 31
  • 2
  • I tried this solution but now maven is adding an additional prod directory to classes. My other directories like dev and test are created as well. What I am doing wrong here? – Gambo Jan 04 '12 at 11:19
0

The alternative way is to use maven-antrun-plugin

<build>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.7</version>
          <executions>
            <execution>
              <phase>validate</phase>
              <goals>
                <goal>run</goal>
              </goals>
              <configuration>
                <tasks>
                  <echo>build.env: ${build.env} </echo>
                  <delete file="src/main/resources/log4j.properties" />
                  <copy file="src/env/${build.env}/log4j.properties"
                        tofile="src/main/resources/log4j.properties" />
                </tasks>
              </configuration>
            </execution>
          </executions>
        </plugin>
    </plugins>
</build>

Assume resource files are in following structure:

src/
  env/
      dev/
          log4j.properties
      local/
          log4j.properties
      prod/
          log4j.properties

When do maven build, run the following commands per environment:

mvn package -Dbuild.env=dev
mvn package -Dbuild.env=local
mvn package -Dbuild.env=prod
Jonathan L
  • 9,552
  • 4
  • 49
  • 38