Skip to content

Commit e89d576

Browse files
committed
* NullPointerException when a project doesn't define SCM blocks.
* Centralized the resolution of the git branch name down to the default expression into the single util method without exceptions throwing up the stack. * Works on multi-module projects that _do_not_ have an SCM block and do not execute the 'tag' step.
1 parent d24ee95 commit e89d576

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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.ScmException;
1211
import org.apache.maven.scm.manager.ScmManager;
1312
import org.codehaus.plexus.util.cli.CommandLineUtils;
1413

@@ -66,7 +65,7 @@ private void logExecute(final GitBranchType type, final String gitBranch, final
6665

6766
public void execute() throws MojoExecutionException, MojoFailureException {
6867
if (gitBranchExpression == null) {
69-
gitBranchExpression = getGitBranch();
68+
gitBranchExpression = ScmUtils.resolveBranchOrExpression(scmManager, project, getLog());
7069
}
7170

7271
try {
@@ -105,16 +104,4 @@ public void execute() throws MojoExecutionException, MojoFailureException {
105104
logExecute(GitBranchType.UNDEFINED, gitBranch, null);
106105
}
107106
}
108-
109-
private String getGitBranch() throws MojoExecutionException {
110-
try {
111-
String branch = ScmUtils.getGitBranch(scmManager, project);
112-
if (branch == null) {
113-
throw new MojoExecutionException("gitflow-helper-maven-plugin requires a Git project, use gitBranchExpression to by-pass this check");
114-
}
115-
return branch;
116-
} catch (ScmException e) {
117-
throw new MojoExecutionException("Cannot get the branch information from the git repository: \n" + e.getLocalizedMessage(), e);
118-
}
119-
}
120107
}

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
import org.apache.maven.execution.MavenSession;
77
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
88
import org.apache.maven.model.Plugin;
9+
import org.apache.maven.monitor.logging.DefaultLog;
910
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
10-
import org.apache.maven.plugins.annotations.Parameter;
1111
import org.apache.maven.project.MavenProject;
12-
import org.apache.maven.scm.ScmException;
1312
import org.apache.maven.scm.manager.ScmManager;
1413
import org.codehaus.plexus.component.annotations.Component;
1514
import org.codehaus.plexus.component.annotations.Requirement;
1615
import org.codehaus.plexus.logging.Logger;
1716
import org.codehaus.plexus.util.cli.CommandLineUtils;
1817
import org.codehaus.plexus.util.xml.Xpp3Dom;
1918

20-
import java.io.File;
2119
import java.io.IOException;
2220
import java.util.ArrayList;
2321
import java.util.HashMap;
@@ -92,10 +90,10 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
9290
if (gitBranchExpression == null) {
9391
gitBranchExpression = extractPluginConfigValue("gitBranchExpression", plugin);
9492
}
95-
// Don't drop things we declare goals for.
93+
// Don't drop things we declare goals for.
9694
} else if (pluginsToRetain.contains(plugin)) {
9795
logger.debug("gitflow-helper-maven-plugin retaining plugin: " + plugin + " from project: " + project.getName());
98-
// Don't drop the maven-deploy-plugin
96+
// Don't drop the maven-deploy-plugin
9997
} else if (plugin.getKey().equals("org.apache.maven.plugins:maven-deploy-plugin")) {
10098
logger.debug("gitflow-helper-maven-plugin retaining plugin: " + plugin + " from project: " + project.getName());
10199
} else {
@@ -105,7 +103,7 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
105103
}
106104

107105
if (gitBranchExpression == null) {
108-
gitBranchExpression = getGitBranch(project);
106+
gitBranchExpression = ScmUtils.resolveBranchOrExpression(scmManager, project, new DefaultLog(logger));
109107
}
110108

111109
pluginsToDrop.put(project, dropPlugins);
@@ -156,16 +154,4 @@ private String extractConfigValue(String parameter, Object configuration) {
156154
}
157155
return null;
158156
}
159-
160-
private String getGitBranch(MavenProject project) throws MavenExecutionException {
161-
try {
162-
String branch = ScmUtils.getGitBranch(scmManager, project);
163-
if (branch == null) {
164-
throw new MavenExecutionException("gitflow-helper-maven-plugin requires a Git project, use gitBranchExpression to by-pass this check", project.getFile());
165-
}
166-
return branch;
167-
} catch (ScmException e) {
168-
throw new MavenExecutionException("Cannot get the branch information from the git repository: \n" + e.getLocalizedMessage(), e);
169-
}
170-
}
171157
}
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.e_gineering.maven.gitflowhelper;
22

