Skip to content

Commit 12f5c79

Browse files
authored
Merge pull request #66 from egineering-llc/release/1.7.0
Release/1.7.0
2 parents a4baaf3 + aa8ec4a commit 12f5c79

13 files changed

+333
-118
lines changed

README.md

Lines changed: 110 additions & 37 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 1 deletion
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.6.0</version>
12+
<version>1.7.0</version>
1313
<packaging>maven-plugin</packaging>
1414

1515
<name>gitflow-helper-maven-plugin</name>

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

Lines changed: 82 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import org.apache.maven.artifact.repository.ArtifactRepository;
44
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
55
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
6+
import org.apache.maven.model.Repository;
67
import org.apache.maven.plugin.MojoExecutionException;
78
import org.apache.maven.plugin.MojoFailureException;
89
import org.apache.maven.plugins.annotations.Component;
910
import org.apache.maven.plugins.annotations.Parameter;
1011
import org.apache.maven.project.MavenProjectHelper;
1112
import org.codehaus.plexus.util.FileUtils;
13+
import org.eclipse.aether.DefaultRepositorySystemSession;
1214
import org.eclipse.aether.RepositorySystemSession;
1315
import org.eclipse.aether.artifact.DefaultArtifact;
1416
import org.eclipse.aether.impl.ArtifactResolver;
17+
import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
1518
import org.eclipse.aether.repository.LocalRepository;
1619
import org.eclipse.aether.repository.RemoteRepository;
20+
import org.eclipse.aether.repository.RepositoryPolicy;
1721
import org.eclipse.aether.resolution.ArtifactRequest;
1822
import org.eclipse.aether.resolution.ArtifactResolutionException;
1923
import org.eclipse.aether.resolution.ArtifactResult;
@@ -27,7 +31,6 @@
2731
import java.io.InputStreamReader;
2832
import java.io.OutputStreamWriter;
2933
import java.io.PrintWriter;
30-
import java.lang.reflect.Field;
3134
import java.nio.charset.Charset;
3235
import java.nio.file.Files;
3336
import java.util.ArrayList;
@@ -54,7 +57,10 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
5457
protected String snapshotDeploymentRepository;
5558

5659
@Parameter(defaultValue = "${repositorySystemSession}", required = true)
57-
private RepositorySystemSession session;
60+
protected RepositorySystemSession session;
61+
62+
@Component
63+
protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory;
5864

5965
@Parameter(defaultValue = "${project.build.directory}", required = true)
6066
protected File buildDirectory;
@@ -82,23 +88,39 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
8288
*/
8389
protected ArtifactRepository getDeploymentRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
8490
Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher(altRepository);
91+
Repository candidate = null;
8592
if (!matcher.matches()) {
86-
throw new MojoFailureException(altRepository, "Invalid syntax for repository.",
87-
"Invalid syntax for repository. Use \"id::layout::url::unique\".");
93+
for (int i = 0; i < project.getRepositories().size(); i++) {
94+
candidate = project.getRepositories().get(i);
95+
getLog().debug("Checking defined repository ID: " + candidate.getId().trim() + " against: " + altRepository.trim());
96+
if (candidate.getId().trim().equals(altRepository.trim())) {
97+
break;
98+
}
99+
candidate = null;
100+
}
101+
102+
if (candidate == null) {
103+
throw new MojoFailureException(altRepository, "Invalid syntax for repository or repository id not resolved..",
104+
"Invalid syntax for repository. Use \"id::layout::url::unique\" or only specify the \"id\".");
105+
}
88106
}
89107

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

94-
String id = matcher.group(1).trim();
95-
String layout = matcher.group(2).trim();
96-
String url = matcher.group(3).trim();
97-
boolean unique = Boolean.parseBoolean(matcher.group(4).trim());
112+
if (candidate == null) {
113+
String id = matcher.group(1).trim();
114+
String layout = matcher.group(2).trim();
115+
String url = matcher.group(3).trim();
116+
boolean unique = Boolean.parseBoolean(matcher.group(4).trim());
98117

99-
ArtifactRepositoryLayout repoLayout = getLayout(layout);
118+
ArtifactRepositoryLayout repoLayout = getLayout(layout);
100119

101-
return repositoryFactory.createDeploymentArtifactRepository(id, url, repoLayout, unique);
120+
return repositoryFactory.createDeploymentArtifactRepository(id, url, repoLayout, unique);
121+
} else {
122+
return repositoryFactory.createDeploymentArtifactRepository(candidate.getId(), candidate.getUrl(), getLayout(candidate.getLayout()), candidate.getSnapshots().isEnabled());
123+
}
102124
}
103125

