Skip to content

Commit 0c9d47e

Browse files
authored
Merge pull request #34 from egineering-llc/release/1.4.0
Release/1.4.0
2 parents 932be4c + b1b7e7a commit 0c9d47e

File tree

4 files changed

+214
-93
lines changed

4 files changed

+214
-93
lines changed

README.md

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,15 @@ All of the solutions to these issues are implemented independently in different
6262
<goal>retarget-deploy</goal>
6363
<goal>tag-master</goal>
6464
<goal>promote-master</goal>
65-
</goals>
66-
</execution>
67-
<execution>
68-
<id>git-branch-based-property</id>
69-
<goals>
70-
<goal>set-property</goal>
65+
<goal>set-properties</goal>
7166
</goals>
7267
<configuration>
73-
<key>a.property.name</key>
74-
<masterBranchValue>prod</masterBranchValue>
75-
<releaseBranchValue>${some.arbitrary.value.resolved.at.runtime}</releaseBranchValue>
76-
<hotfixBranchValue>emer</masterBranchValue>
77-
<developmentBranchValue>dev</developmentBranchValue>
78-
<otherBranchValue>foo</otherBranchValue>
79-
<undefinedBranchValue>local.build</undefinedBranchValue>
68+
<masterBranchPropertyFile>foo/bar/prod.props</masterBranchPropertyFile>
69+
<hotfixBranchPropertyFile>foo/bar/emer.props</hotfixBranchPropertyFile>
70+
<releaseBranchPropertyFile>foo/bar/test.props</releaseBranchPropertyFile>
71+
<developmentBranchPropertyFile>foo/bar/dev.props</developmentBranchPropertyFile>
72+
<otherBranchPropertyFile>foo/bar/ci.props</otherBranchPropertyFile>
73+
<undefinedBranchPropertyFile>foo/bar/local.props</undefinedBranchPropertyFile>
8074
</configuration>
8175
</execution>
8276
</executions>
@@ -277,18 +271,15 @@ that the first build deployed into. Once they're attached to the project, the `j
277271
the artifacts built by the first job into a jboss application server.
278272

279273

280-
## Goal: `set-property` (Dynamically Set a Maven Project Property)
274+
## Goal: `set-properties` (Dynamically Set a Maven Project / System Properties)
281275

282276
Some situations with automated testing (and integration testing in particular) demand changing configuration properties
283277
based upon the branch type being built. This is a common necessity when configuring automated DB refactorings as part of
284278
a build, or needing to setup / configure datasources for automated tests to run against.
285279

286-
This goal allows configuration of a single project property. If more than one property is needed, it would
287-
likely be advantageous to leverage the properties-maven-plugin:read-project-properties goal to read the properties from
288-
a file. The filename/url paths loaded by the properties-maven-plugin can reference a property configured by this
289-
gitflow-helper-maven-plugin:set-property goal. Since there is another clear path to load many properties based upon the
290-
value of a single property, the gitflow-helper-maven-plugin only sets a single property.
280+
The `set-properties` goal allows for setting project (or system) properties, dynamically based on the detected git
281+
branch being built. Properties can be specified as a Properties collection in plugin configuration, or can be loaded
282+
from a property file during the build. Both property key names and property values will have placeholders resolved.
291283

292-
Configuration consists of defining the property key value, which can itself contain property references, as well as
293-
values for each type of branch. Any branch without a defined value will have the property resolve to `""`, an empty
294-
string.
284+
Multiple executions can be configured, and each execution can target different scopes (system or project), and can load
285+
properties from files with an assigned keyPrefix, letting you name-space properties from execution ids.

pom.xml

Lines changed: 2 additions & 2 deletions
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.3.0</version>
12+
<version>1.4.0</version>
1313
<packaging>maven-plugin</packaging>
1414

