如何在maven-war-plugin使用它来构建战争之前替换文件内容?
重点是:
所以问题是:如何在建立战争期间(或之前)替换文件的内容
答案 0 :(得分:3)
我将在这里给你一个完全不同的答案,这样就可以让两个不同的文件拥有自己的配置,然后过滤到一个文件。
. ├── 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}
这将为您提供三个文件。输出只有application.properties
。
答案 1 :(得分:1)
您可以使用maven-resources-plugin
详细说明一下。
首先,您必须将您的特殊文件放在src/main/resources
文件夹中的一个目录中。
然后将空文件放在src/main/resources
文件夹中的另一个文件夹下。
这只是一个例子:
. ├── 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>
使用此配置,您将在target/classes
文件夹中获取不同的文件,具体取决于您使用的目标。
mvn test
将src/main/resources/dev/application.properties
文件放在target/classes
。
mvn package
将src/main/resources/prod/application.properties
文件放在target/classes
。
src/main/resources
下的任何其他资源都会照常复制和/或过滤。
答案 2 :(得分:0)
试试
https://maven.apache.org/plugins/maven-assembly-plugin/
但它是一种反模式。最好使用AppServer中的context-params和jndi-resources。
答案 3 :(得分:0)
我发现maven replacer plugin它很容易允许更改文件内容