qoomon / maven-git-versioning-extension

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

Provide more fine grained ${version.*.label*} placeholders #206

Closed mruzicka closed 2 years ago

mruzicka commented 2 years ago

Description

Sometimes there is a need to wedge the git branch/tag information between an already present version label and possible ‑SNAPSHOT suffix. In order to do that conveniently we'd need the label to be parsed into two parts:

Change

This PR introduces four new version label placeholders to assist in the situation described above:

Examples

Original ${project.version} ${version.release.label} ${version.release.label.prefixed} ${version.snapshot.label} ${version.snapshot.label.prefixed}
1.0.0‑java7‑SNAPSHOT java7 ‑java7 SNAPSHOT ‑SNAPSHOT
1.0.0‑java7 java7 ‑java7 (empty string) (empty string)
1.0.0‑SNAPSHOT (empty string) (empty string) SNAPSHOT ‑SNAPSHOT
1.0.0 (empty string) (empty string) (empty string) (empty string)
qoomon commented 2 years ago

Thanks for your PR. To get a better understanding of the issue you want to tackle , could you provide a usage example?

mruzicka commented 2 years ago

Thank you for looking into this! What I'm after is to be able to wedge the git branch/tag into an existing -SNAPSHOT version. Suppose I have a version like this in my pom: 1.0.0‑java7‑SNAPSHOT and the following configuration in .mvn/maven-git-versioning-extension.xml:

...
        <ref type="branch">
            <pattern>master</pattern>
        </ref>
        <ref type="branch">
            <pattern>.+</pattern>
            <version>${version.release}${version.release.label.prefixed}-${ref}${version.snapshot.label.prefixed}</version>
        </ref>
...

This gives me unchanged version on the master branch, but version including the branch name wedged between the existing label (-java7) and the possible snapshot suffix on any other branch. E.g. on the dev branch I get: 1.0.0‑java7‑dev‑SNAPSHOT I don't see a way to achieve that without the placeholders I propose.

qoomon commented 2 years ago

I think you could just use following version template to achieve your version.

<version>${version.release}-${ref}-SNAPSHOT</version>

instead of

<version>${version.release}${version.release.label.prefixed}-${ref}${version.snapshot.label.prefixed}</version>
mruzicka commented 2 years ago

No, that would lose the -java7 label and would add the -SNAPSHOT suffix even to release versions.

qoomon commented 2 years ago

could you give an example of <version> in original pom file and expected version or the version you want to create?

qoomon commented 2 years ago

I think I got your point. What about following placeholders:

assume following pom project version 1.2.3-java7-SNAPSHOT:

assume following pom project version 1.2.3-java7:

then you should be able to generate your version like this

<version>${version.release}-${ref}${version.label.snapshot.prefixed}</version>

WDYT?

qoomon commented 2 years ago

However in general I think your versioning should follow semantic versioning and -... indicates a pre-release version, so you should use versions like 1.2.4.java-SNAPSHOT and then it works with current placeholders already.

mruzicka commented 2 years ago

I like your proposal. It has all the flexibility I need and by changing the ${version.release} definition it makes it even more straightforward to achieve my goal. So If the breaking changes to ${version.release} and ${version.label} are not a concern, I can go ahead and update the PR accordingly.

Re semantic versioning: I did not know the pre-release build concept, thanks for bringing it to my attention. Unfortunately it does not apply in our use case. It may apply in our use case.

Re examples: Given my original template (or your template using the new semantics), my desire is to have the ${project.version} transformed as follows (on any branch except master): ${project.version} git branch name desired version
1.0.0‑java7‑SNAPSHOT dev 1.0.0‑java7‑dev-SNAPSHOT
1.0.0‑java7 dev 1.0.0‑java7‑dev
1.0.0‑SNAPSHOT dev 1.0.0‑dev-SNAPSHOT
1.0.0 dev 1.0.0‑dev

I want the ${project.version} unmodified on the master branch, but that is taken care of by the below configuration:

...
        <ref type="branch">
            <pattern>master</pattern>
        </ref>
...
qoomon commented 2 years ago

hmm I am not that sure anymore, if this is the right approach

qoomon commented 2 years ago

For edge cases like yours I think it is maybe better to introduce an optional regex <projectVersionPattern> so you can model access to every part of your version format e.g.

<projectVersionPattern><![CDATA[^(?<core>.+?)(?<label>-SNAPSHOT)?$]]></projectVersionPattern>
<refs>
    <ref type="branch">
                <pattern>master</pattern>
    </ref>
    <ref type="branch">
        <pattern>.+</pattern>
        <version>${version.core}-${ref}${version.label}</version>
    </ref>
</refs>
mruzicka commented 2 years ago

That is of course ultimately flexible and does not bloat the defined placeholders with rarely used ones. So, I agree this is the way to go. Is this something you want to look into yourself or would you prefer me taking a stab at it?

qoomon commented 2 years ago

I'll will work on this next weekend

qoomon commented 2 years ago

feel free to try brand new version 9.1.0