1515
<name>gitflow-helper-maven-plugin</name>
@@ -192,7 +192,7 @@
192192
<plugin>
193193
<groupId>org.sonatype.plugins</groupId>
194194
<artifactId>nexus-staging-maven-plugin</artifactId>
195-
<version>1.6.3</version>
195+
<version>1.6.7</version>
196196
<extensions>true</extensions>
197197
<configuration>
198198
<serverId>ossrh</serverId>
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.e_gineering.maven.gitflowhelper;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.apache.maven.plugin.MojoFailureException;
5+
import org.apache.maven.plugins.annotations.LifecyclePhase;
6+
import org.apache.maven.plugins.annotations.Mojo;
7+
import org.apache.maven.plugins.annotations.Parameter;
8+
9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.util.Enumeration;
13+
import java.util.Properties;
14+
15+
/**
16+
* Set a project property value based upon the current ${env.GIT_BRANCH} resolution.
17+
*/
18+
@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true)
19+
public class SetPropertiesMojo extends AbstractGitflowBranchMojo {
20+
21+
/**
22+
* Properties to be applied if executing against a master branch
23+
*/
24+
@Parameter(property = "masterBranchProperties")
25+
private Properties masterBranchProperties;
26+
27+
/**
28+
* A Property file to load if executing against the master branch
29+
*/
30+
@Parameter(property = "masterBranchPropertyFile")
31+
private File masterBranchPropertyFile;
32+
33+
/**
34+
* Properties to be applied if executing against a release branch
35+
*/
36+
@Parameter(property = "releaseBranchProperties")
37+
private Properties releaseBranchProperties;
38+
39+
/**
40+
* A Property file to load if executing against a release branch
41+
*/
42+
@Parameter(property = "releaseBranchPropertyFile")
43+
private File releaseBranchPropertyFile;
44+
45+
/**
46+
* Properties to be applied if executing against a hotfix branch
47+
*/
48+
@Parameter(property = "hotfixBranchProperties")
49+
private Properties hotfixBranchProperties;
50+
51+
/**
52+
* A Property file to load if executing against a hotfix branch
53+
*/
54+
@Parameter(property = "hotfixBranchPropertyFile")
55+
private File hotfixBranchPropertyFile;
56+
57+
/**
58+
* Properties to be applied if executing against the development branch
59+
*/
60+
@Parameter(property = "developmentBranchProperties")
61+
private Properties developmentBranchProperties;
62+
63+
/**
64+
* A Property file to load if executing against the development branch
65+
*/
66+
@Parameter(property = "developmentBranchPropertyFile")
67+
private File developmentBranchPropertyFile;
68+
69+
/**
70+
* Properties to be applied if executing against a non-releasable (feature) branch
71+
*/
72+
@Parameter(property = "otherBranchProperties")
73+
private Properties otherBranchProperties;
74+
75+
/**
76+
* A Property file to load if executing against a non-releasable (feature) branch
77+
*/
78+
@Parameter(property = "otherBranchPropertyFile")
79+
private File otherBranchPropertyFile;
80+
81+
/**
82+
* Properties to be applied if executing against an undefined (local) branch
83+
*/
84+
@Parameter(property = "undefinedBranchProperties")
85+
private Properties undefinedBranchProperties;
86+
87+
/**
88+
* A Property file to load if executing against an undefined (local) branch
89+
*/
90+
@Parameter(property = "undefinedBranchPropertyFile")
91+
private File undefinedBranchPropertyFile;
92+
93+
/**
94+
* Scope in which to set the properties. default is "project", set to "system" in order to set system-level properties.
95+
*/
96+
@Parameter(property = "scope", defaultValue = "project")
97+
private String scope;
98+
99+
/**
100+
* Weather or not to attempt to resolve keys / values as if they're properties.
101+
*/
102+
@Parameter(property ="resolve", defaultValue = "true")
103+
private boolean resolve;
104+
105+
/**
106+
* Added to the beginning of any property key which is set or loaded by this plugin.
107+
*/
108+
@Parameter(property = "keyPrefix", defaultValue = "")
109+
private String keyPrefix = "";
110+
111+
112+
@Override
113+
protected void execute(final GitBranchType type, final String gitBranch, final String branchPattern) throws MojoExecutionException, MojoFailureException {
114+
Properties toInject = null;
115+
File toLoad = null;
116+
switch (type) {
117+
case MASTER: {
118+
toInject = masterBranchProperties;
119+
toLoad = masterBranchPropertyFile;
120+
break;
121+
}
122+
case RELEASE: {
123+
toInject = releaseBranchProperties;
124+
toLoad = releaseBranchPropertyFile;
125+
break;
126+
}
127+
case HOTFIX: {
128+
toInject = hotfixBranchProperties;
129+
toLoad = hotfixBranchPropertyFile;
130+
break;
131+
}
132+
case DEVELOPMENT: {
133+
toInject = developmentBranchProperties;
134+
toLoad = developmentBranchPropertyFile;
135+
break;
136+
}
137+
case OTHER: {
138+
toInject = otherBranchProperties;
139+
toLoad = otherBranchPropertyFile;
140+
break;
141+
}
142+
case UNDEFINED: {
143+
toInject = undefinedBranchProperties;
144+
toLoad = undefinedBranchPropertyFile;
145+
break;
146+
}
147+
}
148+
149+
setProperties(toInject);
150+
151+
if (toLoad != null) {
152+
toInject = new Properties();
153+
FileInputStream fis = null;
154+
try {
155+
getLog().info("Loading properties from: " + toLoad.getCanonicalPath());
156+
fis = new FileInputStream(toLoad);
157+
toInject.load(fis);
158+
} catch (IOException ioe) {
159+
getLog().error("Could not load from : " + toLoad.getAbsolutePath(), ioe);
160+
} finally {
161+
if (fis != null) {
162+
try {
163+
fis.close();
164+
} catch (IOException ioe) {
165+
}
166+
fis = null;
167+
}
168+
}
169+
setProperties(toInject);
170+
}
171+
}
172+
173+
public void setProperties(Properties toInject) {
174+
if (toInject == null) {
175+
return;
176+
}
177+
getLog().info("Setting " + toInject.size() + " properties...");
178+
179+
for (Enumeration<?> propertyNames = toInject.propertyNames(); propertyNames.hasMoreElements(); ) {
180+
String propertyName = propertyNames.nextElement().toString();
181+
182+
String key = keyPrefix + resolveExpression(propertyName);
183+
String value = resolveExpression(toInject.getProperty(propertyName));
184+
185+
getLog().debug(" " + key + " = " + value);
186+
187+
Object replaced;
188+
if ("system".equalsIgnoreCase(scope)) {
189+
replaced = System.setProperty(key, value);
190+
} else {
191+
replaced = project.getProperties().setProperty(key, value);
192+
}
193+
194+
if (replaced != null) {
195+
getLog().debug(" replaced previous value : " + replaced);
196+
}
197+
}
198+
}
199+
}

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

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)