|
6 | 6 | import org.apache.maven.plugins.annotations.Mojo; |
7 | 7 | import org.apache.maven.plugins.annotations.Parameter; |
8 | 8 |
|
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.IOException; |
9 | 12 | import java.util.Enumeration; |
10 | 13 | import java.util.Properties; |
11 | 14 |
|
12 | 15 | /** |
13 | 16 | * Set a project property value based upon the current ${env.GIT_BRANCH} resolution. |
14 | 17 | */ |
15 | | -@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.INITIALIZE) |
| 18 | +@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) |
16 | 19 | public class SetPropertiesMojo extends AbstractGitflowBranchMojo { |
17 | 20 |
|
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") |
37 | 97 | private String scope; |
38 | 98 |
|
| 99 | + /** |
| 100 | + * Weather or not to attempt to resolve keys / values as if they're properties. |
| 101 | + */ |
39 | 102 | @Parameter(property ="resolve", defaultValue = "true") |
40 | 103 | private boolean resolve; |
41 | 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 | + |
42 | 111 |
|
43 | 112 | @Override |
44 | 113 | protected void execute(final GitBranchType type, final String gitBranch, final String branchPattern) throws MojoExecutionException, MojoFailureException { |
45 | 114 | Properties toInject = null; |
| 115 | + File toLoad = null; |
46 | 116 | switch (type) { |
47 | 117 | case MASTER: { |
48 | 118 | toInject = masterBranchProperties; |
| 119 | + toLoad = masterBranchPropertyFile; |
49 | 120 | break; |
50 | 121 | } |
51 | 122 | case RELEASE: { |
52 | 123 | toInject = releaseBranchProperties; |
| 124 | + toLoad = releaseBranchPropertyFile; |
53 | 125 | break; |
54 | 126 | } |
55 | 127 | case HOTFIX: { |
56 | 128 | toInject = hotfixBranchProperties; |
| 129 | + toLoad = hotfixBranchPropertyFile; |
57 | 130 | break; |
58 | 131 | } |
59 | 132 | case DEVELOPMENT: { |
60 | 133 | toInject = developmentBranchProperties; |
| 134 | + toLoad = developmentBranchPropertyFile; |
61 | 135 | break; |
62 | 136 | } |
63 | 137 | case OTHER: { |
64 | 138 | toInject = otherBranchProperties; |
| 139 | + toLoad = otherBranchPropertyFile; |
65 | 140 | break; |
66 | 141 | } |
67 | 142 | case UNDEFINED: { |
68 | | - toInject = undefinedBranchValue; |
| 143 | + toInject = undefinedBranchProperties; |
| 144 | + toLoad = undefinedBranchPropertyFile; |
69 | 145 | break; |
70 | 146 | } |
71 | 147 | } |
72 | 148 |
|
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..."); |
74 | 178 |
|
75 | 179 | for (Enumeration<?> propertyNames = toInject.propertyNames(); propertyNames.hasMoreElements(); ) { |
76 | 180 | String propertyName = propertyNames.nextElement().toString(); |
77 | 181 |
|
78 | | - String key = resolveExpression(propertyName); |
| 182 | + String key = keyPrefix + resolveExpression(propertyName); |
79 | 183 | String value = resolveExpression(toInject.getProperty(propertyName)); |
80 | 184 |
|
81 | 185 | getLog().debug(" " + key + " = " + value); |
|
0 commit comments