Skip to content

Commit

Permalink
Merge pull request #66 from egineering-llc/release/1.7.0
Browse files Browse the repository at this point in the history
Release/1.7.0
  • Loading branch information
bvarner authored Feb 27, 2017
2 parents a4baaf3 + aa8ec4a commit 12f5c79
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 118 deletions.
147 changes: 110 additions & 37 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<groupId>com.e-gineering</groupId>
<artifactId>gitflow-helper-maven-plugin</artifactId>

<version>1.6.0</version>
<version>1.7.0</version>
<packaging>maven-plugin</packaging>

<name>gitflow-helper-maven-plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.model.Repository;
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.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
Expand All @@ -27,7 +31,6 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
Expand All @@ -54,7 +57,10 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
protected String snapshotDeploymentRepository;

@Parameter(defaultValue = "${repositorySystemSession}", required = true)
private RepositorySystemSession session;
protected RepositorySystemSession session;

@Component
protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;

@Parameter(defaultValue = "${project.build.directory}", required = true)
protected File buildDirectory;
Expand Down Expand Up @@ -82,23 +88,39 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
*/
protected ArtifactRepository getDeploymentRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher(altRepository);
Repository candidate = null;
if (!matcher.matches()) {
throw new MojoFailureException(altRepository, "Invalid syntax for repository.",
"Invalid syntax for repository. Use \"id::layout::url::unique\".");
for (int i = 0; i < project.getRepositories().size(); i++) {
candidate = project.getRepositories().get(i);
getLog().debug("Checking defined repository ID: " + candidate.getId().trim() + " against: " + altRepository.trim());
if (candidate.getId().trim().equals(altRepository.trim())) {
break;
}
candidate = null;
}

if (candidate == null) {
throw new MojoFailureException(altRepository, "Invalid syntax for repository or repository id not resolved..",
"Invalid syntax for repository. Use \"id::layout::url::unique\" or only specify the \"id\".");
}
}

if (getLog().isDebugEnabled()) {
getLog().debug("Getting maven deployment repository (to target artifacts) for: " + altRepository);
}

String id = matcher.group(1).trim();
String layout = matcher.group(2).trim();
String url = matcher.group(3).trim();
boolean unique = Boolean.parseBoolean(matcher.group(4).trim());
if (candidate == null) {
String id = matcher.group(1).trim();
String layout = matcher.group(2).trim();
String url = matcher.group(3).trim();
boolean unique = Boolean.parseBoolean(matcher.group(4).trim());

ArtifactRepositoryLayout repoLayout = getLayout(layout);
ArtifactRepositoryLayout repoLayout = getLayout(layout);

return repositoryFactory.createDeploymentArtifactRepository(id, url, repoLayout, unique);
return repositoryFactory.createDeploymentArtifactRepository(id, url, repoLayout, unique);
} else {
return repositoryFactory.createDeploymentArtifactRepository(candidate.getId(), candidate.getUrl(), getLayout(candidate.getLayout()), candidate.getSnapshots().isEnabled());
}
}

/**
Expand All @@ -109,7 +131,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
* @throws MojoExecutionException
* @throws MojoFailureException
*/
private RemoteRepository getRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
protected RemoteRepository getRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
if (getLog().isDebugEnabled()) {
getLog().debug("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository);
}
Expand Down Expand Up @@ -137,15 +159,14 @@ private RemoteRepository getRepository(final String altRepository) throws MojoEx
}

private String getCoordinates(ArtifactResult result) {
StringBuilder buffer = new StringBuilder( 128 );
buffer.append( result.getArtifact().getGroupId() );
buffer.append( ':' ).append( result.getArtifact().getArtifactId() );
buffer.append( ':' ).append( result.getArtifact().getExtension() );
if ( result.getArtifact().getClassifier().length() > 0 )
{
buffer.append( ':' ).append( result.getArtifact().getClassifier() );
StringBuilder buffer = new StringBuilder(128);
buffer.append(result.getArtifact().getGroupId());
buffer.append(':').append(result.getArtifact().getArtifactId());
buffer.append(':').append(result.getArtifact().getExtension());
if (result.getArtifact().getClassifier().length() > 0) {
buffer.append(':').append(result.getArtifact().getClassifier());
}
buffer.append( ':' ).append( result.getArtifact().getBaseVersion() );
buffer.append(':').append(result.getArtifact().getBaseVersion());
return buffer.toString();
}

Expand Down Expand Up @@ -183,47 +204,46 @@ private String getCoordinates(org.apache.maven.artifact.Artifact artifact) {
* group:artifact:type:classifier:version
*/
protected void attachArtifactCatalog() throws MojoExecutionException {
getLog().info("Cataloging Artifacts for promotion & reattachment: " + project.getBuild().getDirectory());
getLog().info("Cataloging Artifacts for promotion & reattachment: " + project.getBuild().getDirectory());

File catalog = new File(buildDirectory, project.getArtifact().getArtifactId() + ".txt");
File catalog = new File(buildDirectory, project.getArtifact().getArtifactId() + ".txt");

PrintWriter writer = null;
PrintWriter writer = null;

try {
catalog.delete();
buildDirectory.mkdirs();
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), Charset.forName("UTF-8")));

if (project.getArtifact() != null && project.getArtifact().getFile() != null &&
project.getArtifact().getFile().exists() && !project.getArtifact().getFile().isDirectory())
{
String coords = getCoordinates(project.getArtifact());
if (!coords.isEmpty()){
getLog().info("Cataloging: " + coords);
writer.println(coords);
}
} else {
getLog().info("No primary artifact to catalog, cataloging attached artifacts instead.");
try {
catalog.delete();
buildDirectory.mkdirs();
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), Charset.forName("UTF-8")));

