使用Maven一次构建多个配置文件

20

我们的策略是仅构建一个可部署的jar文件,所有特定于环境的配置都被分开保存,并在一次构建中将它们全部构建在一起。因此,在我们当前的Ant过程中,我们为每个环境都有一个属性文件,遍历它们,并为每个环境创建一组配置文件。

在我的当前POM XML中,我能够构建只提供命令行的一个配置文件。通过Maven,是否有可能实现这一点呢?

以下是POM.xml的相关部分:

<!-- Define profiles here and make DEV as default profile -->
<profiles>

    <!-- dev Profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <!-- qa Profile -->
    <profile>
        <id>qa</id>
        <properties>
            <env>qa</env>
        </properties>
    </profile>

    <!-- prod Profile -->
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>

</profiles>
...


<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>

    <executions>
        <execution>

            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>

            <configuration>

                <filters>
                    <filter>env/${env}.properties</filter>
                </filters>

                <outputDirectory>${project.build.directory}/config/${env}
                </outputDirectory>
                <resources>
                    <resource>

                        <filtering>true</filtering>

                        <directory>${basedir}/src/main/config/default
                        </directory>
                        <includes>
                            <include>*.xml</include>
                            <include>*.properties</include>
                        </includes>
                    </resource>

谢谢,Prabhjot


这里描述了解决此问题的方法: https://dev59.com/lGct5IYBdhLWcg3wIqHW - Sergey Ponomarev
1个回答

24

Maven和Ant不同。使用Ant基本上可以按照自己的意愿进行操作。而Maven则有清晰且有文档记录的构建生命周期,它旨在构建一个组件(并可能将其他工件连接到构建过程中)。

但您要做的是使用不同的参数多次构建一个组件。这无法符合Maven生命周期的要求。因此,您需要启动一些外部进程来迭代,并使用不同的参数反复调用Maven。

传统的解决方法是使用shell脚本,但您也可以使用Maven Invoker从Java或Maven环境中启动单独的进程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接