Why does POM <configuration> have precedence over -D property expressions?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does POM <configuration> have precedence over -D property expressions?

Andreas Sewe-3
Hi,

I am currently sprinkling <configuration> child elements like the
following through my (parent) POMs for enforcer:enforce, tidy:check and
checkstyle:check <executions>:

  <skip>${skipChecks}</skip>

This allows me to skip all kinds of checks with a simple
-DskipChecks=true (or even -DskipChecks), just like I am used to for
tests with -DskipTests.

Unfortunately, I cannot selectively override this, as the following
doesn't work:

  mvn install -DskipChecks -Denforcer.skip=false

The above command still uses the <skip>${skipChecks}</skip> (with
skipChecks=true).

Further experimentation led me to believe that *any* explicit pom.xml
<configuration> cannot be overridden by a property expression given on
the command line.

This was very surprising, as I would have expected Maven to follow a
"command line takes precedence over configuration file" approach like
other tools -- but apparently it doesn't (at least in Maven 3.3.9 and
3.6.3) and thus violates the Principle of Least Astonishment (for one of
its users anyway).

In particular, Maven's handling of property expressions means that
having no <skip> element and making the default explicit behave
differently; a <configuration> element like this can never be overridden
on the command-line:

  <skip>false</skip>

I wonder why it was designed that way?

Or should this be considered a bug?

Best wishes,

Andreas


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andy Feldman
I agree this behavior is unexpected. But it's different for properties! So
a workaround here is to have a unique property for each check you want to
skip, so that you can override the property by command line rather than
overriding the <skip> configuration directly. Then you can have a single
way to set all these properties. In my company's parent pom we have a
profile that can be activated like `mvn install -P quick` to skip tests and
checks:

<profile>
  <id>quick</id>
  <properties>
    <checkstyle.skip>true</checkstyle.skip>
    <duplicate-finder.skip>true</duplicate-finder.skip>
    <enforcer.skip>true</enforcer.skip>
    <skipITs>true</skipITs>
    <skipTests>true</skipTests>
  </properties>
</profile>

So if I want to still run the enforcer plugin, I can do this: `mvn install
-P quick -Denforcer.skip=false`.

If you want to keep using a property rather than a profile, I assume you
could have the specific property for each check reference your main
property by default, like:

<properties>
  <skip.our.enforcer>${skipChecks}</skip.our.enforcer>
</properties>
[...]
<skip>${skip.our.enforcer}</skip>

But I haven't tested it.


On Mon, Sep 14, 2020 at 9:11 AM Andreas Sewe <[hidden email]>
wrote:

> Hi,
>
> I am currently sprinkling <configuration> child elements like the
> following through my (parent) POMs for enforcer:enforce, tidy:check and
> checkstyle:check <executions>:
>
>   <skip>${skipChecks}</skip>
>
> This allows me to skip all kinds of checks with a simple
> -DskipChecks=true (or even -DskipChecks), just like I am used to for
> tests with -DskipTests.
>
> Unfortunately, I cannot selectively override this, as the following
> doesn't work:
>
>   mvn install -DskipChecks -Denforcer.skip=false
>
> The above command still uses the <skip>${skipChecks}</skip> (with
> skipChecks=true).
>
> Further experimentation led me to believe that *any* explicit pom.xml
> <configuration> cannot be overridden by a property expression given on
> the command line.
>
> This was very surprising, as I would have expected Maven to follow a
> "command line takes precedence over configuration file" approach like
> other tools -- but apparently it doesn't (at least in Maven 3.3.9 and
> 3.6.3) and thus violates the Principle of Least Astonishment (for one of
> its users anyway).
>
> In particular, Maven's handling of property expressions means that
> having no <skip> element and making the default explicit behave
> differently; a <configuration> element like this can never be overridden
> on the command-line:
>
>   <skip>false</skip>
>
> I wonder why it was designed that way?
>
> Or should this be considered a bug?
>
> Best wishes,
>
> Andreas
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andy Feldman
I guess you don't even need a custom property since each plugin tends to
have one already. So a simpler property-based approach would just be:

