Search code examples
javamavenmaven-ear-plugin

maven-ear-plugin project property lookup fails


I have created simple ear project with filters. I want to use different settings for each environment, those settings should be passed to generated application.xml file in the form of env-entries. The generation of ear package is done with maven-ear-plugin as shown below:

        <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <generateApplicationXml>true</generateApplicationXml>
                <version>6</version>
                <envEntries>
                    <env-entry>
                        <env-entry-name>customProperty</env-entry-name>
                        <env-entry-type>java.lang.String</env-entry-type>
                        <env-entry-value>${custom.property}</env-entry-value>
                    </env-entry>
                </envEntries>
                <applicationName>${custom.property}</applicationName>
            </configuration>
        </plugin>

To do that I had to use another plugin properties-maven-plugin. It successfully reads properties from file and sets them as maven project properties, so I can insert them in pom.xml file using ${}. It works for most of the pom.xml elements (i.e. <applicationName>, unfortunately it is not successfully looked up when I place it inside env-entry element, where I need it. Below is generated application.xml.

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
    <application-name>default property</application-name>
    <display-name>test</display-name>
    <env-entry>
        <env-entry-name>customProperty</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>${custom.property}</env-entry-value>
    </env-entry>
</application>

It's probably a bug which should be issued at Maven Ear Plugin, but I don't have an account there. I'm also attaching archived maven project, if somebody would like to check that himself: test.zip.

Edit

I've managed to overcome this problem using maven-resource-plugin and filter application.xml file after it's created as user @skegg99 proposed. Since I cannot replace this file I had to copy it to META-INF directory. I does not look pretty, I know, but it solves the case for now. Here is additional markup for maven-resource-plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <outputDirectory>${basedir}/target/${project.artifactId}-${project.version}/META-INF</outputDirectory>
                <filters>
                    <filter>src/main/filters/${env}.properties</filter>
                </filters>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

and also here:

    <resources>
        <resource>
            <directory>${basedir}/target</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.xml</include>
            </includes>
        </resource>
    </resources>

Whole project configuration can be downloaded from here.


Solution

  • With the current setup you have this may well be impossible.

    I just took a brief look into the code where it states that the env entries are load through the PlexusConfiguration.

    Without diving too deep into that code as well, I can't see that this part handles the entries any more special than "read XML, put into List".