Skip to content

Commit 7ac1213

Browse files
authored
Merge pull request e-gineering#133 from DEsoftware/set-properties-sets-git-info
New goal set-git-properties
2 parents 04fb7c2 + c1a29a2 commit 7ac1213

File tree

4 files changed

+351
-0
lines changed

4 files changed

+351
-0
lines changed

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,123 @@ from a property file during the build. Both property key names and property valu
177177
Multiple executions can be configured, and each execution can target different scopes (system or project), and can load
178178
properties from files with an assigned keyPrefix, letting you name-space properties from execution ids.
179179

180+
## Goal: `set-git-properties` (Stores branch type and name in Maven properties)
181+
182+
The goal `set-git-properties` sets in the Git branch type and name in Maven properties.
183+
The following properties are set:
184+
185+
* `branchType`: The type of the branch
186+
* `branchName`: The name of the branch
187+
188+
### Mapping Git branch name
189+
190+
In certain situations it is necessary to sanitize the branch name (e.g. when using the branch name as tag for a docker image).
191+
For such purposes it is possible to the map the branch name and store the mapped value in an additional Maven property.
192+
193+
### Mapping branch name with a java class
194+
195+
The mapper can be implemented with a Java class that implements `java.util.function.BiFunction<String,String,String>`.
196+
* The first argument of the function is the branch name, the second argument is the branch type.
197+
* The result of the function is the mapped branch name.
198+
* The class has to be provided via a maven artifact as dependency of the plugin.
199+
* The `language` element has the value `java`.
200+
* The `propertyName` defines the name of the Maven property where the mapped branch name is stored.
201+
202+
Example:
203+
```xml
204+
<build>
205+
<plugins>
206+
<plugin>
207+
<groupId>com.e-gineering</groupId>
208+
<artifactId>gitflow-helper-maven-plugin</artifactId>
209+
<version>${gitflow.helper.plugin.version}</version>
210+
<configuration>
211+
<branchNamePropertyMappers>
212+
<branchNamePropertyMapper>
213+
<propertyName>branchNameLowerCase</propertyName>
214+
<language>java</language>
215+
<mapper>com.example.BranchMapperToLowerCase</mapper>
216+
</branchNamePropertyMapper>
217+
</branchNamePropertyMappers>
218+
</configuration>
219+
<executions>
220+
<execution>
221+
<goals>
222+
<goal>set-git-properties</goal>
223+
</goals>
224+
</execution>
225+
</executions>
226+
<dependencies>
227+
<dependency>
228+
<groupdId>com.example</groupdId>
229+
<artifactId>branchNameMapper</artifactId>
230+
<version>1.0.0</version>
231+
</dependency>
232+
</dependencies>
233+
</plugin>
234+
</plugins>
235+
...
236+
</build>
237+
```
238+
The mapper class:
239+
```java
240+
package com.example;
241+
242+
import java.util.function.BiFunction;
243+
244+
public class BranchMapperToLowerCase implements BiFunction<String,String,String>
245+
{
246+
public String apply(String branchName, String branchType)
247+
{
248+
return branchName.toLowerCase();
249+
}
250+
}
251+
```
252+
253+
### Mapping the branch name with a JSR223 scripting language
254+
255+
The branch name mapping can also be implemented with a JSR232 scripting language.
256+
For this case the
257+
258+
* `language` is the name of the JSR232 scripting language (per default only `javascript` is possible. For additional language can be provided by additional dependencies)
259+
* `mapper` contains the script that implements a function with map `map`. The first parameter of the function is the branch name, the second parameter is the branch type.
260+
* The return value of the script is the mapped branch name. It is stored in the property.
261+
* The Git branch name is stored in the variable `branchName` and the branch type is accessible via `branchType`
262+
263+
**Example:** Mapper in javascript
264+
265+
```xml
266+
<build>
267+
<plugins>
268+
<plugin>
269+
<groupId>com.e-gineering</groupId>
270+
<artifactId>gitflow-helper-maven-plugin</artifactId>
271+
<version>${gitflow.helper.plugin.version}</version>
272+
<configuration>
273+
<branchNamePropertyMappers>
274+
<branchNamePropertyMapper>
275+
<propertyName>branchNameLowerCase</propertyName>
276+
<language>javascript</language>
277+
<mapper>
278+
function map(branchName, branchType) {
279+
return branchName.toLowerCase();
280+
}
281+
</mapper>
282+
</branchNamePropertyMapper>
283+
</branchNamePropertyMappers>
284+
</configuration>
285+
<executions>
286+
<execution>
287+
<goals>
288+
<goal>set-git-properties</goal>
289+
</goals>
290+
</execution>
291+
</executions>
292+
</plugin>
293+
</plugins>
294+
...
295+
</build>
296+
```
180297

