Reputation: 14550
How can I replace the content of a file just before maven-war-plugin uses it to build a war?
the point is:
so the question is: how can i replace file's content during (or just before) building war
Upvotes: 5
Views: 4695
Reputation: 14550
i found maven replacer plugin it easily allows change file content
Upvotes: 0
Reputation: 48075
I will give you a completely different answer here that will make it possible to have two different files with their own configuration that is then filtered to a single 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 only have the application.properties
.
Upvotes: 3
Reputation: 48075
You can elaborate a little with the maven-resources-plugin
to accomplish this.
First of all you will have to put your special file in one directory in the src/main/resources
folder.
Then you place an empty file under 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>
With this configuration you will get different files in the target/classes
folder depending on what goal you use.
mvn test
Will put the src/main/resources/dev/application.properties
file in the target/classes
.
mvn package
Will put the src/main/resources/prod/application.properties
file in the target/classes
.
Any other resources under src/main/resources
will be copied and/or filtered as usual.
Upvotes: 1
Reputation: 1986
Try the
https://maven.apache.org/plugins/maven-assembly-plugin/
But its an anti-pattern. Better you use context-params and jndi-resources from the AppServer.
Upvotes: 0