Skip to content

Commit

Permalink
Merge pull request #27 from jhinrichsen/master
Browse files Browse the repository at this point in the history
[JENKINS-17523] feat: Enable passing job parameter as properties
  • Loading branch information
gezerk committed Feb 18, 2016
2 parents 8730d78 + a9d9945 commit 42d3e2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/hudson/plugins/gradle/Gradle.java
Expand Up @@ -30,10 +30,12 @@ public class Gradle extends Builder implements DryRun {
private final boolean makeExecutable;
private final boolean fromRootBuildScriptDir;
private final boolean useWorkspaceAsHome;
private final boolean passAsProperties;

@DataBoundConstructor
public Gradle(String description, String switches, String tasks, String rootBuildScriptDir, String buildFile,
String gradleName, boolean useWrapper, boolean makeExecutable, boolean fromRootBuildScriptDir, boolean useWorkspaceAsHome) {
String gradleName, boolean useWrapper, boolean makeExecutable, boolean fromRootBuildScriptDir,
boolean useWorkspaceAsHome, boolean passAsProperties) {
this.description = description;
this.switches = switches;
this.tasks = tasks;
Expand All @@ -44,6 +46,7 @@ public Gradle(String description, String switches, String tasks, String rootBuil
this.makeExecutable = makeExecutable;
this.fromRootBuildScriptDir = fromRootBuildScriptDir;
this.useWorkspaceAsHome = useWorkspaceAsHome;
this.passAsProperties = passAsProperties;
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -96,6 +99,11 @@ public boolean isUseWorkspaceAsHome() {
return useWorkspaceAsHome;
}

@SuppressWarnings("unused")
public boolean isPassAsProperties() {
return passAsProperties;
}

public GradleInstallation getGradle() {
for (GradleInstallation i : getDescriptor().getInstallations()) {
if (gradleName != null && i.getName().equals(gradleName)) {
Expand Down Expand Up @@ -221,7 +229,7 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher


Set<String> sensitiveVars = build.getSensitiveBuildVariables();
args.addKeyValuePairs("-D", fixParameters(build.getBuildVariables()), sensitiveVars);
args.addKeyValuePairs(passPropertyOption(), fixParameters(build.getBuildVariables()), sensitiveVars);
args.addTokenized(normalizedSwitches);
args.addTokenized(normalizedTasks);
if (buildFile != null && buildFile.trim().length() != 0) {
Expand Down Expand Up @@ -276,6 +284,10 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
}
}

private String passPropertyOption() {
return passAsProperties ? "-P" : "-D";
}

private Map<String, String> fixParameters(Map<String, String> parmas) {
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, String> entry : parmas.entrySet()) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/hudson/plugins/gradle/Gradle/config.jelly
Expand Up @@ -50,5 +50,9 @@
<f:checkbox default="false"/>
</f:entry>

<f:entry title="${%Pass job parameters as Gradle properties}" field="passAsProperties">
<f:checkbox default="false"/>
</f:entry>


</j:jelly>
@@ -0,0 +1,5 @@
<p>
Jenkins job parameters can be passed to Gradle either as Java system properties (<tt>-D</tt>), or as Gradle
properties (<tt>-P</tt>). Using properties has the advantage that job parameters are directly accessible in the
Gradle DSL, e.g. <tt>project.hasProperty(...)</tt>.
</p>

0 comments on commit 42d3e2b

Please sign in to comment.