Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a resource or another in Maven depending on the target environment?

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?

like image 925
Spring Monkey Avatar asked Feb 04 '09 17:02

Spring Monkey


People also ask

What is Maven resource filtering?

Resource Filtering. You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

What is the use of resources tag in POM XML?

Via the resources area in the pom you can filter files from their way src/main/resources to the target/classes folder. The lifecycle of Maven is not influenced by this. I have added resources successfully in . Jar file.


2 Answers

  • 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

like image 126
Matthew McCullough Avatar answered Oct 09 '22 18:10

Matthew McCullough


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> 
like image 26
Spring Monkey Avatar answered Oct 09 '22 17:10

Spring Monkey