20

I'd like to negate a boolean property as defined in pom.xml.

Meaning, given a property ${doSomething} I need to pass <skip> to a plugin where the value of <skip> should be the negation of ${doSomething}.

If ${doSomething} is false, then I want to skip. If it is true, then I don't want to skip. Any ideas?

Clarification: I am not talking about using the negation of a property for profile activation. I just want to pass the negation of a boolean into a plugin.

4
  • could you share the current command you are using to skip involving doSomething.
    – Naman
    Dec 2, 2016 at 17:14
  • @nullpointer It's a custom plugin. See ${download.cmake} at github.com/cmake-maven-project/cmake-maven-project. I want to skip downloads if users set this property to false, but the plugin actually takes the negation of this value as input (<skip> parameter).
    – Gili
    Dec 2, 2016 at 17:35
  • please add the details to the question, about the plugin context of your current pom.xml and the method of execution that you are following currently. I don't see ${download.cmake} in the link provided.
    – Naman
    Dec 2, 2016 at 17:38
  • Also if you are passing the value as -Dskip=true currently I don't see a point why you can't simply use false instead. Your current usage is not clear from the content in the question.
    – Naman
    Dec 2, 2016 at 17:41

2 Answers 2

18

Apart from profile activation, Maven doesn't have a boolean logic implemented. So if want to negate a property to pass it to a plugin, you'll need to do it yourself. It is a bit clumsy, but you could use the build-helper-maven-plugin:bsh-property goal, which enables to write a BeanShell script and export variables defined in it as Maven properties:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.12</version>
  <executions>
    <execution>
      <id>negate-prop</id>
      <phase>initialize</phase>
      <goals>
        <goal>bsh-property</goal>
      </goals>
      <configuration>
        <source>dontDoSomething = !${doSomething};</source>
        <properties>
          <property>dontDoSomething</property>
        </properties>
      </configuration>
    </execution>
  </executions>
</plugin>

You can't override the property, but you can define a new one containing the result of the negation; in the example above, it is dontDoSomething. This is ran in the initialize phase so that the rest of the plugins can use it as a parameter, with the standard ${dontDoSomething}.

This could be enhanced to have a default value for dontDoSomething if doSomething doesn't exist.

<source>
  value = project.getProperties().getProperty("doSomething");
  dontDoSomething = value == null ? false : !Boolean.parseBoolean(value);
</source>

BeanShell is a scripting language that looks very much like Java and you can use existing Java methods. In the above, the property "doSomething" is retrieved from the project's properties (project is injected by the plugin at evaluation-time with the current Maven project); it it isn't defined, we return false, otherwise, we negate the value.


If doSomething is specifically a system property, it could also be possible to (ab)use the profile activation feature and have 2 profiles: one activated by a property being true and setting another to false, and a second profile doing the inverse:

<profiles>
  <profile>
    <id>pro-1</id>
    <activation>
      <property>
        <name>doSomething</name>
        <value>!false</value>
      </property>
    </activation>
    <properties>
      <dontDoSomething>false</dontDoSomething>
    </properties>
  </profile>
  <profile>
    <id>pro-2</id>
    <activation>
      <property>
        <name>doSomething</name>
        <value>false</value>
      </property>
    </activation>
    <properties>
      <dontDoSomething>true</dontDoSomething>
    </properties>
  </profile>
</profiles>

This won't work if doSomething is a Maven property set in the <properties> tag for example. It will need to be passed as a system property with mvn -DdoSomething=true|false. The corresponding profile will be activated according to the value of the system property, which will define the dontDoSomething property to its inverse. If the property isn't defined, pro-1 will be active, setting dontDoSomething to the default value of false. All of this is quite ugly though...

7
  • 1
    Can you please update the answer to contain a more complete example of using two profiles to set the boolean to true or false?
    – Gili
    Dec 2, 2016 at 19:26
  • 1
    @Gili Yes, I just edited. Note that it'll only work if doSomething is a system property, not just any Maven property.
    – Tunaki
    Dec 2, 2016 at 19:36
  • How do I make the BeanShell solution work for properties with default values? Meaning, if ${doSomething} is defined, I want the negation; otherwise, I want a default value of false.
    – Gili
    Dec 2, 2016 at 19:55
  • @Gili BeanShell is a scripting language which looks very much like Java. I edited further. I also edited the profile-based solution, as it is also doable with it.
    – Tunaki
    Dec 2, 2016 at 20:04
  • Looks good, although Boolean.valueOf(String) will never return null, so your conditional can be simplified.
    – Gili
    Dec 2, 2016 at 20:21
0

I don't know the exact answer but I think you could try the following to invert the properties value (from http://maven.apache.org/guides/introduction/introduction-to-profiles.html). Declare another property where you invert the value of the first property:

<property>
    <name>dontDoSomething</name>
    <value>!${doSomething}</value>
</property>
5
  • This doesn't work because we're not talking about profile activation here. We are talking about defining a property that gets passed into a plugin.
    – Gili
    Dec 2, 2016 at 17:32
  • It's not about WHAT but HOW. How do you invert a property's value.
    – actc
    Dec 2, 2016 at 17:34
  • Ah. Sorry about that :) I'll try that now.
    – Gili
    Dec 2, 2016 at 17:41
  • Works for me. Thank you!
    – Gili
    Dec 2, 2016 at 19:27
  • 1
    This config will activate the profile it is under if dontDoSomething is a system property with a value which is the opposite of the value of the system property doSomething. It won't define the Maven property dontDoSomething to some value dynamically.
    – Tunaki
    Dec 2, 2016 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.