104126
/**
@@ -109,7 +131,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
109131
* @throws MojoExecutionException
110132
* @throws MojoFailureException
111133
*/
112-
private RemoteRepository getRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
134+
protected RemoteRepository getRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
113135
if (getLog().isDebugEnabled()) {
114136
getLog().debug("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository);
115137
}
@@ -137,15 +159,14 @@ private RemoteRepository getRepository(final String altRepository) throws MojoEx
137159
}
138160

139161
private String getCoordinates(ArtifactResult result) {
140-
StringBuilder buffer = new StringBuilder( 128 );
141-
buffer.append( result.getArtifact().getGroupId() );
142-
buffer.append( ':' ).append( result.getArtifact().getArtifactId() );
143-
buffer.append( ':' ).append( result.getArtifact().getExtension() );
144-
if ( result.getArtifact().getClassifier().length() > 0 )
145-
{
146-
buffer.append( ':' ).append( result.getArtifact().getClassifier() );
162+
StringBuilder buffer = new StringBuilder(128);
163+
buffer.append(result.getArtifact().getGroupId());
164+
buffer.append(':').append(result.getArtifact().getArtifactId());
165+
buffer.append(':').append(result.getArtifact().getExtension());
166+
if (result.getArtifact().getClassifier().length() > 0) {
167+
buffer.append(':').append(result.getArtifact().getClassifier());
147168
}
148-
buffer.append( ':' ).append( result.getArtifact().getBaseVersion() );
169+
buffer.append(':').append(result.getArtifact().getBaseVersion());
149170
return buffer.toString();
150171
}
151172

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

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

190-
PrintWriter writer = null;
211+
PrintWriter writer = null;
191212

192-
try {
193-
catalog.delete();
194-
buildDirectory.mkdirs();
195-
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), Charset.forName("UTF-8")));
196-
197-
if (project.getArtifact() != null && project.getArtifact().getFile() != null &&
198-
project.getArtifact().getFile().exists() && !project.getArtifact().getFile().isDirectory())
199-
{
200-
String coords = getCoordinates(project.getArtifact());
201-
if (!coords.isEmpty()){
202-
getLog().info("Cataloging: " + coords);
203-
writer.println(coords);
204-
}
205-
} else {
206-
getLog().info("No primary artifact to catalog, cataloging attached artifacts instead.");
213+
try {
214+
catalog.delete();
215+
buildDirectory.mkdirs();
216+
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(catalog), Charset.forName("UTF-8")));
217+
218+
if (project.getArtifact() != null && project.getArtifact().getFile() != null &&
219+
project.getArtifact().getFile().exists() && !project.getArtifact().getFile().isDirectory()) {
220+
String coords = getCoordinates(project.getArtifact());
221+
if (!coords.isEmpty()) {
222+
getLog().info("Cataloging: " + coords);
223+
writer.println(coords);
207224
}
225+
} else {
226+
getLog().info("No primary artifact to catalog, cataloging attached artifacts instead.");
227+
}
208228

209-
// Iterate the attached artifacts.
210-
for (org.apache.maven.artifact.Artifact artifact : project.getAttachedArtifacts()) {
211-
String coords = getCoordinates(artifact);
212-
if (!coords.isEmpty()) {
213-
getLog().info("Cataloging: " + coords);
214-
writer.println(coords);
215-
}
229+
// Iterate the attached artifacts.
230+
for (org.apache.maven.artifact.Artifact artifact : project.getAttachedArtifacts()) {
231+
String coords = getCoordinates(artifact);
232+
if (!coords.isEmpty()) {
233+
getLog().info("Cataloging: " + coords);
234+
writer.println(coords);
216235
}
236+
}
217237

218-
getLog().info("Attaching catalog artifact: " + catalog);
219-
projectHelper.attachArtifact(project, "txt", "catalog", catalog);
220-
} catch (IOException ioe) {
221-
throw new MojoExecutionException("Failed to create catalog of artifacts", ioe);
222-
} finally {
223-
if (writer != null) {
224-
writer.close();
225-
}
238+
getLog().info("Attaching catalog artifact: " + catalog);
239+
projectHelper.attachArtifact(project, "txt", "catalog", catalog);
240+
} catch (IOException ioe) {
241+
throw new MojoExecutionException("Failed to create catalog of artifacts", ioe);
242+
} finally {
243+
if (writer != null) {
244+
writer.close();
226245
}
246+
}
227247
}
228248

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

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

