maven-2 - Maven 一次性构建多个配置文件

标签 maven-2 maven profiles

我们的政策是只构建 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>

......

谢谢, 普拉布约特

最佳答案

Maven 不像 ant。有了ant,基本上你想做什么就可以做什么。有了maven,就有了明确且有记录的build life cycle ,它的目标是构建一个组件(并可能将其他 Artifact 附加到构建中)。

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

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

关于maven-2 - Maven 一次性构建多个配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4932944/

相关文章:

maven-2 - Maven : download artifact and its deps to a specific directory

java - Netbeans "default config"用于 Maven 项目

java - Apache Cxf对不同的包提供不同的服务

java - 多种配置文件- Spring Bean 注入(inject)

java - 为什么 Maven 在生产构建中使用测试资源?

java - 如何使用 Cobertura 检测多项目 Maven 2?

java - Maven Web 服务 WSDL 生成不起作用

java - Maven 禁用插件

python - Django - 如何以最佳方式编写用户和配置文件处理?

javascript - 如何声明配置文件变量并将其集成到JS文件中?如何排除/包含构建功能?