【问题标题】:What is the proper way to replace files in Maven?在 Maven 中替换文件的正确方法是什么?
【发布时间】:2016-03-22 16:01:23
【问题描述】:

我的 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>

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

【问题讨论】:

  • 所有定义的配置文件都依赖于 ${id} 属性,它是否在 POM 的其余部分中定义?
  • 我的错误是 ${id} 占位符值是定义的应用程序的全名,例如 [groupId:artifactId:version] 而不是配置文件 ID 名称。 :)

标签: java maven


【解决方案1】:

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

<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>

【讨论】:

  • 我有一个问题,web.xml 文件何时更改?示例:web.dev.xmlweb.preprod.xmlweb.prod.xml
  • 'mvn clean install -P dev' - 在控制台中输入的这个命令使用开发配置文件启动 Maven 生命周期。您可以通过“-P profile”指定配置文件。
  • 我们可以用它来覆盖我的一个java类吗? stackoverflow.com/questions/55869228/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
  • 2011-01-24
  • 1970-01-01
相关资源
最近更新 更多