qoomon / maven-git-versioning-extension

This extension will set project version, based on current Git branch or tag.
GNU General Public License v3.0
303 stars 87 forks source link

Maven enforcer tries to enforce version management #171

Closed delanym closed 1 year ago

delanym commented 2 years ago

I add to .mvn/extensions.xml

  <extension>
    <groupId>me.qoomon</groupId>
    <artifactId>maven-git-versioning-extension</artifactId>
    <version>7.1.2</version>
  </extension>

When I run mvn validate -N I get

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:
Some plugins are missing valid versions or depend on Maven 3.8.4 defaults: (LATEST RELEASE SNAPSHOT are not allowed)
me.qoomon:maven-git-versioning-extension.       The version currently in use is 7.1.2 via super POM or default lifecycle bindings

Because maven-enforcer-plugin includes enforce-managed-deps-rule:1.3 configured like so

                <requireManagedDeps implementation="org.commonjava.maven.enforcer.rule.EnforceManagedDepsRule">
                  <checkProfiles>true</checkProfiles>
                  <failOnViolation>true</failOnViolation>
                </requireManagedDeps>

I don't have this problem with my other extensions.

qoomon commented 2 years ago

Hi, thanks for reporting this behaviour. Could you provide a minimal project to reproduce this bug?

qoomon commented 2 years ago

I can't reproduce the warning.

My setup
delanym commented 2 years ago

@qoomon https://github.com/delanym/maven-git-versioning-extension-test

qoomon commented 2 years ago

I"m able to reproduce the bug, thanks a lot. I found the root cause already, however I still try to figuring out how to work around that.

delanym commented 2 years ago

Ok cool. BTW we've been using https://github.com/pascalgn/properties-maven-extension for a long time. Its really simple and did what we needed. But the author is not getting around to the issues I've logged - I haven't the skills to fix them myself. One of them is critical for our workflow so I'm contemplating using this extension instead.

Is it possible for the extension to

We're using CI-friendly properties https://maven.apache.org/maven-ci-friendly.html Since the properties-maven-extension doesn't give the correct tag I'm forced to set the revision myself with -Drevision=2.2.14 All I need this extension to do is set the revision according to git describe --abbrev=0

qoomon commented 2 years ago

I have two simple workarounds so far:

qoomon commented 2 years ago

I think I was able to fix it. The fix is already on master branch. Feel free to test it. I'll release a fixed version tomorrow.

qoomon commented 2 years ago

@delanym

Is it possible for the extension to

  • not write intermediate pom files to disk

yes and no :) An .git-versioned.pom.xml will be created always. The original pom.xml will be updated only, if configured within plugin config file or if the command line parameter is set.

  • provide the git.dir.worktree property

currently thats not supported, however I could add this feature quite easily.

  • set the version to the output of git describe --abbrev=0

yes see ${describe} in README

delanym commented 2 years ago

@qoomon having git.dir.worktree would undepend me on the properties-maven-extension so that would be great.

Creating .git-versioned.pom.xml follows the same approach as flatten-maven-plugin. But this is overkill to write out 500+ files when only a single line changes. We're stuck building on NTFS disk and sometimes have lock file issues, so the less IO the better. As an extension, the poms are written out for ALL projects, even when I just want to build one project with -pl switch, or when gitflow-incremental-builder extension limits the projects. This also comes with an extra 1500+ lines of logs. Can these be turned off?

qoomon commented 2 years ago

Out of curiosity, what do you need git.worktree for?

delanym commented 2 years ago

@qoomon there are a couple of ways to figure out the root Maven directory but none as reliable as asking git. There was a discussion about this on the users mailing list last year. For example, I configure the depgraph-maven-plugin with <customStyleConfiguration>${git.dir.worktree}/depgraph.json</customStyleConfiguration>

qoomon commented 2 years ago

i just added git.worktree project property at master branch

qoomon commented 2 years ago

Regarding the generation of .git-versioned-pom.xml , I'll need to do some more testing. I"ll keep you posted.

qoomon commented 2 years ago

I'm not sure what is the best way to implement not writing git versioned pom file right away. What do you think about following ideas and questions?

delanym commented 2 years ago

What I don't understand from the start is why you're calling this an extension when its actually a plugin?

Currently I use properties-maven-extension to set a property that determines the version number of projects before the poms are even parsed. It greatly simplifies the issue. Of course I have to use CI-friendly properties to achieve this, but Im quite happy to.

FYI I run enforcer and sortpom plugin in validate, followed by flatten in initialize (although this could come later).

qoomon commented 2 years ago

The plugin mechanism ist only needed to postpone set new pom location. This is needed to make maven plugins work like mvn versions:set -DnewVersion=1.0.0, otherwise this change would be written to the git version pom file. The reason I need to set new pom location at all, is that otherwise the unmodified pom would be part of a maven release (mvn deploy)

JWT007 commented 1 year ago

Hi @qoomon, just jumping in here :)

I am using 9.3.1 and the only way I have to get around the enforcer problem is still the "unCheckedPluginList" option - but that works fine for me ... just need to remember to add it to each project.

@delanym regarding extension vs. plugin - see: https://maven.apache.org/guides/mini/guide-using-extensions.html

_"Extensions are a way to add classes to Core Classloader. This is necessary for adjusting Maven in a way that affects more than just one plug-in."_

By the way you can limit output to the logger if you change the log-level for the extension: ie. in your top-level project add a ".mvn/jvm.config" and add the following line to it (which will change from 'info' to 'error')

-Dorg.slf4j.simpleLogger.log.me.qoomon.maven.gitversioning.GitVersioningModelProcessor=error

But then you really see nothing if no errors pop...this will be it.

[03:48:25.758+0100] [INFO] --- maven-git-versioning-extension:9.3.1:git-versioning (git-versioning) @ foobar ---

That being said the info output is very useful to see that your configured pattern rules are working correctly...otherwise you might be searching for a while when something misbehaves :)

[03:50:25.437+0100] [INFO] --- me.qoomon:maven-git-versioning-extension:9.3.1 [core extension] ----
[03:50:28.113+0100] [INFO] matching ref: BRANCH - master
[03:50:28.113+0100] [INFO] ref configuration: BRANCH - pattern: master
[03:50:28.114+0100] [INFO]   version: ${version}
[03:50:28.126+0100] [INFO] 
[03:50:28.127+0100] [INFO] foobar
[03:50:28.130+0100] [INFO] set version to 1.1.x-SNAPSHOT

As far as I can tell, a separate file must be written to disk for each the main project and any sub-modules because:

I think validate is your best phase...because everything after that relies on the modified model? (ie. filtering resources and replacing ${project.version}). You need to replace the effective model early. I think if defined in the .mvn/extensions.xml the phases don't really apply and the early-loading hooks into the lifecycle-listener after the MavenSession is started and before any other phases are run???

Either way wouldn't this be a feature request for the maven-enforcer-plugin on the check to add an option to ignore extensions? Just by chance I had created this exact ticket last week :P:

https://issues.apache.org/jira/browse/MENFORCER-436

qoomon commented 1 year ago

maybe it is possible to add me.qoomon:maven-git-versioning-extension to unCheckedPluginList automatically by the extension.

JWT007 commented 1 year ago

is it loaded by that point? or it might not be in the plugin list at all... it would require a dependency on the enforcer plugin wouldn't it? it is the question if you need to try to deal with a "bug" from another plugin? :)

qoomon commented 1 year ago

feel free to try version 9.3.2, it should add plugin to unCheckedPluginList automatically.