<properties>
  <enforcer.skip>${skipChecks}</enforcer.skip>
</properties>

And maybe you'd also need <skipChecks>false</skipChecks> in your properties
to provide a default for when skipChecks isn't specified on the command
line. But like I said, I haven't tried this approach so you'd have to
experiment and see if it works for you.


On Mon, Sep 14, 2020 at 10:38 AM Andy Feldman <[hidden email]> wrote:

> I agree this behavior is unexpected. But it's different for properties! So
> a workaround here is to have a unique property for each check you want to
> skip, so that you can override the property by command line rather than
> overriding the <skip> configuration directly. Then you can have a single
> way to set all these properties. In my company's parent pom we have a
> profile that can be activated like `mvn install -P quick` to skip tests and
> checks:
>
> <profile>
>   <id>quick</id>
>   <properties>
>     <checkstyle.skip>true</checkstyle.skip>
>     <duplicate-finder.skip>true</duplicate-finder.skip>
>     <enforcer.skip>true</enforcer.skip>
>     <skipITs>true</skipITs>
>     <skipTests>true</skipTests>
>   </properties>
> </profile>
>
> So if I want to still run the enforcer plugin, I can do this: `mvn install
> -P quick -Denforcer.skip=false`.
>
> If you want to keep using a property rather than a profile, I assume you
> could have the specific property for each check reference your main
> property by default, like:
>
> <properties>
>   <skip.our.enforcer>${skipChecks}</skip.our.enforcer>
> </properties>
> [...]
> <skip>${skip.our.enforcer}</skip>
>
> But I haven't tested it.
>
>
> On Mon, Sep 14, 2020 at 9:11 AM Andreas Sewe <
> [hidden email]> wrote:
>
>> Hi,
>>
>> I am currently sprinkling <configuration> child elements like the
>> following through my (parent) POMs for enforcer:enforce, tidy:check and
>> checkstyle:check <executions>:
>>
>>   <skip>${skipChecks}</skip>
>>
>> This allows me to skip all kinds of checks with a simple
>> -DskipChecks=true (or even -DskipChecks), just like I am used to for
>> tests with -DskipTests.
>>
>> Unfortunately, I cannot selectively override this, as the following
>> doesn't work:
>>
>>   mvn install -DskipChecks -Denforcer.skip=false
>>
>> The above command still uses the <skip>${skipChecks}</skip> (with
>> skipChecks=true).
>>
>> Further experimentation led me to believe that *any* explicit pom.xml
>> <configuration> cannot be overridden by a property expression given on
>> the command line.
>>
>> This was very surprising, as I would have expected Maven to follow a
>> "command line takes precedence over configuration file" approach like
>> other tools -- but apparently it doesn't (at least in Maven 3.3.9 and
>> 3.6.3) and thus violates the Principle of Least Astonishment (for one of
>> its users anyway).
>>
>> In particular, Maven's handling of property expressions means that
>> having no <skip> element and making the default explicit behave
>> differently; a <configuration> element like this can never be overridden
>> on the command-line:
>>
>>   <skip>false</skip>
>>
>> I wonder why it was designed that way?
>>
>> Or should this be considered a bug?
>>
>> Best wishes,
>>
>> Andreas
>>
>>
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Tomo Suzuki
> *any* explicit pom.xml <configuration> cannot be overridden by a property
expression given on
> the command line.

I also encountered the problem when my Maven project has multiple main
classes and I wanted to run them by switching "mvn exec:java
-Dexec.mainClass=...' while keeping the default one in pom.xml. I don't
know why Maven is designed in that way.

Good to know the use of properties to control it. Thank you.

--
Regards,
Tomo
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Oliver B. Fischer-2
In reply to this post by Andreas Sewe-3

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256


