Maven: If sentences in pom.xml in the property tag

Elyahu picture Elyahu · Aug 5, 2013 · Viewed 36.4k times · Source

I'd like to set a property if an environment variable is set. I googled a lot on it and all I found is something similar to the code below, but I keep getting the error:

[FATAL] Non-parseable POM Y:\Maven\parent-pom\pom.xml: TEXT must be immediately followed by END_TAG and not START_TAG (position: START_TAG s een ...roperties"\r\n
classpathref="maven.plugin.classpath" />... @29:55) @ line 29, column 55

That's the code I'm trying, its inside a pom.xml and I ran the command -

mvn --errors deploy

Of course, I'll be happy to get any other solution, if you have other suggestion on how to set a property in pom.xml depending on an environment variable content.

Thanks, Eli

    <distributionManagement>
       .....
    </distributionManagement>

    <properties>
          <tasks>
        <taskdef resource="net/sf/antcontrib/antcontrib.properties"
          classpathref="maven.plugin.classpath" />

        <if>
           <condition>
             <equals arg1="${env.WAS60_HOME}" arg2=""\>
           </condition>
           <then>
             <was60.home>${env.WAS60_HOME}</was60.home>
             <javac>${was60.home}/java/bin/javac</javac>
           </then>
        </if>

         <if>
           <condition>
             <equals arg1="${env.WAS85_HOME}" arg2=""\>
           </condition>
           <then>
             <was85.home>${env.WAS85_HOME}</was60.home>
             <javac>${was85.home}/java/bin/javac</javac>
           </then>
        </if>
      </tasks>
</properties>
    <profiles>
       <profile>
    <id>was.base.v60</id>
            <dependencies>
               <dependency>
                 ....
                  <systemPath>${was60.home}/java/jre/lib/xml.jar</systemPath>
               </dependency>
               .....
            </dependencies>
        </profile>
        <profile>
    <id>was.base.v85</id>
            <dependencies>
               <dependency>
                 ....
                  <systemPath>${was85.home}/java/jre/lib/xml.jar</systemPath>
               </dependency>
               .....
            </dependencies>
        </profile>
    </profiles>

Answer

Mark O&#39;Connor picture Mark O'Connor · Aug 5, 2013

A much better approach would be to use profile activations.

<profiles>
  <profile>
    <id>was.base.v60</id>
    <activation>
      <property>
        <name>env.WAS60_HOME</name>
      </property>
    </activation>
    <dependencies>
      <dependency>
        ....     
        <systemPath>${env.WAS60_HOME}/java/jre/lib/xml.jar</systemPath>
      </dependency>
      .....    
    </dependencies>
  </profile>
  <profile>
    <id>was.base.v85</id>
    <activation>
      <property>
        <name>env.WAS85_HOME</name>
      </property>
    </activation>
    <dependencies>
      <dependency>
        ....     
        <systemPath>${env.WAS85_HOME}/java/jre/lib/xml.jar</systemPath>
      </dependency>
      .....    
    </dependencies>
  </profile>
</profiles>

Update:

My preferred way to use profiles is to have a default set of properties in my POM and then override these on demand using profiles in my settings file.

This approach is easy to do explicitly by using the "-s" and "-P" commandline parameters:

mvn -s $PROJECT_SETTINGS -P myProfile ....

This approach is easy to maintain in Jenkins using Config File Provider plugin which enables a GUI for editing the various settings files I use for each project.

Update 2:

Here's an example of how I setup my builds. The POM contains a section with the default property values. And I setup one or more pfiles to over-ride these values:

<project>
  <properties>
     <my.property1>hello</my.property1>
     <my.property2>world</my.property2>
     ..
  </properties>
  ..

  <build>
    <profiles>
      <profile>
        <id>build_in_spanish</id>
        <properties>
          <my.property1>hola</my.property1>
          <my.property2>mundo</my.property2>
          ..
        </properties>
      </profile>
      <profile>
        <id>build_in_irish</id>
        <properties>
          <my.property1>dia dhuit</my.property1>
          <my.property2>an domhain</my.property2>
          ..
        </properties>
      </profile>
    <profiles>
  </build>
</project>

So in this example the build defaults to English. To run the build with the settings in Spanish

mvn -P build_in_spanish ...

Note:

  • Profiles can be in the POM or in a separated settings file. The latter approach gives the most flexibility.
  • Profiles can be explicitly enabled (using the "-P" command line option or by attempting to discover the environment it sits within. This is a design choice)