19

我们的政策是只构建 1 个可部署的 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>

......

谢谢, 普拉布约特

4

1 回答 1

23

Maven不像蚂蚁。有了ant,你基本上可以随心所欲地做你想做的事。使用 maven,有一个清晰且记录在案的构建生命周期,它的目标是构建一个组件(并可能将其他工件附加到构建中)。

然而,您计划做的是多次构建一个组件,但使用不同的参数。这不适合 Maven 生命周期。因此,您需要做的是使一些外部进程进行迭代并使用不同的参数重复调用 maven。

完成此操作的经典方法是使用 shell 脚本,但您也可以使用Maven Invoker从 Java 或 Maven 上下文启动单独的进程。

于 2011-02-08T12:42:27.450 回答