maven profile在构建中使用了错误的属性

时间:2014-05-21 08:29:24

标签: maven properties profile

我有一个生成.war的pom文件,我想使用2个不同的配置文件:ots和wiblatt

每个配置文件都有一个属性(具有相同的名称),在jboss-web.xml中使用:mailabo.ear

两个配置文件看起来像这样

<profiles>
    <profile>
        <id>ots</id>
        <properties>
            <mailabo.ear>mailabo-ots-ear-${project.version}</mailabo.ear>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>ots</id>
                            <phase>package</phase>
                            <goals>
                                <goal>war</goal>
                            </goals>
                            <configuration>
                                <classifier>ots</classifier>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                                <filtering>true</filtering>
                                <targetPath>WEB-INF</targetPath>
                                <includes>
                                    <include>jboss-web.xml</include>
                                </includes>
                            </resource>
                        </webResources>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>wiblatt</id>
        <properties>
            <mailabo.ear>mailabo-wiblatt-ear-${project.version}</mailabo.ear>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>wiblatt</id>
                            <phase>package</phase>
                            <goals>
                                <goal>war</goal>
                            </goals>
                            <configuration>
                                <classifier>wiblatt</classifier>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                                <filtering>true</filtering>
                                <targetPath>WEB-INF</targetPath>
                                <includes>
                                    <include>jboss-web.xml</include>
                                </includes>
                            </resource>
                        </webResources>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这很好用:

mvn clean install -DskipTests -P ots => mailabo-web-1.0.1.9-SNAPSHOT-ots.war
where ${mailabo.ear} is correctly replaced with mailabo-ots-ear-1.0.1.9-SNAPSHOT

mvn clean install -DskipTests -P wiblatt => mailabo-web-1.0.1.9-SNAPSHOT-wiblatt.war
where ${mailabo.ear} is correctly replaced with mailabo-wiblatt-ear-1.0.1.9-SNAPSHOT

这不起作用:

mvn clean install -DskipTests -P ots,wiblatt
generates mailabo-web-1.0.1.9-SNAPSHOT-ots.war
generates mailabo-web-1.0.1.9-SNAPSHOT-wiblatt.war
BUT all occurrences of ${mailabo.ear} contain mailabo-wiblatt-ear-1.0.1.9-SNAPSHOT

我希望操作系统的 ots 配置文件包含特定于ots的属性

可以以某种方式解决这个问题吗? maven是否有一些限制,即使在不同的配置文件中,具有相同名称的属性在一个构建调用中不能共存?我可以用某种方式定义2个不同名称的属性,以及2个jboss-web.xml文件,每个属性使用一个属性,然后根据配置文件包含一个或另一个?

2 个答案:

答案 0 :(得分:0)

当你启用两个配置文件时,结果会被合并,所以基本上属性重叠只生成一个最终属性。

尝试执行mvn help:effective-pom -Pots,wiblatt以查看它实际打算执行的pom。

我使用替代解决方案将多个war文件打包在一个项目中,即Iterator Plugin。您也可能会觉得有帮助。

答案 1 :(得分:0)

我最终创建了2个jboss web xml文件,每个文件使用不同名称的属性,然后使用resources指令将web xmls移动到WEB-INF

<profiles>
    <profile>
        <id>ots</id>
        <properties>
            <mailabo.ots.ear>mailabo-ots-ear-${project.version}</mailabo.ots.ear>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>ots</id>
                            <phase>package</phase>
                            <goals>
                                <goal>war</goal>
                            </goals>
                            <configuration>
                                <classifier>ots</classifier>
                                <webResources>
                                    <resource>
                                        <directory>${basedir}/src/main/module-specific/ots/webapp/WEB-INF</directory>
                                        <targetPath>WEB-INF</targetPath>
                                        <filtering>true</filtering>
                                        <includes>
                                            <include>jboss-web.xml</include>
                                        </includes>
                                    </resource>
                                </webResources>
                                <archive>
                                    <manifest>
                                        <addClasspath>true</addClasspath>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>wiblatt</id>
        <properties>
            <mailabo.wiblatt.ear>mailabo-wiblatt-ear-${project.version}</mailabo.wiblatt.ear>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>wiblatt</id>
                            <phase>package</phase>
                            <goals>
                                <goal>war</goal>
                            </goals>
                            <configuration>
                                <classifier>wiblatt</classifier>
                                <webResources>
                                    <resource>
                                        <directory>${basedir}/src/main/module-specific/wiblatt/webapp/WEB-INF</directory>
                                        <targetPath>WEB-INF</targetPath>
                                        <filtering>true</filtering>
                                        <includes>
                                            <include>jboss-web.xml</include>
                                        </includes>
                                    </resource>
                                </webResources>
                                <archive>
                                    <manifest>
                                        <addClasspath>true</addClasspath>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>