Skip to content

Commit 69fd48e

Browse files
authored
Merge pull request #254 from TheSnoozer/master
Adding a new Validation Mojo (as outlined in #106)
2 parents cac2c74 + 0fc9350 commit 69fd48e

File tree

5 files changed

+338
-1
lines changed

5 files changed

+338
-1
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,50 @@ And not imagine that you need to deploy a drastic API change - so the new versio
2626
Using this plugin, you can easily expose the information needed - based on git tags for example.
2727
One might say that this is usually accomplished by using `${project.version}` and I generally would agree, but maybe tags would fit your use case better than a plain version. :-)
2828

29+
Validate if properties are set as expected
30+
---------------------------------------------
31+
Since version **2.2.2** the maven-git-commit-id-plugin comes equipped with an additional validation utility which can be used to verify if your project properties are set as you would like to have them set.
32+
The validation can be used for *any* property that is visible inside the pom.xml and therefore can be used for a various amount of different use cases.
33+
In order to understand the ideology and intention here are some pretty usefull ideas you could achive by using the validation:
34+
* validate if the version of your project does not end with SNAPSHOT
35+
* validate if you are currently on a tag
36+
* ensure that your repository is not dirty
37+
* may other's :-)
38+
39+
With the current version of the validation the user can decide if the build should fail if *at least one* of the defined criterias do not match with the desired values.
40+
41+
For flexibility and due to the fact that this validation has a different scope than the maven-git-commit-id-plugin this validation needs to be configured as additional execution inside the configuration of the pom.xml.
42+
Once configured, the validation is executed during the verification-phase. However since the validation is done in a seperate execution the phase can easily be changed by adding the desired phase to the execution configuration.
43+
44+
Usage Example:
45+
46+
```xml
47+
<validationProperties>
48+
<!-- verify that the project version does not end with `-SNAPSHOT` -->
49+
<validationProperty>
50+
<name>validating project version</name>
51+
<value>${project.version}</value>
52+
<shouldMatchTo><![CDATA[^.*(?<!-SNAPSHOT)$]]></shouldMatchTo>
53+
<!-- for future reference on this particular regex, please refer to lookahead and lookbehind expressions -->
54+
<!-- we could also use: <shouldMatchTo>^[0-9\.]*$</shouldMatchTo> -->
55+
</validationProperty>
56+
<!-- verify that the current repository is not dirty -->
57+
<validationProperty>
58+
<name>validating git dirty</name>
59+
<value>${git.dirty}</value>
60+
<shouldMatchTo>false</shouldMatchTo>
61+
</validationProperty>
62+
<!-- verify that the current commit has a tag -->
63+
<validationProperty>
64+
<name>validating current commit has a tag</name>
65+
<value>${git.closest.tag.commit.count}</value>
66+
<shouldMatchTo>0</shouldMatchTo>
67+
</validationProperty>
68+
</validationProperties>
69+
```
70+
71+
*Note* : In order to be able to validate the generated git-properties inside the pom itself you may need to set the configutation `<injectAllReactorProjects>true</injectAllReactorProjects>`.
72+
2973
Other
3074
-----
3175
If you have a nice use case to share, please do fork this file and file a pull request with your story :-)
@@ -108,9 +152,18 @@ It's really simple to setup this plugin; below is a sample pom that you may base
108152
<version>2.2.1</version>
109153
<executions>
110154
<execution>
155+
<id>get-the-git-infos</id>
111156
<goals>
112157
<goal>revision</goal>
113-
</goals>
158+
</goals>
159+
</execution>
160+
<execution>
161+
<id>validate-the-git-infos</id>
162+
<goals>
163+
<goal>validateRevision</goal>
164+
</goals>
165+
<!-- *NOTE*: The default phase of validateRevision is verify, but in case you want to change it, you can do so by adding the phase here -->
166+
<phase>package</phase>
114167
</execution>
115168
</executions>
116169

@@ -317,6 +370,36 @@ It's really simple to setup this plugin; below is a sample pom that you may base
317370
-->
318371
<forceLongFormat>false</forceLongFormat>
319372
</gitDescribe>
373+
<!-- @since 2.2.2 -->
374+
<!--
375+
Since version **2.2.2** the maven-git-commit-id-plugin comes equipped with an additional validation utility which can be used to verify if your project properties are set as you would like to have them set.
376+
*Note*: This configuration will only be taken into account when the additional goal `validateRevision` is configured inside an execution.
377+
-->
378+
<validationProperties>
379+
<validationProperty>
380+
<!--
381+
A descriptive name that will be used to be able to identify the validation that does not match up (will be displayed in the error message).
382+
-->
383+
<name>validating project version</name>
384+
<!--
385+
the value that needs the validation
386+
*Note* : In order to be able to validate the generated git-properties inside the pom itself you may need to set the configutation `<injectAllReactorProjects>true</injectAllReactorProjects>`.
387+
-->
388+
<value>${project.version}</value>
389+
<!--
390+
the expected value
391+
-->
392+
<shouldMatchTo><![CDATA[^.*(?<!-SNAPSHOT)$]]></shouldMatchTo>
393+
</validationProperty>
394+
<!-- the next validationProperty you would like to validate -->
395+
</validationProperties>
396+
<!-- @since 2.2.2 -->
397+
<!--
398+
true by default, controls whether the validation will fail if *at least one* of the validationProperties does not match with it's expected values.
399+
If you don't care about this, you may want to set this value to false (this makes the configuration of validationProperties useless).
400+
*Note*: This configuration will only be taken into account when the additional goal `validateRevision` is configured inside an execution and at least one validationProperty is defined.
401+
-->
402+
<validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
320403
</configuration>
321404

