|
| 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 | +} |
0 commit comments