Skip to content

Commit 24ff9a7

Browse files
committed
Much better property handling
1 parent 8cee294 commit 24ff9a7

File tree

1 file changed

+127
-23
lines changed

1 file changed

+127
-23
lines changed

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

Lines changed: 127 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,180 @@
66
import org.apache.maven.plugins.annotations.Mojo;
77
import org.apache.maven.plugins.annotations.Parameter;
88

9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
912
import java.util.Enumeration;
1013
import java.util.Properties;
1114

1215
/**
1316
* Set a project property value based upon the current ${env.GIT_BRANCH} resolution.
1417
*/
15-
@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.INITIALIZE)
18+
@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true)
1619
public class SetPropertiesMojo extends AbstractGitflowBranchMojo {
1720

18-
@Parameter(property = "masterBranchProperties", readonly = true, required = false)
19-
private Properties masterBranchProperties = new Properties();
20-
21-
@Parameter(property = "releaseBranchProperties", readonly = true, required = false)
22-
private Properties releaseBranchProperties = new Properties();
23-
24-
@Parameter(property = "hotfixBranchProperties", readonly = true, required = false)
25-
private Properties hotfixBranchProperties = new Properties();
26-
27-
@Parameter(property = "developmentBranchValue", readonly = true, required = false)
28-
private Properties developmentBranchProperties = new Properties();
29-
30-
@Parameter(property = "otherBranchValue", readonly = true, required = false)
31-
private Properties otherBranchProperties = new Properties();
32-
33-
@Parameter(property = "undefinedBranchValue", readonly = true, required = false)
34-
private Properties undefinedBranchValue = new Properties();
35-
36-
@Parameter(property = "scope", readonly = false, required = true, defaultValue = "project")
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")
3797
private String scope;
3898

99+
/**
100+
* Weather or not to attempt to resolve keys / values as if they're properties.
101+
*/
39102
@Parameter(property ="resolve", defaultValue = "true")
40103
private boolean resolve;
41104

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+
42111

43112
@Override
44113
protected void execute(final GitBranchType type, final String gitBranch, final String branchPattern) throws MojoExecutionException, MojoFailureException {
45114
Properties toInject = null;
115+
File toLoad = null;
46116
switch (type) {
47117
case MASTER: {
48118
toInject = masterBranchProperties;
119+
toLoad = masterBranchPropertyFile;
49120
break;
50121
}
51122
case RELEASE: {
52123
toInject = releaseBranchProperties;
124+
toLoad = releaseBranchPropertyFile;
53125
break;
54126
}
55127
case HOTFIX: {
56128
toInject = hotfixBranchProperties;
129+
toLoad = hotfixBranchPropertyFile;
57130
break;
58131
}
59132
case DEVELOPMENT: {
60133
toInject = developmentBranchProperties;
134+
toLoad = developmentBranchPropertyFile;
61135
break;
62136
}
63137
case OTHER: {
64138
toInject = otherBranchProperties;
139+
toLoad = otherBranchPropertyFile;
65140
break;
66141
}
67142
case UNDEFINED: {
68-
toInject = undefinedBranchValue;
143+
toInject = undefinedBranchProperties;
144+
toLoad = undefinedBranchPropertyFile;
69145
break;
70146
}
71147
}
72148

73-
getLog().debug("Setting " + toInject.size() + " properties...");
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...");
74178

75179
for (Enumeration<?> propertyNames = toInject.propertyNames(); propertyNames.hasMoreElements(); ) {
76180
String propertyName = propertyNames.nextElement().toString();
77181

78-
String key = resolveExpression(propertyName);
182+
String key = keyPrefix + resolveExpression(propertyName);
79183
String value = resolveExpression(toInject.getProperty(propertyName));
80184

81185
getLog().debug(" " + key + " = " + value);

0 commit comments

Comments
 (0)