181298
## Goal: `retarget-deploy` (Branch Specific Deploy Targets & Staging)
182299

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.e_gineering.maven.gitflowhelper;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.apache.maven.shared.utils.StringUtils;
5+
6+
import javax.script.Invocable;
7+
import javax.script.ScriptEngine;
8+
import javax.script.ScriptEngineManager;
9+
import java.util.function.BiFunction;
10+
11+
/**
12+
* Helper for mapping the GIT branch name
13+
*/
14+
public class PropertyMapper
15+
{
16+
private String propertyName;
17+
private String language = "java";
18+
private String mapper;
19+
20+
public String getLanguage()
21+
{
22+
return language;
23+
}
24+
25+
public void setLanguage(final String language)
26+
{
27+
this.language = language;
28+
}
29+
30+
public String getPropertyName()
31+
{
32+
return propertyName;
33+
}
34+
35+
public void setPropertyName(final String propertyName)
36+
{
37+
this.propertyName = propertyName;
38+
}
39+
40+
public String getMapper()
41+
{
42+
return mapper;
43+
}
44+
45+
public void setMapper(final String mapper)
46+
{
47+
this.mapper = mapper;
48+
}
49+
50+
/**
51+
* Maps the Git branch name
52+
* @param gitBranchInfo the Git Info
53+
* @return the mapped branch name
54+
* @throws MojoExecutionException on error
55+
*/
56+
String map(GitBranchInfo gitBranchInfo) throws MojoExecutionException
57+
{
58+
if(StringUtils.isBlank(getPropertyName())) {
59+
throw new MojoExecutionException("Mapper has not propertyName");
60+
}
61+
if(StringUtils.isBlank(getLanguage())) {
62+
throw new MojoExecutionException("Mapper for property [" + getPropertyName() + "] has no language");
63+
}
64+
if(StringUtils.isBlank(getMapper())) {
65+
throw new MojoExecutionException("Mapper for property [" + getPropertyName() + "] has no mapper");
66+
}
67+
if("java".equalsIgnoreCase(language)) {
68+
69+
Class<?> mapperClass;
70+
try
71+
{
72+
mapperClass = Class.forName(mapper.trim());
73+
}
74+
catch(ClassNotFoundException e)
75+
{
76+
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]: class " + mapper + " not found", e);
77+
}
78+
79+
if(!BiFunction.class.isAssignableFrom(mapperClass)) {
80+
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]: class " + mapper + " does not implement " + BiFunction.class.getName());
81+
}
82+
83+
try
84+
{
85+
@SuppressWarnings("unchecked")
86+
BiFunction<String,String,String> function = (BiFunction<String,String,String>)Class.forName(mapper).newInstance();
87+
return function.apply(gitBranchInfo.getName(), gitBranchInfo.getType().name());
88+
}
89+
catch(Exception e)
90+
{
91+
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]", e);
92+
}
93+
}
94+
ScriptEngineManager mgr = new ScriptEngineManager();
95+
ScriptEngine engine = mgr.getEngineByName(language);
96+
if(engine == null) {
97+
throw new MojoExecutionException("Property mapper for property [" + getPropertyName() + "] has an unsupported language [" + language + "]");
98+
}
99+
try
100+
{
101+
engine.eval(mapper);
102+
103+
Invocable inv = (Invocable) engine;
104+
105+
Object ret = inv.invokeFunction("map", gitBranchInfo.getName(), gitBranchInfo.getType());
106+
if(ret == null) {
107+
return null;
108+
}
109+
return String.valueOf(ret);
110+
}
111+
catch(Exception e)
112+
{
113+
throw new MojoExecutionException("Error in property mapper for property [" + getPropertyName()+ "]", e);
114+
}
115+
}
116+
117+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import org.codehaus.plexus.util.StringUtils;
9+
10+
/**
11+
* Stores the branch type and the git branch in Maven properties
12+
*/
13+
@Mojo(name = "set-git-properties", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
14+
public class SetGitPropertiesMojo extends AbstractGitflowBranchMojo
15+
{
16+
/**
17+
* Defines the name of the property where the branch type is stored
18+
*/
19+
@Parameter(property = "branchTypeProperty", defaultValue = "branchType")
20+
private String branchTypeProperty = "branchType";
21+
22+
/**
23+
* Defines the name of the property where the Git branch name is stored.
24+
*/
25+
@Parameter(property = "branchNameProperty", defaultValue = "gitBranchName")
26+
private String branchNameProperty = "branchName";
27+
28+
/**
29+
* Der branchNamePropertyMapper allows to store the Git branch name
30+
* into additional properties.<br>
31+
* The branchName can be mapped by a java class, or an JSR223 scripting language
32+
*/
33+
@Parameter(property = "branchNamePropertyMappers")
34+
private PropertyMapper[] branchNamePropertyMappers;
35+
36+
@Override
37+
protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionException, MojoFailureException
38+
{
39+
if(!StringUtils.isBlank(branchTypeProperty)) {
40+
project.getProperties().setProperty(branchTypeProperty, gitBranchInfo.getType().name());
41+
}
42+
if(!StringUtils.isBlank(branchNameProperty)) {
43+
project.getProperties().setProperty(branchNameProperty, gitBranchInfo.getName());
44+
}
45+
46+
if(branchNamePropertyMappers != null) {
47+
for(PropertyMapper pm : branchNamePropertyMappers)
48+
{
49+
String mappedValue = pm.map(gitBranchInfo);
50+
getLog().info("Mapper [" + pm.getPropertyName() + "] mapped Git branch name [" + gitBranchInfo.getName() + "] to [" + mappedValue + "]");
51+
if(StringUtils.isBlank(mappedValue)) {
52+
getLog().warn("Mapper for property [" + pm.getPropertyName() + "] did not provide a value, ignoring it");
53+
continue;
54+
}
55+
project.getProperties().setProperty(pm.getPropertyName(), mappedValue);
56+
}
57+
}
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.e_gineering.maven.gitflowhelper;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.util.function.BiFunction;
8+
9+
public class TestPropertyMapper
10+
{
11+
/**
12+
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Java Mapper
13+
* @throws MojoExecutionException on error
14+
*/
15+
@Test
16+
public void testPropertyMapperWithJava() throws MojoExecutionException
17+
{
18+
PropertyMapper mapper = new PropertyMapper();
19+
mapper.setPropertyName("prop");
20+
mapper.setLanguage("java");
21+
mapper.setMapper(ToLowerCaseMapper.class.getName());
22+
23+
GitBranchInfo info = new GitBranchInfo("FOO", GitBranchType.DEVELOPMENT, "bar");
24+
25+
Assert.assertEquals("foo", mapper.map(info));
26+
}
27+
28+
/**
29+
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Javascript Mapper
30+
* @throws MojoExecutionException on error
31+
*/
32+
@Test
33+
public void testPropertyMapperWithJavascript() throws MojoExecutionException
34+
{
35+
PropertyMapper mapper = new PropertyMapper();
36+
mapper.setPropertyName("prop");
37+
mapper.setLanguage("javascript");
38+
mapper.setMapper(""
39+
+ "function map(branchName, branchType) {\n"
40+
+" return branchName.toLowerCase();\n"
41+
+ "}"
42+
);
43+
44+
GitBranchInfo info = new GitBranchInfo("FOO", GitBranchType.DEVELOPMENT, "bar");
45+
46+
Assert.assertEquals("foo", mapper.map(info));
47+
48+
}
49+
50+
public static class ToLowerCaseMapper implements BiFunction<String,String,String>
51+
{
52+
@Override
53+
public String apply(String branchName, String branchType)
54+
{
55+
return branchName.toLowerCase();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)