Skip to content

Commit

Permalink
Merge pull request #124 from egineering-llc/feature/other-branch-reve…
Browse files Browse the repository at this point in the history
…rsioning-extension

Feature/other branch reversioning extension
  • Loading branch information
bvarner committed Jan 14, 2021
2 parents 3a09f95 + 1261c44 commit d37a62a
Show file tree
Hide file tree
Showing 25 changed files with 800 additions and 205 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -403,16 +403,16 @@ the artifacts built by the first job into a jboss application server.

## To Debug the plugin (replicating a test-case but without being run from jUnit)
You can 'bootstrap' the plugin into your local repository and get the test project stubbed by running:
`mvn -Dmaven.test.skip=true install`

Then, change directories:
`cd target/test-classes/project-stub`

From there, you'll need to supply the required environment variables or commandline arguments to `mvnDebug`:
```
mvn process-test-classes
mvn -Dmaven.test.skip=true install
cd target/test-classes/project-stub`
export GIT_BRANCH=origin/feature/mybranch-foo-bar
mvnDebug -Dstub.project.version=5.0.0-SNAPSHOT -DotherBranchDeploy=semver -DallowGitflowPluginSnapshot=true deploy
mvnDebug -Dstub.project.version=5.0.0-SNAPSHOT -DallowGitflowPluginSnapshot=true deploy
```
This will get the test classes into the target directory and install the plugin into your local repository.
Then you move to the proper stub directory, supply the environment variables and arguments to `mvnDebug`.

You can then connect a remote debugger and step through the plugin code.

## Building with IntelliJ IDEA notes
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -8,7 +8,7 @@

<groupId>com.e-gineering</groupId>
<artifactId>gitflow-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.0-SNAPSHOT</version>

<packaging>maven-plugin</packaging>

Expand Down
@@ -0,0 +1,171 @@
package com.e_gineering.maven.gitflowhelper;

import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.manager.ScmManager;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.io.IOException;
import java.util.Properties;

public abstract class AbstractBranchDetectingExtension extends AbstractMavenLifecycleParticipant {
@Requirement
MojoDescriptorCreator descriptorCreator;

@Requirement
Logger logger;

@Requirement
ScmManager scmManager;

boolean pluginFound = false;
String masterBranchPattern;
String supportBranchPattern;
String releaseBranchPattern;
String hotfixBranchPattern;
String developmentBranchPattern;
String featureOrBugfixBranchPattern;
String otherDeployBranchPattern;
String otherBranchVersionDelimiter;
GitBranchInfo branchInfo;
Properties systemEnvVars;

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
try {
systemEnvVars = CommandLineUtils.getSystemEnvVars();
} catch (IOException ioe) {
throw new MavenExecutionException("Unable to read System Envirionment Variables: ", ioe);
}

// Look for a configured gitflow-helper-maven-plugin,
// To determine what the gitBranchExpression and branch patterns are...
String gitBranchExpression = null;

pluginFound = false;
for (MavenProject project : session.getProjects()) {
for (Plugin plugin : project.getModel().getBuild().getPlugins()) {
// Don't drop our plugin. Read it's config
if (plugin.getKey().equals("com.e-gineering:gitflow-helper-maven-plugin")) {
pluginFound = true;

logger.debug("gitflow-helper-maven-plugin found in project: [" + project.getName() + "]");

if (masterBranchPattern == null) {
masterBranchPattern = extractPluginConfigValue("masterBranchPattern", plugin);
}

if (supportBranchPattern == null) {
supportBranchPattern = extractPluginConfigValue("supportBranchPattern", plugin);
}

if (releaseBranchPattern == null) {
releaseBranchPattern = extractPluginConfigValue("releaseBranchPattern", plugin);
}

if (hotfixBranchPattern == null) {
hotfixBranchPattern = extractPluginConfigValue("hotfixBranchPattern", plugin);
}

if (developmentBranchPattern == null) {
developmentBranchPattern = extractPluginConfigValue("developmentBranchPattern", plugin);
}

if (featureOrBugfixBranchPattern == null) {
featureOrBugfixBranchPattern = extractPluginConfigValue("featureOrBugfixBranchPattern", plugin);
}

if (otherDeployBranchPattern == null) {
otherDeployBranchPattern = extractPluginConfigValue("otherDeployBranchPattern", plugin);
}

if (otherBranchVersionDelimiter == null) {
otherBranchVersionDelimiter = extractPluginConfigValue("otherBranchVersionDelimiter", plugin);
}

if (gitBranchExpression == null) {
gitBranchExpression = extractPluginConfigValue("gitBranchExpression", plugin);
}
}
}
}

// Any missing configuration options need to be defaulted.
if (pluginFound) {
if (masterBranchPattern == null) {
logger.debug("Using default master branch Pattern.");
masterBranchPattern = "(origin/)?master";
}
logger.debug("Master Branch Pattern: " + masterBranchPattern);

if (supportBranchPattern == null) {
logger.debug("Using default support branch Pattern.");
supportBranchPattern = "(origin/)?support/(.*)";
}
logger.debug("Support Branch Pattern: " + supportBranchPattern);

if (releaseBranchPattern == null) {
logger.debug("Using default release branch Pattern.");
releaseBranchPattern = "(origin/)?release/(.*)";
}
logger.debug("Release Branch Pattern: " + releaseBranchPattern);

if (hotfixBranchPattern == null) {
logger.debug("Using default hotfix branch Pattern.");
hotfixBranchPattern = "(origin/)?hotfix/(.*)";
}
logger.debug("Hotfix Branch Pattern: " + hotfixBranchPattern);

if (developmentBranchPattern == null) {
logger.debug("Using default development Pattern.");
developmentBranchPattern = "(origin/)?develop";
}
logger.debug("Development Branch Pattern: " + developmentBranchPattern);

if (featureOrBugfixBranchPattern == null) {
logger.debug("Using default feature or bugfix Pattern.");
featureOrBugfixBranchPattern = "(origin/)?(?:feature|bugfix)/(.*)";
}
logger.debug("Feature or Bugfix Branch Pattern: " + featureOrBugfixBranchPattern);

if (otherDeployBranchPattern == null) {
logger.debug("Using default other deployment branch Pattern.");
otherDeployBranchPattern = "";
}
logger.debug("Other Branch Deployment Pattern: " + otherDeployBranchPattern);

if (otherBranchVersionDelimiter == null) {
logger.debug("Using default otherBranchVersionDelimiter.");
otherBranchVersionDelimiter = "+";
}

ScmUtils scmUtils = new ScmUtils(systemEnvVars, scmManager, session.getTopLevelProject(), new PlexusLoggerToMavenLog(logger), masterBranchPattern, supportBranchPattern, releaseBranchPattern, hotfixBranchPattern, developmentBranchPattern);
branchInfo = scmUtils.resolveBranchInfo(gitBranchExpression);
} else {
logger.debug("Unable to configure gitflow-helper-maven-plugin lifecycle extensions. No Plugin configuration found.");
}
}

private String extractPluginConfigValue(String parameter, Plugin plugin) {
String value = extractConfigValue(parameter, plugin.getConfiguration());
for (int i = 0; i < plugin.getExecutions().size() && value == null; i++) {
value = extractConfigValue(parameter, plugin.getExecutions().get(i).getConfiguration());
}
return value;
}

private String extractConfigValue(String parameter, Object configuration) {
try {
return ((Xpp3Dom) configuration).getChild(parameter).getValue();
} catch (Exception ignored) {
}
return null;
}
}
Expand Up @@ -2,17 +2,13 @@

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.shared.utils.StringUtils;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
Expand All @@ -38,7 +34,6 @@
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -75,7 +70,7 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
RepositorySystemSession repositorySystemSession;

@Parameter(defaultValue = "${project.build.directory}", required = true)
private File buildDirectory;
File buildDirectory;

@Component
private RepositorySystem repositorySystem;
Expand Down
@@ -1,7 +1,5 @@
package com.e_gineering.maven.gitflowhelper;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
Expand Down

0 comments on commit d37a62a

Please sign in to comment.