Replace file contents during builidng war (maven2)

How can I replace the contents of a file before the maven-war plugin uses it to create war?

point:

  • in tomcat started in eclipse and mvn tomcat: run, I want to use development configuration. it should be in the class path (for example, in the resource folder). therefore, the file should be at all stages before testing, but not during the packaging phase.
  • on bamboo, uat, prod server, I want this configuration to be deleted. it will be provided from classpath
  • but I do not want to delete the file. we use shared servers so that everyone can put a file with the same name in the classpath (by chance, for example, application.properties). so I want this file to be inside the war (so spring will not look for it outside the war), but it should be empty.
  • I do not want to use profiles because I want to have the same package that works in all environments.

so the question is: how can I replace the contents of a file during (or only earlier) building a war

+4
source share
4 answers

I found the maven replacer plugin , it easily allows you to modify the contents of the file

0
source

I will give you a completely different answer here, which will allow you to have two different files with their own configuration, which are then filtered into one file.

  .
 ├── pom.xml
 └── src
     └── main
         ├── java
         ├── webapp
         |  └── WEB-INF
         |  └── web.xml
         ├── filters
         |  ├── dev.properties
         |  └── prod.properties
         └── resources
             ├── application.properties
             └── other.properties

pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.stackoverflow</groupId> <artifactId>Q13045684</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>${project.artifactId}-${project.version}</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <id>dev-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application.properties</include> </includes> <filtering>true</filtering> </resource> </resources> <filters> <filter>src/main/filters/dev.properties</filter> </filters> </configuration> </execution> <execution> <id>prod-resources</id> <phase>prepare-package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <overwrite>true</overwrite> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application.properties</include> </includes> <filtering>true</filtering> </resource> </resources> <filters> <filter>src/main/filters/prod.properties</filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 

src/main/filters/dev.properties

 someProperty = foo someOtherProperty = bar 

src/main/filters/prod.properties

 someProperty = someOtherProperty = 

src/main/resources/application.properties

 someProperty = ${someProperty} someOtherProperty = ${someOtherProperty} 

This will give you three files to work with. The output will have only application.properties .

+3
source

You can figure out a little with the maven-resources-plugin to accomplish this.

First of all, you will need to put your special file in one directory in the src/main/resources folder.

Then you put the empty file in another folder in the src/main/resources folder.

This is just an example:

  .
 ├── pom.xml
 └── src
     └── main
         ├── java
         ├── webapp
         |  └── WEB-INF
         |  └── web.xml
         └── resources
             ├── prod
             |  └── application.properties
             └── dev
                 └── application.properties

pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.stackoverflow</groupId> <artifactId>Q13045684</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>${project.artifactId}-${project.version}</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <id>dev-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <resources> <resource> <directory>src/main/resources/dev</directory> </resource> </resources> </configuration> </execution> <execution> <id>prod-resources</id> <phase>prepare-package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <overwrite>true</overwrite> <resources> <resource> <directory>src/main/resources/prod</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>dev/**</exclude> <exclude>prod/**</exclude> </excludes> </resource> </resources> </build> </project> 

In this configuration, you will get different files in the target/classes folder depending on what purpose you are using.

 mvn test 

Place the src/main/resources/dev/application.properties file in target/classes .

 mvn package 

Place the src/main/resources/prod/application.properties file in target/classes .

Any other resources in the src/main/resources section will be copied and / or filtered, as usual.

+1
source

Try

 https://maven.apache.org/plugins/maven-assembly-plugin/ 

But its an anti-pattern. You better use context parameters and jndi resources from AppServer.

0
source

Source: https://habr.com/ru/post/1441759/


All Articles