0

假设我有一个 Maven 多模块 Java EE 6 应用程序:

foobar/
    foobar-ear/
        src/
            main/
                application/
                    META-INF/
                        MANIFEST.MF
                        glassfish-application.xml
                        glassfish-resources.xml
                filters/
                    dev.properties
                    prod.properties
                    test.properties
        pom.xml
    foobar-web/
    foobar-ejb/
    pom.xml

如您所见,我正在使用资源过滤。foob​​ar-ear 中的 pom 定义了构建配置文件:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
    <profile>
        <id>demo</id>
        <properties>
            <env>demo</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>

并且每个子项目的 pom 都定义了资源过滤。在 foobar-ear/pom.xml 中:

<build>
    ...

    <filters>
        <filter>src/main/filters/${env}.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/application</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/application</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.xml</exclude>
            </excludes>
        </resource>
    </resources>

    ...
</build>

此处的目的是过滤 foobar-ear/src/main/application/META-INF/glassfish-resources.xml,其中包含过滤器属性文件中定义的 JDBC 和 JavaMail 配置项的占位符。

当我使用 构建时mvn -Pdev clean install,我在 foobar-ear 中得到这个输出:

foobar-ear/
    target/
        classes/
            META-INF/
                MANIFEST.MF
                glassfish-application.xml
                glassfish-resources.xml    <- This one is filtered
        foobar-ear/
            META-INF/
                MANIFEST.MF
                glassfish-application.xml
                glassfish-resources.xml    <- This one is NOT filtered
            lib/
            foobar-ejb.jar
            foobar-web.war
        application.xml
        foobar-ear.ear                     <- ear file. Contents same as foobar-ear/

如您所见,复制到通常输出位置的资源会被过滤,但 EAR 目录布局和 ear 文件中的资源则不会。

4

1 回答 1

0

我添加<filtering>true</filtering>maven-ear-plugin配置中,现在它可以工作了。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <filtering>true</filtering>
                [...]
            </configuration>
        </plugin>
        [...]
    </plugins>
    [...]
</build>

参考:过滤源

于 2012-06-02T05:28:40.463 回答