Skip to content

Commit 7a8357a

Browse files
committed
Added support for re-attaching deployed artifacts from OTHER branch types.
* Inside the retarget, you need to set the project version -- this forms the basis for future attached artifacts. * Existing artifacts need their version set (updated). * Don't fiddle wiht the base version. that gets set when you update the version. * Do fiddle with the VersionRange, as this sets up things to be able to be resolved properly when attaching the catalog artifact later. * Failure to do so results in nullpointerexceptions in strange ways. * Catch unsupported operation exception for some internal classes, and carry on your happy merry way. * Added test case for attaching from an 'OTHER'. this does require that you run the VALIDATION phase, so that retarget-deploy can fiddle with the internal project state -- it's a bit wonky, but it works.
1 parent 94d57fb commit 7a8357a

File tree

4 files changed

+71
-13
lines changed

4 files changed

+71
-13
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.e_gineering.maven.gitflowhelper;
22

3+
import org.apache.maven.artifact.Artifact;
34
import org.apache.maven.plugin.MojoExecutionException;
45
import org.apache.maven.plugin.MojoFailureException;
56
import org.apache.maven.plugins.annotations.Execute;
@@ -35,7 +36,12 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
3536
break;
3637
}
3738
case OTHER: {
38-
39+
String otherBranchesToDeploy = resolveExpression(otherDeployBranchPattern);
40+
if (!"".equals(otherBranchesToDeploy) && gitBranchInfo.getName().matches(otherBranchesToDeploy)) {
41+
getLog().info("Attaching branch artifacts from snapshot repository...");
42+
attachExistingArtifacts(snapshotDeploymentRepository, true);
43+
break;
44+
}
3945
}
4046
default: {
4147
getLog().info("Attaching Artifacts from local repository...");

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.e_gineering.maven.gitflowhelper;
22

3+
import org.apache.maven.artifact.Artifact;
34
import org.apache.maven.plugin.MojoExecutionException;
45
import org.apache.maven.plugin.MojoFailureException;
56
import org.apache.maven.plugins.annotations.LifecyclePhase;
67
import org.apache.maven.plugins.annotations.Mojo;
78

89
/**
9-
* If the build is being executed from a FEATURE_OR_BUGFIX, DEVELOPMENT, HOTFIX or RELEASE branch, attach an artifact containing a list of
10+
* If the build is being executed from a DEVELOPMENT, HOTFIX or RELEASE branch, attach an artifact containing a list of
1011
* the attached artifacts. This list is then used for promoting artifacts from the stage repository to the release
1112
* repository. Or it can be used manually by the attach-deployed goal.
1213
*
@@ -27,6 +28,14 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
2728
attachArtifactCatalog();
2829
break;
2930
}
31+
// In order to use attach-deployed, we need to build the artifactCatalog.
32+
case OTHER: {
33+
String otherBranchesToDeploy = resolveExpression(otherDeployBranchPattern);
34+
if (!"".equals(otherBranchesToDeploy) && gitBranchInfo.getName().matches(otherBranchesToDeploy)) {
35+
attachArtifactCatalog();
36+
}
37+
break;
38+
}
3039

3140
case SUPPORT:
3241
case MASTER: {

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.e_gineering.maven.gitflowhelper;
22

33
import org.apache.maven.artifact.Artifact;
4-
import org.apache.maven.artifact.ArtifactUtils;
4+
import org.apache.maven.artifact.versioning.VersionRange;
55
import org.apache.maven.model.DistributionManagement;
66
import org.apache.maven.plugin.MojoExecutionException;
77
import org.apache.maven.plugin.MojoFailureException;
@@ -39,15 +39,15 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
3939
if (!"".equals(otherBranchesToDeploy) && gitBranchInfo.getName().matches(otherBranchesToDeploy)) {
4040
setTargetSnapshots();
4141

42-
String branchName = gitBranchInfo.getName();
43-
String semVerAddition = "+" + branchName.replaceAll("[^0-9A-Za-z-.]", "-") + "-SNAPSHOT";
42+
project.setVersion(getAsBranchSnapshotVersion(project.getVersion(), gitBranchInfo.getName()));
4443

45-
updateArtifactVersion(project.getArtifact(), semVerAddition);
44+
// Update any attached artifacts.
45+
updateArtifactVersion(project.getArtifact(), gitBranchInfo.getName());
4646
for (Artifact a : project.getAttachedArtifacts()) {
47-
updateArtifactVersion(a, semVerAddition);
47+
updateArtifactVersion(a, gitBranchInfo.getName());
4848
}
4949

50-
getLog().info("Artifact versions updated with semVer build metadata: " + semVerAddition);
50+
getLog().info("Artifact versions updated with semVer build metadata: " + getAsBranchSnapshotVersion("", gitBranchInfo.getName()));
5151
break;
5252
}
5353
}
@@ -58,16 +58,36 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
5858
}
5959
}
6060

61-
private void updateArtifactVersion(final Artifact a, final String semVerAdditon) {
62-
// Handle null-safety. In some cases Projects don't have primary artifacts.
61+
/**
62+
* Updates artifact versions for a given branch name.
63+
* @param a artifact to update (may be null)
64+
* @param branchName the branch name
65+
*/
66+
private void updateArtifactVersion(Artifact a, String branchName) {
6367
if (a != null) {
64-
// If the version contains -SNAPSHOT, replace it with ""
65-
a.setVersion(a.getVersion().replace("-SNAPSHOT", "") + semVerAdditon);
66-
a.setBaseVersion(a.getBaseVersion().replace("-SNAPSHOT", "") + semVerAdditon);
68+
a.setVersion(getAsBranchSnapshotVersion(a.getVersion(), branchName));
69+
try {
70+
a.setVersionRange(VersionRange.createFromVersion(a.getVersion()));
71+
} catch (UnsupportedOperationException uoe) { // Some artifact types don't like this.
72+
getLog().debug("Unable to update VersionRange for artifact.");
73+
}
6774
}
6875
}
6976

7077

78+
/**
79+
* Given a String version (which may be a final or -SNAPSHOT version) return a
80+
* version version string mangled to include a `+normalized-branch-name-SNAPSHOT format version.
81+
*
82+
* @param version The base version (ie, 1.0.2-SNAPSHOT)
83+
* @param branchName to be normalized
84+
* @return A mangled version string with the branchname and -SNAPSHOT.
85+
*/
86+
private String getAsBranchSnapshotVersion(final String version, final String branchName) {
87+
return version.replace("-SNAPSHOT", "") + "+" + branchName.replaceAll("[^0-9A-Za-z-.]", "-") + "-SNAPSHOT";
88+
89+
}
90+
7191
private void setTargetSnapshots() throws MojoExecutionException, MojoFailureException {
7292
getLog().info("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]");
7393
project.setSnapshotArtifactRepository(getDeploymentRepository(snapshotDeploymentRepository));

src/test/java/com/e_gineering/maven/gitflowhelper/OtherBranchIT.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.runner.RunWith;
66
import org.junit.runners.BlockJUnit4ClassRunner;
77

8+
import java.util.Arrays;
9+
810
@RunWith(BlockJUnit4ClassRunner.class)
911
public class OtherBranchIT extends AbstractIntegrationTest {
1012
@Test
@@ -71,4 +73,25 @@ public void automagicVersionDependenciesResolve() throws Exception {
7173
verifier.resetStreams();
7274
}
7375
}
76+
77+
@Test
78+
public void attachDeployed() throws Exception {
79+
Verifier verifier = createVerifier("/project-stub", "origin/feature/poc/reattach", "5.0.0-SNAPSHOT");
80+
try {
81+
verifier.executeGoal("deploy");
82+
83+
verifier.verifyTextInLog("Artifact versions updated with semVer build metadata: +origin-feature-poc-reattach-SNAPSHOT");
84+
verifier.verifyErrorFreeLog();
85+
} finally {
86+
verifier.resetStreams();
87+
}
88+
89+
verifier = createVerifier("/project-stub", "origin/feature/poc/reattach", "5.0.0-SNAPSHOT");
90+
try {
91+
verifier.executeGoals(Arrays.asList("validate", "gitflow-helper:attach-deployed"));
92+
verifier.verifyErrorFreeLog();
93+
} finally {
94+
verifier.resetStreams();
95+
}
96+
}
7497
}

0 commit comments

Comments
 (0)