java - 在构建 war 期间替换文件内容(maven2)

标签 java maven build

如何在 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/

相关文章:

maven - m2eclipse 说 "Missing artifact"但我可以从命令行构建!

android - 无法在 Flutter 中构建 .aab

svn - Subversion - 时间戳发生变化,但内容不变

android - 如何修复构建gradle

java - 我在 Android Studio 中使用 Wea​​therstack API 在 Retrofit 中得到空值

java - 使用左连接在 postgres 数据库中进行 SQL 查询

java - 从 bash 脚本中将 Spring Boot 作为前台进程启动

java - @EnableSpringConfigured 导入

java - 由于来自单元测试的日志文件,Jenkins Maven 发布失败

java - 使用 HttpClient 4.0.1 使用 x509 证书进行相互身份验证