Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define properties in maven depending on other property values

I want to create a maven project with the following structure:

A
|--pom.xml
|--B
   |--pom.xml
|--C
   |--pom.xml

where A, B and C are folders, and B's pom.xml and C's pom.xml are children of A's pom.xml. I want to have in B's pom.xml the following section:

<properties>
   <some.property>B</some.property>
</properties>

And in C:

<properties>
   <some.property>C</some.property>
</properties>

And I want in A something to define the value of several other properties based on the value of some property. So for example, in pseudocode, A would do something like this:

if ( some.property == 'B') then
    some.other.property = 'some-value-based-on-b'
else if ( some.property == 'C') then
    some.other.property = 'some-value-based-on-c'
...

I want to run the mvn clean install referring to A's pom.xml (which contains a module section pointing to B and C), so, as far as I understand, I cannot use profiles for this (since in maven2 projects running in the same reactor inherits the same active profile. I can use maven3, but couldn't find if it changes anything).

Does anyone has any idea how to do this?

Thanks,

like image 306
Rafael Avatar asked Nov 18 '10 18:11

Rafael


1 Answers

Out of the box, maven can't do this, and workarounds are discouraged (the properties are not supposed to change during the lifecycle).

There are several workarounds though, my favorite being the gmaven plugin, which lets you embed Groovy code in the pom.

The following code snippet will set the property 'abc' to either 'bar' or 'baz', depending on whether the property 'def' contains 'foo':

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source><![CDATA[
pom.properties['abc']=
   pom.properties['def'].contains('foo') ? 'bar' : 'baz';
                ]]></source>
            </configuration>
        </execution>
    </executions>
</plugin>

BTW, the docs are outdated, the plugin version is now 1.3 and the groupId has changed. Here's the current version.

like image 53
Sean Patrick Floyd Avatar answered Sep 30 '22 15:09

Sean Patrick Floyd