I had the same question some days back
(https://mail-archives.apache.org/mod_mbox/maven-users/202009.mbox/%3C922b4efc-3296-d35d-0675-d6c0090cc4b1%40swe-blog.net%3E)
and Stuart McCulloch sent me a link to this JIRA issue:
https://issues.apache.org/jira/browse/MNG-4979

So, this seems to be a recurring issue that irritates a lot of people.

Oliver

Am 14.09.20 um 18:11 schrieb Andreas Sewe:

> Hi,
>
> I am currently sprinkling <configuration> child elements like the
> following through my (parent) POMs for enforcer:enforce, tidy:check and
> checkstyle:check <executions>:
>
>   <skip>${skipChecks}</skip>
>
> This allows me to skip all kinds of checks with a simple
> -DskipChecks=true (or even -DskipChecks), just like I am used to for
> tests with -DskipTests.
>
> Unfortunately, I cannot selectively override this, as the following
> doesn't work:
>
>   mvn install -DskipChecks -Denforcer.skip=false
>
> The above command still uses the <skip>${skipChecks}</skip> (with
> skipChecks=true).
>
> Further experimentation led me to believe that *any* explicit pom.xml
> <configuration> cannot be overridden by a property expression given on
> the command line.
>
> This was very surprising, as I would have expected Maven to follow a
> "command line takes precedence over configuration file" approach like
> other tools -- but apparently it doesn't (at least in Maven 3.3.9 and
> 3.6.3) and thus violates the Principle of Least Astonishment (for one of
> its users anyway).
>
> In particular, Maven's handling of property expressions means that
> having no <skip> element and making the default explicit behave
> differently; a <configuration> element like this can never be overridden
> on the command-line:
>
>   <skip>false</skip>
>
> I wonder why it was designed that way?
>
> Or should this be considered a bug?
>
> Best wishes,
>
> Andreas
>
- --
N Oliver B. Fischer
A Schönhauser Allee 64, 10437 Berlin, Deutschland/Germany
P +49 30 44793251
M +49 178 7903538
E [hidden email]
S oliver.b.fischer
J [hidden email]
X http://xing.to/obf
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEU7j685HGR9cAsMGwB88X6wLziPwFAl9fzxAACgkQB88X6wLz
iPzd/w/9FFSjkXtHrspdg9z8sV5tXEEXIGJdw1XRcDL8SDl412GmCIoCN5+HuYSa
MIMyGrmlsMgdeHUkx/0wvUXLBjmV7oEiAIhiLhA4l25F3Qc+y5zoUxZIUjNbZiZu
4oR04RZcrIdBA67sdxkvhQfM8rnv4/P97L70CVZpA8/nFNDjYWrG2troCTLMn6MI
NmjiYMpmG9xHDxy3KkqJqXwfEZDmEqphYV2K4T18EOcw5dP7ZJBK3sHXczK93Wkz
eTrDDgVcBHexSR2aqGX6xlNdtR4UScsCJjsXpnCZq/09qS9pOduBCIPX8Cswsi7B
d1nFKmNR41TpZtQFv8LCrbNkK5FBSupsxrOGgKjGza70y3JPki13I15tKZjnMZFY
CvaSK36VzEQOMVaZL5TB1ybXvF9OWXJgwqGT1dYCoCm3WDFCdGka3igVKKJWEE/P
xriYNkmzhKuKY5mAJa7wpXa1DQLnraFLcIPTEHNKLYocct58yRGtsgCvx7FIXflY
rtfou8+b5Q6FGHJcGpTiL7Zh191nV1gUJJA3ZXJwGzdvs+tvwLYPj34+5LNvwj0z
Ax1MKZLllCfq2wLm8NAnVqHYh55K5YN5O/jG+Fk9Mw87ynj+Tn9qIQ9jQ7nGIVyv
nvF87+KgBTQE5OFSpmLvDJEkN+zPqnp81VDkjNdMUSj16kcgAw8=
=oPqj
-----END PGP SIGNATURE-----


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andreas Sewe-3
Oliver B. Fischer wrote:
> I had the same question some days back
> (https://mail-archives.apache.org/mod_mbox/maven-users/202009.mbox/%3C922b4efc-3296-d35d-0675-d6c0090cc4b1%40swe-blog.net%3E)
> and Stuart McCulloch sent me a link to this JIRA issue:
> https://issues.apache.org/jira/browse/MNG-4979

Thanks for the pointer, Oliver. I also thought this behavior was a bug
and even tested with different Maven versions, assuming that this was a
regression.

> So, this seems to be a recurring issue that irritates a lot of people.

Yes, it is very irritating and I don't really buy Robert Scholte's
argument [1] that this is a feature rather than a bug.

Yes, it allows you to lock-in critical configuration options and make
them read-only, but to be on the safe side, you would need to do this
for *every* option of your plug-ins, which is obviously undesirable.

Also, if you break something with a manual override via the
command-line, the cause is normally crystal-clear, as you just added the
-D to the invocation; it's not something that snuck in using some
configuration file the user didn't even know about.

I hence really wish MNG-4979 would be reconsidered, maybe for Maven 4.

Best wishes,

Andreas

[1] <https://issues.apache.org/jira/browse/MNG-4979#comment-16853944>


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andreas Sewe-3
In reply to this post by Andy Feldman
Andy Feldman wrote:

> I guess you don't even need a custom property since each plugin tends to
> have one already. So a simpler property-based approach would just be:
>
> <properties>
>   <enforcer.skip>${skipChecks}</enforcer.skip>
> </properties>
>
> And maybe you'd also need <skipChecks>false</skipChecks> in your properties
> to provide a default for when skipChecks isn't specified on the command
> line. But like I said, I haven't tried this approach so you'd have to
> experiment and see if it works for you.
Thank you, Andy, for the suggestions.

My situation is unfortunately a bit more complex than that, as I have
*two* <execution>s of the maven-enforcer-plugin, only one of which
should be affected by -DskipChecks. The other simply uses the
<requireMavenVersion> rule, which IMHO shouldn't easily be disabled (but
should still respect -Denforcer.skip)

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>enforce-maven-version/id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireMavenVersion>
              <version>[3.6,4)</version>
            </requireMavenVersion>
          </rules>
       </configuration>
     </execution>
     <execution>
        <id>checks</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <skip>$[skipChecks}</skip>
          <rules>
            <!-- further rules -->
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>