if (project.getArtifact() != null && project.getArtifact().getFile() != null &&
project.getArtifact().getFile().exists() && !project.getArtifact().getFile().isDirectory()) {
String coords = getCoordinates(project.getArtifact());
if (!coords.isEmpty()) {
getLog().info("Cataloging: " + coords);
writer.println(coords);
}
} else {
getLog().info("No primary artifact to catalog, cataloging attached artifacts instead.");
}

// Iterate the attached artifacts.
for (org.apache.maven.artifact.Artifact artifact : project.getAttachedArtifacts()) {
String coords = getCoordinates(artifact);
if (!coords.isEmpty()) {
getLog().info("Cataloging: " + coords);
writer.println(coords);
}
// Iterate the attached artifacts.
for (org.apache.maven.artifact.Artifact artifact : project.getAttachedArtifacts()) {
String coords = getCoordinates(artifact);
if (!coords.isEmpty()) {
getLog().info("Cataloging: " + coords);
writer.println(coords);
}
}

getLog().info("Attaching catalog artifact: " + catalog);
projectHelper.attachArtifact(project, "txt", "catalog", catalog);
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to create catalog of artifacts", ioe);
} finally {
if (writer != null) {
writer.close();
}
getLog().info("Attaching catalog artifact: " + catalog);
projectHelper.attachArtifact(project, "txt", "catalog", catalog);
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to create catalog of artifacts", ioe);
} finally {
if (writer != null) {
writer.close();
}
}
}

