java - 在 Maven 中替换文件的正确方法是什么?

标签 java maven

我的 Maven 应用程序具有 3 个不同的配置文件,如下所示

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileVersion>DEV</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <profileVersion>1.0.0-RC1</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <profileVersion>1.0.0-Final</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>
    </profiles>

我有这样的 Maven 结构:

src/main/config/default/WEB-INF/web.xml

src/main/config/dev/WEB-INF/web.xml

src/main/config/test/WEB-INF/web.xml

src/main/config/prod/WEB-INF/web.xml

src/main/webapp/WEB-INF/

我的任务是在构建时将指定的 web.xml 设置到 webapp/WEB-INF 中,具体取决于指定的配置文件。如果未指定配置文件,则 web.xml 将从默认文件夹复制。

我有插件,但它不工作。

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-prod-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${project.build.outputDirectory}/classes/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/config/${webXmlfolder}/WEB-INF</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

有什么想法吗?我花了很多时间解决这个问题,现在我有点困惑了。

最佳答案

好的,现在一切正常。这是我的最终代码,有效:

<properties>
    <webXmlFolder>default</webXmlFolder>
    <profileVersion>defaultVersion</profileVersion>
</properties>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileVersion>DEV</profileVersion>
            <webXmlFolder>dev</webXmlFolder>
        </properties>
    </profile>

    <profile>
        <id>test</id>
        <properties>
            <profileVersion>1.0.0-RC1</profileVersion>
            <webXmlFolder>test</webXmlFolder>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <properties>
            <profileVersion>1.0.0-Final</profileVersion>
            <webXmlFolder>prod</webXmlFolder>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-web.xml</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${basedir}/target/classes/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/config/${webXmlFolder}/WEB-INF</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

关于java - 在 Maven 中替换文件的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34313348/

相关文章:

java - 无法使用 scala 构建 gRPC ManagedChannel

java - Spring 自动接线

java - 如何使用 SNAPSHOT 进行测试以及将 <pluginRepository> 放在哪里?

java - 简单的 Jackson XML 反序列化到 Java 不起作用

java - JNI 和 DLL : Two way interaction (callback)

eclipse - 两个基于 Resteasy 的 WAR,一个不扫描提供者?

java - 从命令行运行 Apache CXF 客户端

java - 解决错误: unmappable character for encoding UTF8

java - 文件和目录例程

java - 以编程方式确定哪个 Java 线程持有锁