I can't think of a way to use <properties> to get the desired behavior
in this case, at least not without resorting to profiles -- which I am
trying to avoid, as they seem to be frowned upon for this kind of stuff.

What I want is this:

  no property -> enforce-maven-version, checks

  -DskipChecks=false -> enforce-maven-version, checks

  -DskipChecks=true -> enforce-maven-version

  -Denforcer.skip=true -> no executions

  -Denforcer.skip=false, -DskipChecks=true -> enforce-maven-version,
checks (but possibly other checks predicated on skipChecks)

  etc.

In other words, -Denforcer.skip, being the more direct option, should
take precedence over -DskipChecks.

Any ideas?

Best wishes,

Andreas Sewe


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andy Feldman
On Tue, Sep 15, 2020 at 1:12 PM Andreas Sewe <[hidden email]>
wrote:

> My situation is unfortunately a bit more complex than that, as I have
> *two* <execution>s of the maven-enforcer-plugin, only one of which
> should be affected by -DskipChecks. The other simply uses the
> <requireMavenVersion> rule, which IMHO shouldn't easily be disabled (but
> should still respect -Denforcer.skip)
> [...]
> What I want is this:
> [...]

-Denforcer.skip, being the more direct option, should
> take precedence over -DskipChecks.
>

Just an idea:

<properties>
  <enforcer.skip>false</enforcer.skip>
  <skipChecks>${enforcer.skip}</skipChecks>
</properties>

This way if you set -DskipChecks=true, then only <id>checks</id> would be
skipped, but if you set -Denforcer.skip=true, it would also cause
skipChecks to be true, so both executions would be skipped. I haven't
actually tested it though.

