Skip to content

Commit 0a54987

Browse files
authored
Merge pull request #58 from egineering-llc/release/1.6.0
Release/1.6.0
2 parents 8a0b932 + d90f546 commit 0a54987

File tree

7 files changed

+168
-100
lines changed

7 files changed

+168
-100
lines changed

README.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ It does so by:
77
* Enforcing [gitflow](http://nvie.com/posts/a-successful-git-branching-model/) version heuristics in [Maven](http://maven.apache.org/) projects.
88
* Coercing Maven to gracefully support the gitflow workflow without imposing complex CI job configurations or complex Maven setups.
99
* Setting distributionManagement repositories (for things like [maven-deploy-plugin](https://maven.apache.org/plugins/maven-deploy-plugin/)) based upon the current git branch.
10-
* SCM tagging builds for the master branch, using the CI server's repository connection information. (Zero Maven scm configuration necessary)
10+
* SCM tagging builds for the master branch. You can use the project SCM definition, or if you omit it, you can resolve the CI server's repository connection information. (Zero Maven scm configuration necessary)
1111
* Promoting existing tested (staged) artifacts for release, rather than re-building the artifacts. Eliminates the risk of accidental master merges or commits resulting in untested code being released, and provides digest hash traceability for the history of artifacts.
1212
* Enabling the decoupling of repository deployment and execution environment delivery based on the current git branch.
1313
* Automated deployment, promotion, and delivery of projects without the [maven-release-plugin](http://maven.apache.org/maven-release/maven-release-plugin/) or some other [*almost there* solution](https://axelfontaine.com/blog/final-nail.html).
14+
* Customizing maven project and system properties based upon the current branch being built. This allows test cases to target different execution environments without changing the artifact results.
1415

1516
# Why would I want to use this?
1617

@@ -32,11 +33,16 @@ All of the solutions to these issues are implemented independently in different
3233

3334
# I want all of that. (Usage)
3435

35-
1. Make sure your build server sets environment variables for git branches and git URLs. The plugin defaults are configured out of the box for Jenkins & Hudson.
36-
2. Configure the plugin goals and add the build extension to your Maven project. Here's an example that will get you going quickly...
36+
1. Make sure you have a your Project SCM configured for your git repository, or that your build server sets environment variables for git branches and git URLs.
37+
Out of the box, the plugin will try to resolve the git branch based upon the SCM definition on your maven project, or fall back to the environment variables set by Jenkins and Hudson.
38+
2. Configure the plugin goals and add the build extension to your Maven project. Here's an example that will get you going quickly with all the features...
3739

3840
```
3941
<project>
42+
...
43+
<scm>
44+
<developerConnection>scm:git:ssh://git@server/project/path.git</developerConnection>
45+
</scm>
4046
...
4147
<build>
4248
<plugins>
@@ -52,8 +58,6 @@ All of the solutions to these issues are implemented independently in different
5258
<releaseDeploymentRepository>${release.repository}</releaseDeploymentRepository>
5359
<stageDeploymentRepository>${stage.repository}</stageDeploymentRepository>
5460
<snapshotDeploymentRepository>${snapshot.repository}</snapshotDeploymentRepository>
55-
<!-- The plugin will read the git branch ang git url by resolving these properties at run-time -->
56-
<gitBranchProperty>
5761
</configuration>
5862
<executions>
5963
<execution>
@@ -89,24 +93,24 @@ In practice, the Maven versions should:
8993

9094
* Be synchronized with release branch and hotfix branch names.
9195
* Never be -SNAPSHOT in the master branch, release, or hotfix branches.
92-
* Always be -SNAPSHOT in the development branch.
96+
* Always be -SNAPSHOT in the develop branch.
9397
* Be irrelevant if there's no git branch resolvable from your environment.
9498

9599
The `enforce-versions` goal asserts these semantics when it can resolve the `gitBranchExpression`.
96100

97101
The goal accomplishes this by checking the Maven pom.xml version value, and asserting the -SNAPSHOT status, as well as matching the current branch name
98-
against regular expressions, extracting version numbers from the branch names where applicable. If a regex specifies a subgroup 1, the content of that
99-
subgroup is asserted to equal the version defined in the pom.xml.
102+
against regular expressions, extracting version numbers from the branch names where applicable. If a regex specifies subgroups, the content of the
103+
last subgroup is asserted to equal the version defined in the pom.xml.
100104

101105
The following properties change the behavior of this goal:
102106

103107
| Property | Default Value | SNAPSHOT allowed? | Description |
104108
| -------------------- | ------------- | --------------------------- | ----------- |
105-
| gitBranchExpression | ${env.GIT_BRANCH} | n/a | Maven property expression to resolve in order to determine the current git branch |
106-
| masterBranchPattern | origin/master | No | Regex. When matched, signals the master branch is being built. Note the lack of a subgroup. |
107-
| releaseBranchPattern | origin/release/(.*) | No | Regex. When matched, signals a release branch being built. Subgroup 1, if present, must match the Maven project version. |
108-
| hotfixBranchPattern | origin/hotfix/(.*) | No | Regex. When matched, signals a hotfix branch is being built. Subgroup 1, if present, must match the Maven project version. |
109-
| developmentBranchPattern | origin/development | Yes | Regex. When matched, signals a development branch is being built. Note the lack of a subgroup. |
109+
| gitBranchExpression | current git branch resolved from SCM or ${env.GIT_BRANCH} | n/a | Maven property expression to resolve in order to determine the current git branch |
110+
| masterBranchPattern | (origin/)?master | No | Regex. When matched, signals the master branch is being built. |
111+
| releaseBranchPattern | (origin/)?release/(.*) | No | Regex. When matched, signals a release branch being built. Last subgroup, if present, must match the Maven project version. |
112+
| hotfixBranchPattern | (origin/)?hotfix/(.*) | No | Regex. When matched, signals a hotfix branch is being built. Last subgroup, if present, must match the Maven project version. |
113+
| developmentBranchPattern | (origin/)?develop | Yes | Regex. When matched, signals a development branch is being built. Note the lack of a subgroup. |
110114

111115
## Goal: `retarget-deploy` (Branch Specific Deploy Targets & Staging)
112116

@@ -122,7 +126,7 @@ plugins in the build process (deploy, site-deploy, etc.) will use the repositori
122126

123127
| Property | Default Value | Description |
124128
| -------- | ------------- | ----------- |
125-
| gitBranchExpression | ${env.GIT_BRANCH} | Maven property expression to resolve in order to determine the current git branch |
129+
| gitBranchExpression | current git branch resolved from SCM or ${env.GIT_BRANCH} | Maven property expression to resolve in order to determine the current git branch |
126130
| releaseDeploymentRepository | n/a | The repository to use for releases. (Builds with a GIT_BRANCH matching `masterBranchPattern`) |
127131
| stageDeploymentRepository | n/a | The repository to use for staging. (Builds with a GIT_BRANCH matching `releaseBranchPattern` or `hotfixBranchPattern` |
128132
| snapshotDeploymentRepository | n/a | The repository to use for snapshots. (Builds matching `developmentBranchPattern` |
@@ -179,26 +183,18 @@ Can be replaced with the following plugin configuration, which also introduces t
179183
In a gitflow environment, a commit to a master branch should trigger a job to build on the master branch, which would result in the release being tagged if successful.
180184

181185
The `tag-master` goal executes the [maven-scm-plugin tag goal](https://maven.apache.org/scm/maven-scm-plugin/tag-mojo.html) when the
182-
`gitBranchExpression` resolves to a value matching the `masterBranchPattern` regular expression. To determine the SCM URL to use, the `gitURLExpression`
183-
is evaluated at run-time. The default expression, `${env.GIT_URL}`, is provided by Jenkins & Hudson.
184-
185-
To resolve the `<developerConnection>` in an `<scm>` block in your pom, you can specify the following in your plugin configuration:
186-
187-
```
188-
<gitURLExpression>${project.scm.developerConnection}</gitURLExpression>
189-
```
186+
`gitBranchExpression` resolves to a value matching the `masterBranchPattern` regular expression. To determine the SCM URL to use, the plugin looks for a
187+
`developerConnection` or `connection` information in an SCM block, and if not found the `gitURLExpression` is evaluated at run-time.
188+
The default expression, `${env.GIT_URL}`, is one that is commonly provided by Jenkins & Hudson.
190189

191190
The following properties can be configured for this goal:
192191

193192
| Property | Default Value | Description |
194193
| -------------------- | ------------- | ----------- |
195-
| gitBranchExpression | ${env.GIT_BRANCH} | Maven property expression to resolve in order to determine the current git branch |
196-
| gitURLExpression | ${env.GIT_URL} | Maven property expression to resolve for the GIT URL connection to use. |
197-
| masterBranchPattern | origin/master | Regex. When matched against the resolved value of `gitBranchExpression` this plugin executes the scm:tag goal using the `gitURLExpression` to resolve the git URL to use. |
194+
| gitBranchExpression | current git branch resolved from SCM or ${env.GIT_BRANCH} | Maven property expression to resolve in order to determine the current git branch |
195+
| gitURLExpression | current git branch resolved from SCM or ${env.GIT_URL} | Maven property expression to resolve for the GIT URL connection to use. |
196+
| masterBranchPattern | (origin/)?master | Regex. When matched against the resolved value of `gitBranchExpression` this plugin executes the scm:tag goal using the `gitURLExpression` to resolve the git URL to use. |
198197
| tag | ${project.version} | An expression to use for the SCM tag. |
199-
| tag.plugin.groupId | org.apache.maven.plugins | The groupId of the plugin to use for tagging. |
200-
| tag.plugin.artifactId | maven-scm-plugin | The artifactId of the plugin to use for tagging. |
201-
| tag.plugin.version | 1.9.4 | The version of the plugin to use for tagging. |
202198

203199

204200
## Goal: `promote-master` and the Build Extension. (Copy Staged Artifacts to Releases)
@@ -271,7 +267,7 @@ that the first build deployed into. Once they're attached to the project, the `j
271267
the artifacts built by the first job into a jboss application server.
272268

273269

274-
## Goal: `set-properties` (Dynamically Set a Maven Project / System Properties)
270+
## Goal: `set-properties` (Dynamically Set Maven Project / System Properties)
275271

276272
Some situations with automated testing (and integration testing in particular) demand changing configuration properties
277273
based upon the branch type being built. This is a common necessity when configuring automated DB refactorings as part of

pom.xml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<groupId>com.e-gineering</groupId>
1010
<artifactId>gitflow-helper-maven-plugin</artifactId>
1111

12-
<version>1.5.2</version>
12+
<version>1.6.0</version>
1313
<packaging>maven-plugin</packaging>
1414

1515
<name>gitflow-helper-maven-plugin</name>
@@ -64,17 +64,6 @@
6464
</distributionManagement>
6565

6666
<dependencies>
67-
<dependency>
68-
<groupId>org.twdata.maven</groupId>
69-
<artifactId>mojo-executor</artifactId>
70-
<version>2.2.0</version>
71-
<exclusions>
72-
<exclusion>
73-
<groupId>org.apache.maven</groupId>
74-
<artifactId>maven-core</artifactId>
75-
</exclusion>
76-
</exclusions>
77-
</dependency>
7867
<dependency>
7968
<groupId>org.apache.maven</groupId>
8069
<artifactId>maven-core</artifactId>
@@ -92,6 +81,21 @@
9281
<artifactId>maven-plugin-api</artifactId>
9382
<version>3.1.0</version>
9483
</dependency>
84+
<dependency>
85+
<groupId>org.apache.maven.scm</groupId>
86+
<artifactId>maven-scm-api</artifactId>
87+
<version>1.9.5</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.apache.maven.scm</groupId>
91+
<artifactId>maven-scm-manager-plexus</artifactId>
92+
<version>1.9.5</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.maven.scm</groupId>
96+
<artifactId>maven-scm-provider-gitexe</artifactId>
97+
<version>1.9.5</version>
98+
</dependency>
9599
<dependency>
96100
<groupId>org.apache.maven.plugin-tools</groupId>
97101
<artifactId>maven-plugin-annotations</artifactId>

src/main/java/com/e_gineering/maven/gitflowhelper/AbstractGitflowBranchMojo.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.maven.plugins.annotations.Component;
99
import org.apache.maven.plugins.annotations.Parameter;
1010
import org.apache.maven.project.MavenProject;
11+
import org.apache.maven.scm.manager.ScmManager;
1112
import org.codehaus.plexus.util.cli.CommandLineUtils;
1213

1314
import java.io.IOException;
@@ -26,16 +27,19 @@ public abstract class AbstractGitflowBranchMojo extends AbstractMojo {
2627
@Component
2728
protected MavenProject project;
2829

29-
@Parameter(defaultValue = "origin/master", property = "masterBranchPattern", required = true)
30+
@Component
31+
protected ScmManager scmManager;
32+
33+
@Parameter(defaultValue = "(origin/)?master", property = "masterBranchPattern", required = true)
3034
private String masterBranchPattern;
3135

32-
@Parameter(defaultValue = "origin/release/(.*)", property = "releaseBranchPattern", required = true)
36+
@Parameter(defaultValue = "(origin/)?release/(.*)", property = "releaseBranchPattern", required = true)
3337
private String releaseBranchPattern;
3438

35-
@Parameter(defaultValue = "origin/hotfix/(.*)", property = "hotfixBranchPattern", required = true)
39+
@Parameter(defaultValue = "(origin/)?hotfix/(.*)", property = "hotfixBranchPattern", required = true)
3640
private String hotfixBranchPattern;
3741

38-
@Parameter(defaultValue = "origin/development", property = "developmentBranchPattern", required = true)
42+
@Parameter(defaultValue = "(origin/)?develop", property = "developmentBranchPattern", required = true)
3943
private String developmentBranchPattern;
4044

4145
// @Parameter tag causes property resolution to fail for patterns containing ${env.}. Default provided in execute();
@@ -61,7 +65,7 @@ private void logExecute(final GitBranchType type, final String gitBranch, final
6165

6266
public void execute() throws MojoExecutionException, MojoFailureException {
6367
if (gitBranchExpression == null) {
64-
gitBranchExpression = "${env.GIT_BRANCH}";
68+
gitBranchExpression = ScmUtils.resolveBranchOrExpression(scmManager, project, getLog());
6569
}
6670

6771
try {
@@ -80,9 +84,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8084

8185
if (!eb.hasMoreLegalPlaceholders()) {
8286
/*
83-
* /origin/master goes to the maven 'release' repo.
84-
* /origin/release/.* , /origin/hotfix/.* , and /origin/bugfix/.* go to the maven 'stage' repo.
85-
* /origin/development goes to the 'snapshot' repo.
87+
* (/origin/)?master goes to the maven 'release' repo.
88+
* (/origin/)?release/(.*) , (/origin/)?hotfix/(.*) , and (/origin/)?bugfix/(.*) go to the maven 'stage' repo.
89+
* (/origin/)?develop goes to the 'snapshot' repo.
8690
* All other builds will use the default semantics for 'deploy'.
8791
*/
8892
if (gitBranch.matches(masterBranchPattern)) {

src/main/java/com/e_gineering/maven/gitflowhelper/EnforceVersionsMojo.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ protected void execute(final GitBranchType type, final String gitBranch, final S
2828
throw new MojoFailureException("The current git branch: [" + gitBranch + "] is defined as a release branch. The maven project version: [" + project.getVersion() + "] is currently a snapshot version.");
2929
}
3030

31-
// If there is a group 1, expect it to match (exactly) the current projectVersion.
32-
if (gitMatcher.groupCount() >= 1) {
33-
if (!gitMatcher.group(1).trim().equals(project.getVersion().trim())) {
34-
throw new MojoFailureException("The current git branch: [" + gitBranch + "] expected the maven project version to be: [" + gitMatcher.group(1).trim() + "], but the maven project version is: [" + project.getVersion() + "]");
31+
// Expect the last group on non-master branches to match (exactly) the current projectVersion. (only release / hotfix branches)
32+
if (gitMatcher.groupCount() > 0 && !GitBranchType.MASTER.equals(type)) {
33+
if (!gitMatcher.group(gitMatcher.groupCount()).trim().equals(project.getVersion().trim())) {
34+
throw new MojoFailureException("The current git branch: [" + gitBranch + "] expected the maven project version to be: [" + gitMatcher.group(gitMatcher.groupCount()).trim() + "], but the maven project version is: [" + project.getVersion() + "]");
3535
}
3636
}
3737
}
38+
} else if (GitBranchType.DEVELOPMENT.equals(type) && !ArtifactUtils.isSnapshot(project.getVersion())) {
39+
throw new MojoFailureException("The current git branch: [" + gitBranch + "] is detected as the gitflow development branch, and expects a maven project version ending with -SNAPSHOT. The maven project version found was: [" + project.getVersion() + "]");
3840
}
3941
}
4042
}

0 commit comments

Comments
 (0)