257-
// Disable the local repository - using a bit of reflection that I wish we didn't need to use.
274+
// Use a custom repository session, setup to force a few behaviors we like.
275+
DefaultRepositorySystemSession tempSession = new DefaultRepositorySystemSession(session);
276+
tempSession.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
277+
258278
File tempRepo = null;
259279
if (disableLocal) {
260-
getLog().info("Disabling local repository @ " + session.getLocalRepository().getBasedir());
280+
getLog().info("Disabling local repository @ " + tempSession.getLocalRepository().getBasedir());
261281
try {
262-
localBaseDir = LocalRepository.class.getDeclaredField("basedir");
263-
localBaseDir.setAccessible(true);
264-
265-
// Generate a new temp directory.
266282
tempRepo = Files.createTempDirectory("gitflow-helper-maven-plugin-repo").toFile();
267283

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

275-
276291
List<ArtifactRequest> requiredArtifacts = new ArrayList<ArtifactRequest>();
277292

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

284299
if (catalogResult.isResolved()) {
@@ -301,14 +316,15 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
301316
if (reader != null) {
302317
try {
303318
reader.close();
304-
} catch (IOException ioe) {}
319+
} catch (IOException ioe) {
320+
}
305321
}
306322
}
307323

308324

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

340356
// Restore the local repository, again using reflection.
341357
if (disableLocal) {
342-
try {
343-
localBaseDir.set(session.getLocalRepositoryManager().getRepository(), originalBaseDir);
344-
localBaseDir.setAccessible(false);
345-
} catch (Exception ex) {
346-
getLog().warn("Failed to restore original local repository path.", ex);
347-
}
348358
if (tempRepo != null) {
349359
try {
350360
FileUtils.deleteDirectory(tempRepo);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public abstract class AbstractGitflowBranchMojo extends AbstractMojo {
3333
@Parameter(defaultValue = "(origin/)?master", property = "masterBranchPattern", required = true)
3434
private String masterBranchPattern;
3535

36+
@Parameter(defaultValue = "(origin/)?support/(.*)", property = "supportBranchPattern", required = true)
37+
private String supportBranchPattern;
38+
3639
@Parameter(defaultValue = "(origin/)?release/(.*)", property = "releaseBranchPattern", required = true)
3740
private String releaseBranchPattern;
3841

@@ -91,6 +94,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
9194
*/
9295
if (gitBranch.matches(masterBranchPattern)) {
9396
logExecute(GitBranchType.MASTER, gitBranch, masterBranchPattern);
97+
} else if (gitBranch.matches(supportBranchPattern)) {
98+
logExecute(GitBranchType.SUPPORT, gitBranch, supportBranchPattern);
9499
} else if (gitBranch.matches(releaseBranchPattern)) {
95100
logExecute(GitBranchType.RELEASE, gitBranch, releaseBranchPattern);
96101
} else if (gitBranch.matches(hotfixBranchPattern)) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class AttachDeployedArtifactsMojo extends AbstractGitflowBasedRepositoryM
1616
@Override
1717
protected void execute(GitBranchType type, String gitBranch, String branchPattern) throws MojoExecutionException, MojoFailureException {
1818
switch (type) {
19-
case MASTER: {
19+
case MASTER:
20+
case SUPPORT:
21+
{
2022
getLog().info("Attaching artifacts from release repository...");
2123
attachExistingArtifacts(releaseDeploymentRepository, true);
2224
break;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ 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-
// 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())) {
31+
// Non-master version branches require a pom version match of some kind to the branch subgroups.
32+
if (gitMatcher.groupCount() > 0) {
33+
// HOTFIX and RELEASE branches require an exact match to the last subgroup.
34+
if ((GitBranchType.RELEASE.equals(type) || GitBranchType.HOTFIX.equals(type)) && !gitMatcher.group(gitMatcher.groupCount()).trim().equals(project.getVersion().trim())) {
3435
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() + "]");
3536
}
37+
38+
// SUPPORT branches require a 'starts with' match of the maven project version to the subgroup.
39+
// ex: /origin/support/3.1 must have a maven version that starts with "3.1", ala: "3.1.2"
40+
if (GitBranchType.SUPPORT.equals(type) && !project.getVersion().startsWith(gitMatcher.group(gitMatcher.groupCount()).trim())) {
41+
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() + "]");
42+
}
3643
}
3744
}
3845
} else if (GitBranchType.DEVELOPMENT.equals(type) && !ArtifactUtils.isSnapshot(project.getVersion())) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
*/
88
public enum GitBranchType {
99
MASTER,
10+
SUPPORT,
1011
RELEASE,
1112
HOTFIX,
1213
DEVELOPMENT,
1314
OTHER,
1415
UNDEFINED;
1516

16-
static final EnumSet<GitBranchType> VERSIONED_TYPES = EnumSet.of(GitBranchType.MASTER, GitBranchType.RELEASE, GitBranchType.HOTFIX);
17+
static final EnumSet<GitBranchType> VERSIONED_TYPES = EnumSet.of(GitBranchType.MASTER, GitBranchType.SUPPORT, GitBranchType.RELEASE, GitBranchType.HOTFIX);
1718
}

0 commit comments

Comments
 (0)