--
Andy Feldman
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Andreas Sewe-3
Andy Feldman wrote:

>> My situation is unfortunately a bit more complex than that, as I have
>> *two* <execution>s of the maven-enforcer-plugin, only one of which
>> should be affected by -DskipChecks. The other simply uses the
>> <requireMavenVersion> rule, which IMHO shouldn't easily be disabled (but
>> should still respect -Denforcer.skip)
>> [...]
>> What I want is this:
>> [...]
>
> -Denforcer.skip, being the more direct option, should
>> take precedence over -DskipChecks.
>
> Just an idea:
>
> <properties>
>   <enforcer.skip>false</enforcer.skip>
>   <skipChecks>${enforcer.skip}</skipChecks>
> </properties>
>
> This way if you set -DskipChecks=true, then only <id>checks</id> would be
> skipped, but if you set -Denforcer.skip=true, it would also cause
> skipChecks to be true, so both executions would be skipped. I haven't
> actually tested it though.
Thanks, Andy. The above does have the desired behavior *if* the
maven-enforcer-plugin were the only plugin which should be governed by
-DskipChecks.

But as I said in my initial mail, that property should also control the
maven-checkstyle-plugin, maven-tidy-plugin, and others (kine of like
-DskipTests does for maven-surefire-plugin and maven-failsafe-plugin).
And that's just not doable with

  <skipChecks>${enforcer.skip}</skipChecks>

I experimented quite a bit yesterday and have become convinced that the
desired behavior is not possible, at least without profiles (ugh!) --
and even then I am not sure, as the profile would need to affect only
some executions.

But maybe I am missing something.

Best wishes,

Andreas


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why does POM <configuration> have precedence over -D property expressions?

Bernd Eckenfels
Hello,

While I do agree that it is not very intuitive, I don’t think there is any chance to change this in a compatible way.

 Also it’s pretty easy to understand: just remember this simplification: you can only define properties, and if the configuration should be affected by them you need to have a property explicitly in the POMs or as implicated default value. With this info in mind Mojo Docs are much easier to read ;)

Gruß
Bernd
--
https://Bernd.eckenfels.net

________________________________
Von: Andreas Sewe <[hidden email]>
Gesendet: Mittwoch, September 16, 2020 10:02 AM
An: [hidden email]
Betreff: Re: Why does POM <configuration> have precedence over -D property expressions?

Andy Feldman wrote:

>> My situation is unfortunately a bit more complex than that, as I have
>> *two* <execution>s of the maven-enforcer-plugin, only one of which
>> should be affected by -DskipChecks. The other simply uses the
>> <requireMavenVersion> rule, which IMHO shouldn't easily be disabled (but
>> should still respect -Denforcer.skip)
>> [...]
>> What I want is this:
>> [...]
>
> -Denforcer.skip, being the more direct option, should
>> take precedence over -DskipChecks.
>
> Just an idea:
>
> <properties>
>   <enforcer.skip>false</enforcer.skip>
>   <skipChecks>${enforcer.skip}</skipChecks>
> </properties>
>
> This way if you set -DskipChecks=true, then only <id>checks</id> would be
> skipped, but if you set -Denforcer.skip=true, it would also cause
> skipChecks to be true, so both executions would be skipped. I haven't
> actually tested it though.

Thanks, Andy. The above does have the desired behavior *if* the
maven-enforcer-plugin were the only plugin which should be governed by
-DskipChecks.

But as I said in my initial mail, that property should also control the
maven-checkstyle-plugin, maven-tidy-plugin, and others (kine of like
-DskipTests does for maven-surefire-plugin and maven-failsafe-plugin).
And that's just not doable with

  <skipChecks>${enforcer.skip}</skipChecks>

I experimented quite a bit yesterday and have become convinced that the
desired behavior is not possible, at least without profiles (ugh!) --
and even then I am not sure, as the profile would need to affect only
some executions.

But maybe I am missing something.

Best wishes,

Andreas