/**
Expand All @@ -250,35 +270,30 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
// A place to store our resolved files...
List<ArtifactResult> resolvedArtifacts = new ArrayList<ArtifactResult>();

// Keep track of the original base directory.
Field localBaseDir = null;
File originalBaseDir = session.getLocalRepositoryManager().getRepository().getBasedir();

// Disable the local repository - using a bit of reflection that I wish we didn't need to use.
// Use a custom repository session, setup to force a few behaviors we like.
DefaultRepositorySystemSession tempSession = new DefaultRepositorySystemSession(session);
tempSession.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);

File tempRepo = null;
if (disableLocal) {
getLog().info("Disabling local repository @ " + session.getLocalRepository().getBasedir());
getLog().info("Disabling local repository @ " + tempSession.getLocalRepository().getBasedir());
try {
localBaseDir = LocalRepository.class.getDeclaredField("basedir");
localBaseDir.setAccessible(true);

// Generate a new temp directory.
tempRepo = Files.createTempDirectory("gitflow-helper-maven-plugin-repo").toFile();

getLog().info("Using temporary local repository @ " + tempRepo.getAbsolutePath());
localBaseDir.set(session.getLocalRepositoryManager().getRepository(), tempRepo);
tempSession.setLocalRepositoryManager(localRepositoryManagerFactory.newInstance(tempSession, new LocalRepository(tempRepo)));
} catch (Exception ex) {
getLog().warn("Failed to disable local repository path.", ex);
}
}


List<ArtifactRequest> requiredArtifacts = new ArrayList<ArtifactRequest>();

// Locate our text catalog classifier file. :-)
BufferedReader reader = null;
try {
ArtifactResult catalogResult = artifactResolver.resolveArtifact(session, new ArtifactRequest(new DefaultArtifact(project.getGroupId(), project.getArtifactId(), "catalog", "txt", project.getVersion()), remoteRepositories, null));
ArtifactResult catalogResult = artifactResolver.resolveArtifact(tempSession, new ArtifactRequest(new DefaultArtifact(project.getGroupId(), project.getArtifactId(), "catalog", "txt", project.getVersion()), remoteRepositories, null));
resolvedArtifacts.add(catalogResult);

if (catalogResult.isResolved()) {
Expand All @@ -301,14 +316,15 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {}
} catch (IOException ioe) {
}
}
}


// Resolve the artifacts from the catalog (if there are any)
try {
resolvedArtifacts.addAll(artifactResolver.resolveArtifacts(session, requiredArtifacts));
resolvedArtifacts.addAll(artifactResolver.resolveArtifacts(tempSession, requiredArtifacts));
} catch (ArtifactResolutionException are) {
throw new MojoExecutionException("Failed to resolve the required project files from: " + sourceRepository, are);
}
Expand Down Expand Up @@ -339,12 +355,6 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool

// Restore the local repository, again using reflection.
if (disableLocal) {
try {
localBaseDir.set(session.getLocalRepositoryManager().getRepository(), originalBaseDir);
localBaseDir.setAccessible(false);
} catch (Exception ex) {
getLog().warn("Failed to restore original local repository path.", ex);
}
if (tempRepo != null) {
try {
FileUtils.deleteDirectory(tempRepo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public abstract class AbstractGitflowBranchMojo extends AbstractMojo {
@Parameter(defaultValue = "(origin/)?master", property = "masterBranchPattern", required = true)
private String masterBranchPattern;

@Parameter(defaultValue = "(origin/)?support/(.*)", property = "supportBranchPattern", required = true)
private String supportBranchPattern;

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

Expand Down Expand Up @@ -91,6 +94,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
*/
if (gitBranch.matches(masterBranchPattern)) {
logExecute(GitBranchType.MASTER, gitBranch, masterBranchPattern);
} else if (gitBranch.matches(supportBranchPattern)) {
logExecute(GitBranchType.SUPPORT, gitBranch, supportBranchPattern);
} else if (gitBranch.matches(releaseBranchPattern)) {
logExecute(GitBranchType.RELEASE, gitBranch, releaseBranchPattern);
} else if (gitBranch.matches(hotfixBranchPattern)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class AttachDeployedArtifactsMojo extends AbstractGitflowBasedRepositoryM
@Override
protected void execute(GitBranchType type, String gitBranch, String branchPattern) throws MojoExecutionException, MojoFailureException {
switch (type) {
case MASTER: {
case MASTER:
case SUPPORT:
{
getLog().info("Attaching artifacts from release repository...");
attachExistingArtifacts(releaseDeploymentRepository, true);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ protected void execute(final GitBranchType type, final String gitBranch, final S
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.");
}

// Expect the last group on non-master branches to match (exactly) the current projectVersion. (only release / hotfix branches)
if (gitMatcher.groupCount() > 0 && !GitBranchType.MASTER.equals(type)) {
if (!gitMatcher.group(gitMatcher.groupCount()).trim().equals(project.getVersion().trim())) {
// Non-master version branches require a pom version match of some kind to the branch subgroups.
if (gitMatcher.groupCount() > 0) {
// HOTFIX and RELEASE branches require an exact match to the last subgroup.
if ((GitBranchType.RELEASE.equals(type) || GitBranchType.HOTFIX.equals(type)) && !gitMatcher.group(gitMatcher.groupCount()).trim().equals(project.getVersion().trim())) {
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() + "]");
}

// SUPPORT branches require a 'starts with' match of the maven project version to the subgroup.
// ex: /origin/support/3.1 must have a maven version that starts with "3.1", ala: "3.1.2"
if (GitBranchType.SUPPORT.equals(type) && !project.getVersion().startsWith(gitMatcher.group(gitMatcher.groupCount()).trim())) {
throw new MojoFailureException("The current git branch: [" + gitBranch + "] expected the maven project version to start with: [" + gitMatcher.group(gitMatcher.groupCount()).trim() + "], but the maven project version is: [" + project.getVersion() + "]");
}
}
}
} else if (GitBranchType.DEVELOPMENT.equals(type) && !ArtifactUtils.isSnapshot(project.getVersion())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/
public enum GitBranchType {
MASTER,
SUPPORT,
RELEASE,
HOTFIX,
DEVELOPMENT,
OTHER,
UNDEFINED;

static final EnumSet<GitBranchType> VERSIONED_TYPES = EnumSet.of(GitBranchType.MASTER, GitBranchType.RELEASE, GitBranchType.HOTFIX);
static final EnumSet<GitBranchType> VERSIONED_TYPES = EnumSet.of(GitBranchType.MASTER, GitBranchType.SUPPORT, GitBranchType.RELEASE, GitBranchType.HOTFIX);
}
Loading

0 comments on commit 12f5c79

Please sign in to comment.