geoaxis
geoaxis

Reputation: 1560

setting a default property in maven3. Possible?

Is it possible to set a default property in maven3. I can fetch the CATALINA_HOME from bash by following

 <properties>
     <cargo.tomcat.home>${env.CATALINA_HOME}</cargo.tomcat.home>   
 </properties>

But if the variable is not set and I run the evaluate expression command I get null object

! mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=cargo.tomcat.home

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.1.1:evaluate (default-cli) @ cargo-selenium-springmvc-webapp ---
[INFO] No artifact parameter specified, using 'com.keybroker.spike:cargo-selenium-springmvc-webapp:war:1.0-SNAPSHOT' as project.
[INFO] 
null object or invalid expression
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.721s
[INFO] Finished at: Tue Jul 17 16:51:48 CEST 2012
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------

I would be interested in setting a default $CATALINA_HOME. I don't mind using another plugin but vanilla maven3 would be best. So what i am looking for

if $CATALINA_HOME is set by bash then use it
otherwise if the is a -DcatalinaHome argument passed to maven, then use that 
if all else fails, default to XYZ path for $CATALINA_HOME

Upvotes: 4

Views: 3006

Answers (1)

andyb
andyb

Reputation: 43823

Yes, this is possible. I have tested this on Windows 7 and Maven 3.0.4. FOO is my example environment variable.

First, add the following profile to the POM

<profiles>
  <profile>
    <id>env.FOO</id>
    <activation>
      <property>
        <name>!env.FOO</name>
      </property>
    </activation>
    <properties>
      <env.FOO>default_FOO</env.FOO>
    </properties>
  </profile>
</profiles>

As you already know, the placeholder ${env.FOO} is used elsewhere in the POM which will be replaced with a real value. The three ways to supply a value are:

1) By setting an environment variable

set FOO=env_FOO       (Windows)
export FOO=env_FOO    (Bash)

2) Supplying a command line parameter (this will override the environment variable)

mvn -Denv.FOO=cmdline_FOO    (note the env. prefix)

which will override the environment value.

3) Not setting FOO by any of the previous methods, the value in the POM will be activated clause, by the !env.FOO and be set to default_FOO

Upvotes: 4

Related Questions