322405
</plugin>

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,21 @@
337337
<groupId>pl.project13.maven</groupId>
338338
<artifactId>git-commit-id-plugin</artifactId>
339339
<version>${project.version}</version>
340+
<!-- optional to change the phases of the individual mojo's -->
340341
<executions>
341342
<execution>
343+
<id>get-the-git-infos</id>
342344
<goals>
343345
<goal>revision</goal>
344346
</goals>
345347
</execution>
348+
<execution>
349+
<id>validate-the-git-infos</id>
350+
<goals>
351+
<goal>validateRevision</goal>
352+
</goals>
353+
<phase>package</phase>
354+
</execution>
346355
</executions>
347356
<configuration>
348357
<verbose>true</verbose>
@@ -356,6 +365,7 @@
356365
<useNativeGit>false</useNativeGit>
357366
<abbrevLength>7</abbrevLength>
358367
<format>properties</format>
368+
<injectAllReactorProjects>true</injectAllReactorProjects>
359369
<gitDescribe>
360370
<skip>false</skip>
361371
<always>false</always>
@@ -370,6 +380,21 @@
370380
</excludeProperties>
371381
<failOnNoGitDirectory>false</failOnNoGitDirectory>
372382
<failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
383+
<!-- used in validateRevision -->
384+
<validationProperties>
385+
<validationProperty>
386+
<name>validating project version</name>
387+
<value>${project.version}</value>
388+
<!-- <shouldMatchTo>^[0-9\.]*$</shouldMatchTo> -->
389+
<shouldMatchTo><![CDATA[^.*(?<!-SNAPSHOT)$]]></shouldMatchTo>
390+
</validationProperty>
391+
<validationProperty>
392+
<name>validating git dirty</name>
393+
<value>${git.dirty}</value>
394+
<shouldMatchTo>false</shouldMatchTo>
395+
</validationProperty>
396+
</validationProperties>
397+
<validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
373398
</configuration>
374399
</plugin>
375400
</plugins>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.validation;
19+
20+
import org.apache.maven.execution.MavenSession;
21+
import org.apache.maven.plugin.AbstractMojo;
22+
import org.apache.maven.plugin.MojoExecutionException;
23+
import org.apache.maven.plugins.annotations.LifecyclePhase;
24+
import org.apache.maven.plugins.annotations.Mojo;
25+
import org.apache.maven.plugins.annotations.Parameter;
26+
import org.jetbrains.annotations.NotNull;
27+
28+
import pl.project13.maven.git.log.LoggerBridge;
29+
import pl.project13.maven.git.log.MavenLoggerBridge;
30+
31+
import java.util.List;
32+
import java.util.regex.Matcher;
33+
import java.util.regex.Pattern;
34+
35+
/**
36+
* @since 2.2.2
37+
*/
38+
@Mojo(name = "validateRevision", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
39+
public class ValidationMojo extends AbstractMojo {
40+
41+
@NotNull
42+
private final LoggerBridge log = new MavenLoggerBridge(this, false);
43+
44+
@Parameter(defaultValue = "true")
45+
private boolean validationShouldFailIfNoMatch;
46+
47+
@Parameter
48+
private List<ValidationProperty> validationProperties;
49+
50+
@Override
51+
public void execute() throws MojoExecutionException {
52+
if(validationProperties != null && validationShouldFailIfNoMatch) {
53+
for(ValidationProperty validationProperty: validationProperties) {
54+
String name = validationProperty.getName();
55+
String value = validationProperty.getValue();
56+
String shouldMatchTo = validationProperty.getShouldMatchTo();
57+
if((value != null) && (shouldMatchTo != null)) {
58+
validateIfValueAndShouldMatchToMatches(name, value, shouldMatchTo);
59+
} else {
60+
printLogMessageWhenValueOrShouldMatchToIsEmpty(name, value, shouldMatchTo);
61+
}
62+
}
63+
}
64+
}
65+
66+
private void validateIfValueAndShouldMatchToMatches(String name, String value, String shouldMatchTo) throws MojoExecutionException {
67+
Pattern pattern = Pattern.compile(shouldMatchTo);
68+
Matcher matcher = pattern.matcher(value);
69+
if(!matcher.find()) {
70+
String commonLogMessage = "Expected '" + value + "' to match with '" + shouldMatchTo + "'!";
71+
if (name != null) {
72+
throw new MojoExecutionException("Validation '" + name + "' failed! " +commonLogMessage);
73+
} else {
74+
throw new MojoExecutionException("Validation of an unidentified validation (please set the name property-tag to be able to identify the validation) failed! " +commonLogMessage);
75+
}
76+
}
77+
}
78+
79+
private void printLogMessageWhenValueOrShouldMatchToIsEmpty(String name, String value, String shouldMatchTo) {
80+
String commonLogMessage = "since one of the values was null! (value = '" + value + "'; shouldMatchTo = '" + shouldMatchTo + "').";
81+
if (name != null) {
82+
log.warn("Skipping validation '" + name + "' " + commonLogMessage);
83+
} else {
84+
log.warn("Skipping an unidentified validation (please set the name property-tag to be able to identify the validation) "+commonLogMessage);
85+
}
86+
}
87+
88+
public void setValidationShouldFailIfNoMatch(boolean validationShouldFailIfNoMatch){
89+
this.validationShouldFailIfNoMatch = validationShouldFailIfNoMatch;
90+
}
91+
92+
public void setValidationProperties(List<ValidationProperty> validationProperties){
93+
this.validationProperties = validationProperties;
94+
}
95+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.validation;
19+
20+
21+
public class ValidationProperty {
22+
private String name;
23+
private String value;
24+
private String shouldMatchTo;
25+
26+
ValidationProperty(String name, String value, String shouldMatchTo) {
27+
this.name = name;
28+
this.value = value;
29+
this.shouldMatchTo = shouldMatchTo;
30+
}
31+
32+
public void setName(String name){
33+
this.name = name;
34+
}
35+
36+
public void setValue(String value){
37+
this.value = value;
38+
}
39+
40+
public void setShouldMatchTo(String shouldMatchTo){
41+
this.shouldMatchTo = shouldMatchTo;
42+
}
43+
44+
public String getName(){
45+
return name;
46+
}
47+
48+
public String getValue(){
49+
return value;
50+
}
51+
52+
public String getShouldMatchTo(){
53+
return shouldMatchTo;
54+
}
55+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.validation;
19+
20+
import org.apache.maven.plugin.MojoExecutionException;
21+
22+
import pl.project13.maven.git.log.LoggerBridge;
23+
import pl.project13.maven.git.log.MavenLoggerBridge;
24+
25+
import org.junit.Test;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
public class ValidationMojoTest {
31+
@Test(expected = MojoExecutionException.class)
32+
public void validationNotMatchingAndValidationShouldFailIfNoMatch() throws MojoExecutionException {
33+
List<ValidationProperty> validationProperties = getNonMatchingValidationProperties();
34+
executeMojo(validationProperties, true);
35+
}
36+
37+
@Test
38+
public void validationNotMatchingAndValidationShouldNotFailIfNoMatch() throws MojoExecutionException {
39+
List<ValidationProperty> validationProperties = getNonMatchingValidationProperties();
40+
executeMojo(validationProperties, false);
41+
}
42+
43+
private List<ValidationProperty> getNonMatchingValidationProperties() {
44+
return getListValidationProperty("name", "value", "thisIsNotMatchingToValue");
45+
}
46+
47+
@Test
48+
public void validationMatchingAndValidationShouldFailIfNoMatch() throws MojoExecutionException {
49+
List<ValidationProperty> validationProperties = getMatchingValidationProperties();
50+
executeMojo(validationProperties, true);
51+
}
52+
53+
private List<ValidationProperty> getMatchingValidationProperties() {
54+
return getListValidationProperty("name", "value", "value");
55+
}
56+
57+
@Test
58+
public void nullTests() throws MojoExecutionException {
59+
boolean validationShouldFailIfNoMatch = true;
60+
executeMojo(null, validationShouldFailIfNoMatch);
61+
executeMojo(getListValidationProperty(null, null, null), validationShouldFailIfNoMatch);
62+
executeMojo(getListValidationProperty("", null, null), validationShouldFailIfNoMatch);
63+
executeMojo(getListValidationProperty(null, "", null), validationShouldFailIfNoMatch);
64+
executeMojo(getListValidationProperty(null, null, ""), validationShouldFailIfNoMatch);
65+
}
66+
67+
private void executeMojo(List<ValidationProperty> validationProperties, boolean validationShouldFailIfNoMatch) throws MojoExecutionException {
68+
ValidationMojo cut = new ValidationMojo();
69+
cut.setValidationProperties(validationProperties);
70+
cut.setValidationShouldFailIfNoMatch(validationShouldFailIfNoMatch);
71+
cut.execute();
72+
}
73+
74+
private List<ValidationProperty> getListValidationProperty(String name, String value, String shouldMatchTo) {
75+
List<ValidationProperty> list = new ArrayList<ValidationProperty>();
76+
list.add(new ValidationProperty(name, value, shouldMatchTo));
77+
return list;
78+
}
79+
}

0 commit comments

Comments
 (0)