如何在 maven-war-plugin 使用它构建 war 之前替换文件的内容?
重点是:
- 在 eclipse 和 mvn tomcat:run 中启动的 tomcat 我想使用开发配置。它应该在类路径上(例如在资源文件夹中)。所以文件应该在测试的所有阶段都存在,但不在打包阶段
- 在 bamboo、uat、prod 服务器上,我希望删除此配置。它将从类路径中提供
- 但我不想删除文件。我们使用共享服务器,因此任何人都可以将具有相同名称的文件放在类路径中(意外地,例如 application.properties)。所以我希望这个文件在 war 中(所以 spring 不会在 war 之外寻找它)但它应该是空的。
- 我不想使用配置文件,因为我希望在所有环境中使用相同的包
所以问题是:如何在 build war 期间(或之前)替换文件内容
最佳答案
我会在这里给你一个完全不同的答案,这将使两个不同的文件有自己的配置成为可能,然后过滤到一个文件。
. ├── 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
。
关于java - 在构建 war 期间替换文件内容(maven2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13045684/