Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-24682]
Jenkins gradle installations are ignored if gradle wrapper is selected
  • Loading branch information
mamohr committed Sep 11, 2014
1 parent 7632566 commit ace07ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 66 deletions.
99 changes: 48 additions & 51 deletions src/main/java/hudson/plugins/gradle/Gradle.java
Expand Up @@ -163,63 +163,63 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher

//Build arguments
ArgumentListBuilder args = new ArgumentListBuilder();
GradleInstallation ai = getGradle();
if (ai == null) {
if (useWrapper) {
String execName = (launcher.isUnix()) ? GradleInstallation.UNIX_GRADLE_WRAPPER_COMMAND : GradleInstallation.WINDOWS_GRADLE_WRAPPER_COMMAND;
FilePath gradleWrapperFile;
if (fromRootBuildScriptDir && (normalizedRootBuildScriptDir != null)) {
gradleWrapperFile = new FilePath(normalizedRootBuildScriptDir, execName);
} else {
gradleWrapperFile = new FilePath(build.getModuleRoot(), execName); // Fallback path

// It's possible that a user wants to use gradle wrapper of a project which is located
// not at a repo's root. Example:
// my-big-repo
// |__my-project
// |__<my files>
// |__gradlew
// We want to point to the gradlew located at that project then.

if (buildFile != null && !buildFile.isEmpty()) {
// Check if the target project is located not at the root dir
char fileSeparator = launcher.isUnix() ? '/' : '\\';
int i = buildFile.lastIndexOf(fileSeparator);
if (i > 0) {
// Check if there is a wrapper script at the target project's dir.
FilePath baseDir = build.getModuleRoot();
FilePath candidate = new FilePath(baseDir, buildFile.substring(0, i));
if (candidate.isDirectory() && new FilePath(candidate, execName).exists()) {
// Use gradle wrapper file from the target project.
gradleWrapperFile = new FilePath(candidate, execName);
}
if (useWrapper) {
//We are using the wrapper and don't care about the installed gradle versions
String execName = (launcher.isUnix()) ? GradleInstallation.UNIX_GRADLE_WRAPPER_COMMAND : GradleInstallation.WINDOWS_GRADLE_WRAPPER_COMMAND;
FilePath gradleWrapperFile;
if (fromRootBuildScriptDir && (normalizedRootBuildScriptDir != null)) {
gradleWrapperFile = new FilePath(normalizedRootBuildScriptDir, execName);
} else {
gradleWrapperFile = new FilePath(build.getModuleRoot(), execName); // Fallback path

// It's possible that a user wants to use gradle wrapper of a project which is located
// not at a repo's root. Example:
// my-big-repo
// |__my-project
// |__<my files>
// |__gradlew
// We want to point to the gradlew located at that project then.

if (buildFile != null && !buildFile.isEmpty()) {
// Check if the target project is located not at the root dir
char fileSeparator = launcher.isUnix() ? '/' : '\\';
int i = buildFile.lastIndexOf(fileSeparator);
if (i > 0) {
// Check if there is a wrapper script at the target project's dir.
FilePath baseDir = build.getModuleRoot();
FilePath candidate = new FilePath(baseDir, buildFile.substring(0, i));
if (candidate.isDirectory() && new FilePath(candidate, execName).exists()) {
// Use gradle wrapper file from the target project.
gradleWrapperFile = new FilePath(candidate, execName);
}
}

}
}

if (makeExecutable) {
gradleWrapperFile.chmod(0744);
}
args.add(gradleWrapperFile.getRemote());
} else {
args.add(launcher.isUnix() ? GradleInstallation.UNIX_GRADLE_COMMAND : GradleInstallation.WINDOWS_GRADLE_COMMAND);
if (makeExecutable) {
gradleWrapperFile.chmod(0744);
}
args.add(gradleWrapperFile.getRemote());
} else {
ai = ai.forNode(Computer.currentComputer().getNode(), listener);
ai = ai.forEnvironment(env);
String exe;
if (useWrapper) {
exe = ai.getWrapperExecutable(build);
//Look for a gradle installation
GradleInstallation ai = getGradle();
if (ai != null) {
ai = ai.forNode(Computer.currentComputer().getNode(), listener);
ai = ai.forEnvironment(env);
String exe = ai.getExecutable(launcher);
if (exe == null) {
gradleLogger.error("Can't retrieve the Gradle executable.");
return false;
}
env.put("GRADLE_HOME", ai.getHome());
args.add(exe);
} else {
exe = ai.getExecutable(launcher);
}
if (exe == null) {
gradleLogger.error("Can't retrieve the Gradle executable.");
return false;
//No gradle installation either, fall back to simple command
args.add(launcher.isUnix() ? GradleInstallation.UNIX_GRADLE_COMMAND : GradleInstallation.WINDOWS_GRADLE_COMMAND);
}
args.add(exe);
}


Set<String> sensitiveVars = build.getSensitiveBuildVariables();
args.addKeyValuePairs("-D", fixParameters(build.getBuildVariables()), sensitiveVars);
args.addTokenized(normalizedSwitches);
Expand All @@ -229,9 +229,6 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
args.add("-b");
args.add(buildFileNormalized);
}
if (ai != null) {
env.put("GRADLE_HOME", ai.getHome());
}

if (useWorkspaceAsHome) {
// Make user home relative to the workspace, so that files aren't shared between builds
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/hudson/plugins/gradle/GradleInstallation.java
Expand Up @@ -69,21 +69,6 @@ public String call() throws IOException {
});
}

public String getWrapperExecutable(final AbstractBuild<?, ?> build)
throws IOException, InterruptedException {
return build.getModuleRoot().act(new FilePath.FileCallable<String>() {
@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
String execName = (Functions.isWindows()) ? WINDOWS_GRADLE_WRAPPER_COMMAND : UNIX_GRADLE_WRAPPER_COMMAND;
File execFile = new File(f, execName);
if (execFile.exists()) {
return execFile.getPath();
}
return null;
}
});
}

private File getExeFile() {
String execName = (Functions.isWindows()) ? WINDOWS_GRADLE_COMMAND : UNIX_GRADLE_COMMAND;
String antHome = Util.replaceMacro(gradleHome, EnvVars.masterEnvVars);
Expand Down

1 comment on commit ace07ad

@jvdvegt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gboissinot : Any chance a new release of the Gradle-plugin could be made, so this fix makes its way into Jenkins?

Please sign in to comment.