33
import org.apache.commons.lang.StringUtils;
4+
import org.apache.maven.plugin.logging.Log;
45
import org.apache.maven.project.MavenProject;
56
import org.apache.maven.scm.ScmException;
6-
import org.apache.maven.scm.ScmFile;
77
import org.apache.maven.scm.ScmFileSet;
88
import org.apache.maven.scm.log.ScmLogDispatcher;
99
import org.apache.maven.scm.manager.ScmManager;
@@ -13,21 +13,45 @@
1313
import org.apache.maven.scm.repository.ScmRepository;
1414

1515
public abstract class ScmUtils {
16-
public static String getGitBranch(ScmManager scmManager, MavenProject project) throws ScmException {
17-
String scmConnectionUrl = project.getScm().getConnection();
18-
String scmDeveloperConnectionUrl = project.getScm().getDeveloperConnection();
19-
String connectionUrl = StringUtils.isNotBlank(scmDeveloperConnectionUrl) ? scmDeveloperConnectionUrl : scmConnectionUrl;
20-
if (StringUtils.isBlank(connectionUrl)) {
21-
return "${env.GIT_BRANCH}";
16+
/**
17+
* Given the ScmManager for the current execution cycle, and the MavenProject structure, determine if we can
18+
* find a maven-provided manner of resolving the current git branch.
19+
*
20+
* @param scmManager The current maven ScmManager
21+
* @param project The Current maven Project
22+
* @param log A Log to write to
23+
* @return The current git branch name, or <code>${env.GIT_BRACH}</code> if the current git branch could not be resolved.
24+
* @throws ScmException
25+
*/
26+
public static String resolveBranchOrExpression(final ScmManager scmManager, final MavenProject project, final Log log) {
27+
String connectionUrl = null;
28+
29+
// Some projects don't specify SCM Blocks, and instead rely upon the CI server to provide an '${env.GIT_BRANCH}'
30+
if (project.getScm() != null) {
31+
// Start with the developer connection, then fall back to the non-developer connection.
32+
connectionUrl = project.getScm().getDeveloperConnection();
33+
if (StringUtils.isBlank(connectionUrl)) {
34+
connectionUrl = project.getScm().getConnection();
35+
}
2236
}
2337

24-
ScmRepository repository = scmManager.makeScmRepository(connectionUrl);
25-
ScmProvider provider = scmManager.getProviderByRepository(repository);
26-
if (!GitScmProviderRepository.PROTOCOL_GIT.equals(provider.getScmType())) {
27-
return null;
28-
} else {
29-
ScmFileSet fileSet = new ScmFileSet(project.getBasedir());
30-
return GitBranchCommand.getCurrentBranch(new ScmLogDispatcher(), (GitScmProviderRepository)repository.getProviderRepository(), fileSet);
38+
if (StringUtils.isNotBlank(connectionUrl)) {
39+
try {
40+
ScmRepository repository = scmManager.makeScmRepository(connectionUrl);
41+
ScmProvider provider = scmManager.getProviderByRepository(repository);
42+
43+
if (GitScmProviderRepository.PROTOCOL_GIT.equals(provider.getScmType())) {
44+
ScmFileSet fileSet = new ScmFileSet(project.getBasedir());
45+
return GitBranchCommand.getCurrentBranch(new ScmLogDispatcher(), (GitScmProviderRepository) repository.getProviderRepository(), fileSet);
46+
} else {
47+
log.warn("Project SCM defines a non-git SCM provider. Falling back to variable resolution.");
48+
}
49+
} catch (ScmException se) {
50+
log.warn("Unable to resolve Git Branch from Project SCM definition.", se);
51+
}
3152
}
53+
54+
log.debug("Git branch unresolvable from Project SCM definition, defaulting to ${env.GIT_BRANCH}");
55+
return "${env.GIT_BRANCH}";
3256
}
3357
}

0 commit comments

Comments
 (0)