11package com .e_gineering .maven .gitflowhelper ;
22
3+ import org .apache .maven .artifact .Artifact ;
4+ import org .apache .maven .artifact .ArtifactUtils ;
35import org .apache .maven .model .DistributionManagement ;
46import org .apache .maven .plugin .MojoExecutionException ;
57import org .apache .maven .plugin .MojoFailureException ;
@@ -20,31 +22,96 @@ protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionEx
2022 switch (gitBranchInfo .getType ()) {
2123 case SUPPORT :
2224 case MASTER : {
23- getLog ().info ("Setting release artifact repository to: [" + releaseDeploymentRepository + "]" );
24- project .setSnapshotArtifactRepository (null );
25- project .setReleaseArtifactRepository (getDeploymentRepository (releaseDeploymentRepository ));
25+ setTargetRelease ();
2626 break ;
2727 }
2828 case RELEASE :
2929 case HOTFIX : {
30- getLog ().info ("Setting release artifact repository to: [" + stageDeploymentRepository + "]" );
31- project .setSnapshotArtifactRepository (null );
32- project .setReleaseArtifactRepository (getDeploymentRepository (stageDeploymentRepository ));
30+ setTargetStage ();
3331 break ;
3432 }
3533 case DEVELOPMENT : {
36- getLog ().info ("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]" );
37- project .setSnapshotArtifactRepository (getDeploymentRepository (snapshotDeploymentRepository ));
38- project .setReleaseArtifactRepository (null );
34+ setTargetSnapshots ();
3935 break ;
4036 }
37+ case OTHER : {
38+ // Other branches never target release, but may target stage for non-SNAPSHOT artifacts.
39+ // For this reason, "overwrite" is considered _highly_ dangerous.
40+ if (!"false" .equalsIgnoreCase (forceOtherDeploy )) {
41+ // Setup the target based on the base project version.
42+ if (ArtifactUtils .isSnapshot (project .getVersion ())) {
43+ setTargetSnapshots ();
44+ } else {
45+ setTargetStage ();
46+ }
47+
48+ // Monkey with things to do our semVer magic.
49+ if ("semVer" .equalsIgnoreCase (forceOtherDeploy )) {
50+ if (ArtifactUtils .isSnapshot (project .getVersion ())) {
51+ getLog ().warn ("Maven -SNAPSHOT builds break semVer standards, in that -SNAPSHOT must be the _last_ poriton of a maven version. In semVer, the pre-release status is supposed to come before the build meta-data." );
52+ getLog ().info ("The gitflow-helper-maven-plugin will inject the build metadata preceding the -SNAPSHOT, allowing for snapshot deployments of this branch." );
53+ }
54+ String branchName = gitBranchInfo .getName ();
55+ String semVerAddition = "+" + branchName .replaceAll ("[^0-9^A-Z^a-z^-^.]" , "-" );
56+
57+ updateArtifactVersion (project .getArtifact (), semVerAddition );
58+ for (Artifact a : project .getAttachedArtifacts ()) {
59+ updateArtifactVersion (a , semVerAddition );
60+ }
61+
62+ getLog ().info ("Artifact versions updated with semVer build metadata: " + semVerAddition );
63+ }
64+ if ("overwrite" .equalsIgnoreCase (forceOtherDeploy )) {
65+ getLog ().warn ("DANGER! DANGER, WILL ROBINSON!" );
66+ getLog ().warn ("Deployment of this build will OVERWRITE Deployment of " + project .getVersion () + " in the targeted repository." );
67+ getLog ().warn ("THIS IS NOT RECOMMENDED." );
68+ }
69+ break ;
70+ }
71+ }
4172 default : {
4273 unsetRepos ();
4374 break ;
4475 }
4576 }
4677 }
4778
79+ private void updateArtifactVersion (final Artifact a , final String semVerAdditon ) {
80+ String baseVersion = a .getBaseVersion ();
81+ String version = a .getVersion ();
82+
83+ String semBaseVersion = baseVersion + semVerAdditon ;
84+ String semVersion = version + semVerAdditon ;
85+
86+ if (semBaseVersion .contains ("-SNAPSHOT" )) {
87+ semBaseVersion = semBaseVersion .replace ("-SNAPSHOT" , "" ) + "-SNAPSHOT" ;
88+ }
89+ if (semVersion .contains ("-SNAPSHOT" )) {
90+ semVersion = semVersion .replace ("-SNAPSHOT" , "" ) + "-SNAPSHOT" ;
91+ }
92+ a .setBaseVersion (semBaseVersion );
93+ a .setVersion (semVersion );
94+ }
95+
96+
97+ private void setTargetSnapshots () throws MojoExecutionException , MojoFailureException {
98+ getLog ().info ("Setting snapshot artifact repository to: [" + snapshotDeploymentRepository + "]" );
99+ project .setSnapshotArtifactRepository (getDeploymentRepository (snapshotDeploymentRepository ));
100+ project .setReleaseArtifactRepository (null );
101+ }
102+
103+ private void setTargetStage () throws MojoExecutionException , MojoFailureException {
104+ getLog ().info ("Setting release artifact repository to: [" + stageDeploymentRepository + "]" );
105+ project .setSnapshotArtifactRepository (null );
106+ project .setReleaseArtifactRepository (getDeploymentRepository (stageDeploymentRepository ));
107+ }
108+
109+ private void setTargetRelease () throws MojoExecutionException , MojoFailureException {
110+ getLog ().info ("Setting release artifact repository to: [" + releaseDeploymentRepository + "]" );
111+ project .setSnapshotArtifactRepository (null );
112+ project .setReleaseArtifactRepository (getDeploymentRepository (releaseDeploymentRepository ));
113+ }
114+
48115 private void unsetRepos () {
49116 getLog ().info ("Un-Setting artifact repositories." );
50117 project .setSnapshotArtifactRepository (null );
0 commit comments