Skip to content

Commit abaa3cb

Browse files
authored
Merge pull request #57 from egineering-llc/feature/tag-inbuilt-scm
Feature/tag inbuilt scm
2 parents 1c5111d + 2237d9c commit abaa3cb

File tree

7 files changed

+56
-89
lines changed

7 files changed

+56
-89
lines changed

README.md

Lines changed: 16 additions & 16 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

@@ -34,10 +35,14 @@ All of the solutions to these issues are implemented independently in different
3435

3536
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.
3637
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.
37-
2. Configure the plugin goals and add the build extension to your Maven project. Here's an example that will get you going quickly...
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...
3839

3940
```
4041
<project>
42+
...
43+
<scm>
44+
<developerConnection>scm:git:ssh://git@server/project/path.git</developerConnection>
45+
</scm>
4146
...
4247
<build>
4348
<plugins>
@@ -53,8 +58,6 @@ All of the solutions to these issues are implemented independently in different
5358
<releaseDeploymentRepository>${release.repository}</releaseDeploymentRepository>
5459
<stageDeploymentRepository>${stage.repository}</stageDeploymentRepository>
5560
<snapshotDeploymentRepository>${snapshot.repository}</snapshotDeploymentRepository>
56-
<!-- The plugin will read the git branch ang git url by resolving these properties at run-time -->
57-
<gitBranchExpression/>
5861
</configuration>
5962
<executions>
6063
<execution>
@@ -90,24 +93,24 @@ In practice, the Maven versions should:
9093

9194
* Be synchronized with release branch and hotfix branch names.
9295
* Never be -SNAPSHOT in the master branch, release, or hotfix branches.
93-
* Always be -SNAPSHOT in the development branch.
96+
* Always be -SNAPSHOT in the develop branch.
9497
* Be irrelevant if there's no git branch resolvable from your environment.
9598

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

98101
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
99-
against regular expressions, extracting version numbers from the branch names where applicable. If a regex specifies a subgroup 1, the content of that
100-
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.
101104

102105
The following properties change the behavior of this goal:
103106

104107
| Property | Default Value | SNAPSHOT allowed? | Description |
105108
| -------------------- | ------------- | --------------------------- | ----------- |
106109
| 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 |
107-
| masterBranchPattern | origin/master | No | Regex. When matched, signals the master branch is being built. Note the lack of a subgroup. |
108-
| releaseBranchPattern | origin/release/(.*) | No | Regex. When matched, signals a release branch being built. Subgroup 1, if present, must match the Maven project version. |
109-
| hotfixBranchPattern | origin/hotfix/(.*) | No | Regex. When matched, signals a hotfix branch is being built. Subgroup 1, if present, must match the Maven project version. |
110-
| developmentBranchPattern | origin/development | Yes | Regex. When matched, signals a development branch is being built. Note the lack of a subgroup. |
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. |
111114

112115
## Goal: `retarget-deploy` (Branch Specific Deploy Targets & Staging)
113116

@@ -190,11 +193,8 @@ The following properties can be configured for this goal:
190193
| -------------------- | ------------- | ----------- |
191194
| gitBranchExpression | current git branch resolved from SCM or ${env.GIT_BRANCH} | Maven property expression to resolve in order to determine the current git branch |
192195
| gitURLExpression | current git branch resolved from SCM or ${env.GIT_URL} | Maven property expression to resolve for the GIT URL connection to use. |
193-
| 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. |
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. |
194197
| tag | ${project.version} | An expression to use for the SCM tag. |
195-
| tag.plugin.groupId | org.apache.maven.plugins | The groupId of the plugin to use for tagging. |
196-
| tag.plugin.artifactId | maven-scm-plugin | The artifactId of the plugin to use for tagging. |
197-
| tag.plugin.version | 1.9.4 | The version of the plugin to use for tagging. |
198198

199199

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

269269

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

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

pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ public abstract class AbstractGitflowBranchMojo extends AbstractMojo {
3030
@Component
3131
protected ScmManager scmManager;
3232

33-
@Parameter(defaultValue = "origin/master", property = "masterBranchPattern", required = true)
33+
@Parameter(defaultValue = "(origin/)?master", property = "masterBranchPattern", required = true)
3434
private String masterBranchPattern;
3535

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

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

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

4545
// @Parameter tag causes property resolution to fail for patterns containing ${env.}. Default provided in execute();
@@ -84,9 +84,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8484

8585
if (!eb.hasMoreLegalPlaceholders()) {
8686
/*
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/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.
9090
* All other builds will use the default semantics for 'deploy'.
9191
*/
9292
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
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
102102
}
103103
}
104104

105-
if (gitBranchExpression == null) {
106-
gitBranchExpression = ScmUtils.resolveBranchOrExpression(scmManager, project, new DefaultLog(logger));
107-
}
108-
109105
pluginsToDrop.put(project, dropPlugins);
110106
}
111107

@@ -117,15 +113,15 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
117113
logger.debug("Master Branch Pattern: " + masterBranchPattern);
118114

119115
if (gitBranchExpression == null) {
120-
logger.debug("Using default gitBranchExpression.");
121-
gitBranchExpression = "${env.GIT_BRANCH}";
116+
logger.debug("Using default branch expression resolver.");
117+
gitBranchExpression = ScmUtils.resolveBranchOrExpression(scmManager, session.getTopLevelProject(), new DefaultLog(logger));
122118
}
123119
logger.debug("Git Branch Expression: " + gitBranchExpression);
124120

125121

126122
PropertyResolver pr = new PropertyResolver();
127123
String gitBranch = pr.resolveValue(gitBranchExpression, session.getCurrentProject().getProperties(), systemEnvVars);
128-
logger.info("Build Extension Resolved: " + gitBranchExpression + " to: " + gitBranch);
124+
logger.info("gitflow-helper-maven-plugin: Build Extension resolved gitBranchExpression: " + gitBranchExpression + " to: " + gitBranch);
129125

130126
// Test to see if the current GIT_BRANCH matches the masterBranchPattern...
131127
if (gitBranch != null && gitBranch.matches(masterBranchPattern)) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ public abstract class ScmUtils {
2121
* Given the ScmManager for the current execution cycle, and the MavenProject structure, determine the SCM URL or
2222
* an expression we can resolve the URL from.
2323
*
24-
* @param scmManager The current maven ScmManager
2524
* @param project The Current maven Project
2625
* @param log A Log to write to
2726
* @return The developerConnection, if none set, the connection, if none set, then the expression, <code>"${env.GIT_URL}"</code>
2827
*/
29-
public static String resolveUrlOrExpression(final ScmManager scmManager, final MavenProject project, final Log log) {
28+
public static String resolveUrlOrExpression(final MavenProject project, final Log log) {
3029
String connectionUrl = null;
3130

3231
// Some projects don't specify SCM Blocks, and instead rely upon the CI server to provide an '${env.GIT_BRANCH}'
@@ -53,7 +52,7 @@ public static String resolveUrlOrExpression(final ScmManager scmManager, final M
5352
* @throws ScmException
5453
*/
5554
public static String resolveBranchOrExpression(final ScmManager scmManager, final MavenProject project, final Log log) {
56-
String connectionUrl = resolveUrlOrExpression(scmManager, project, log);
55+
String connectionUrl = resolveUrlOrExpression(project, log);
5756

5857
// If a connectionURL other than the default expression was resolved, try to resolve the branch.
5958
if (!StringUtils.equals(connectionUrl, DEFAULT_URL_EXPRESSION)) {

0 commit comments

Comments
 (0)