65

I've installed the Gradle plugin for Jenkins. When I try to build the project I get the following error:

[workspace] $ gradle clean -b build/build.gradle
FATAL: command execution failed
java.io.IOException: Cannot run program "gradle" (in directory "/Users/Shared/Jenkins/Home/jobs/test/workspace"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at hudson.Proc$LocalProc.<init>(Proc.java:244)
    at hudson.Proc$LocalProc.<init>(Proc.java:216)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:707)
    at hudson.Launcher$ProcStarter.start(Launcher.java:338)
    at hudson.Launcher$ProcStarter.join(Launcher.java:345)
    at hudson.plugins.gradle.Gradle.performTask(Gradle.java:201)
    at hudson.plugins.gradle.Gradle.perform(Gradle.java:97)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
    at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:695)
    at hudson.model.Build$RunnerImpl.build(Build.java:178)
    at hudson.model.Build$RunnerImpl.doRun(Build.java:139)
    at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:465)
    at hudson.model.Run.run(Run.java:1404)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
    at hudson.model.ResourceController.execute(ResourceController.java:88)
    at hudson.model.Executor.run(Executor.java:238)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
    at java.lang.ProcessImpl.start(ProcessImpl.java:91)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 16 more
Build step 'Invoke Gradle script' changed build result to FAILURE
Build step 'Invoke Gradle script' marked build as failure
Finished: FAILURE

Running the task $ gradle clean -b build/build.gradle from the command line works as expected.

6
  • Are you sure that Jenkins have the correct gradle path? I don't have my build machine available, but I think you had to configure a gradle home path for the gradle plugin.
    – thoredge
    Commented Dec 27, 2011 at 20:34
  • Its the same path as in my .profile Commented Dec 27, 2011 at 20:41
  • "No such file or directory" is pretty clear. Jenkins can't find the gradle executable. Probably it isn't on the path of the OS user that runs the Jenkins job. In any case, I recommend to use the Gradle Wrapper on a CI machine. Commented Dec 28, 2011 at 3:57
  • Using Gradle Wrapper isn't really an option at the moment, at least not if slaves are involved in the build: issues.jenkins-ci.org/browse/JENKINS-9538
    – Huxi
    Commented Jan 24, 2012 at 9:59
  • As an update to the above, according to the referenced ticket, the problem is solved as of Jenkins 1.464 and Gradle Plugin 1.16, so the wrapper should once again be the best option for builds even in Jenkins slaves.
    – Jolta
    Commented Jan 15, 2015 at 11:51

8 Answers 8

73

I had the same issue and found that the problem for me was the gradle version in the project configuration. It was set to Default and when I set it to the gradle version I was pointing to in the plugin configuration in the Manage Jenkins > Configuration Options, It found gradle and worked properly.

This is a difficult issue to debug and I hope this saves someone else some time.

4
  • Yes, tested for mine that this is the case, the drop down box to select Gradle version was hidden, and i had to explicitly select the Gradle version radio button for it to show, i think this is a bug. Once selecting the Gradle version (other than default), it was working fine. Thanks.
    – HJW
    Commented Feb 4, 2014 at 1:37
  • 2
    How do you explicitly enter or select the actual gradle version? it doesnt allow me to select anything else besides default
    – Jono
    Commented Mar 23, 2015 at 11:17
  • You really ought not do this - use the Gradle Wrapper instead, it saves you a lot of bother. See my answer.
    – Jolta
    Commented Jul 15, 2016 at 10:28
  • 9
    The gradle configuration is now on Manage Jenkins -> Global Tool Configuration
    – AmitW
    Commented May 4, 2017 at 8:56
24

Solution Mastering_the_Object pointed out worked form me too, just to clarify steps there:
just installing Gradle plugin in Jenkins is not enough, you must also go to:
Jenkins->Manage Jenkins->Configure Jenkins->Configure system.
Under "Gradle Installations" type in name (it appears as version in project configuration), check "Install automatically" and select version. Then you can select that "Gradle version" in project configuration.

1
  • +1 For pointing out the solution on how to find the place of configuration. Had seen many suggestions to change the settings but none said where to find it.
    – Lmickos
    Commented Aug 19, 2014 at 10:52
21

I was getting this error using the Gradle wrapper, was able to fix my broken build as follows:

  1. Go to Manage Jenkins -> Global Tool Configuration -> Gradle -> Add Gradle, give it a name
  2. Go to Jenkins -> (your job) -> Configure -> Build, choose "Invoke Gradle" and change the Gradle version from (Default) to the named version
12

When running Gradle on a CI machine such as Jenkins, it's most convenient to use the Gradle Wrapper.

On your development machine, stand in your root project directory and run

gradle wrapper

Then, check in the resulting files into your source control system. After that, you don't have to install anything on your Jenkins server if you need to change Gradle versions. Just configure your Jenkins job like this:

enter image description here

Quoting from the Gradle User Guide:

By distributing the wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with. Of course, this is also great for continuous integration servers (i.e. servers that regularly build your project) as it requires no configuration on the server.

4
  • This isn't a reasonable suggestion for almost any enterprise. It is important that we trust the sources of our artefacts, and therefore most enterprises use an internal repository. GradleW only works if it has access to the internet. I'm not saying that this isn't reasonable in other environments, I'm just pointing out that it probably wouldn't work in an enterprise. Commented Apr 2, 2015 at 8:53
  • 3
    Not so. The wrapper is happy with whatever URL you provide it, so it doesn't need Internet access. We have a locally cached copy of Gradle, in an Artifactory server (any HTTP server would do). The gradle-wrapper.properties needs to be pointed to the internal URL instead of the official repo. This also offloads the official server, and provides better build speeds for our local lusers. After the first download of the wrapper, you need no network connection to build on your PC - and the CI machines have constant access to our local repo.
    – Jolta
    Commented Apr 3, 2015 at 14:12
  • 1
    Using a wrapper is indeed a better solution as you don't need to do all the hard work around matching the versions in jenkin's global configuration and the project's config. Much simpler !
    – iCrus
    Commented Jul 14, 2016 at 19:38
  • While this work, you will ended up having wrapper folder with a size about 200mb on each project you'll working on which is such a waste of disk space. Commented Jun 14, 2021 at 20:10
1

The jenkins can not find the gradle executable. I have never made gradle wrapper to work. Please follow the following steps:

  • Download gradle ( http://gradle.org/downloads),
  • unpack it to, e.g., /usr/local/lib/gradle ,
  • open /etc/profile and add the following two lines:
  • export GRADLE_HOME=/usr/local/lib/gradle
  • export PATH=$PATH:$GRADLE_HOME/bin

It works for my jenkins installation.

ps. I answer this old question, because it is a common issue when setting up gradle with jenkins. I have spent some time trying to make the gradle wrapper work without success before.

1
  • I have set gradle_home under bash_profile, still its the same issue for me Commented Feb 17, 2017 at 9:08
1

Used both @Skarab & a number of other solutions here to get it to work.

Download gradle ( http://gradle.org/downloads), unpack it to, e.g., /usr/local/lib/gradle , open /etc/profile and add the following two lines: export GRADLE_HOME=/usr/local/lib/gradle export PATH=$PATH:$GRADLE_HOME/bin

Then under Manage Jenkins > Configure system > Gradle, defined GRADLE_HOME and gave that gradle installation a name

Then, {this could be a bug} for the project, change gradle version from Default to the one I defined above

0

As others have noted, this is because Jenkins can't find the gradle executable.

None of the published solutions worked for me, but I was able to fix the problem by adding the gradle bin dir to the path set in the .bashrc for the build account on the build slave. Modifying the .profile failed, and setting PATH in the jenkins node configuration also failed.

Some posts will suggest setting the gradle path in the tools menu, but no gradle entry was available there either (perhaps due to regressions / design changes in the gradle plugin?).

In any case, the best test I found (short of running the build again and watching for failure) was to run env over ssh:

ssh <host> env

and check the PATH variable defined that way; if gradle isn't in that path, you probably won't be able to run it from jenkins.

0

This groovy script of jenkins with gradle might help you. Check it out:

pipeline {
agent any

environment {
    MAINTAINER = "${MAINTAINER}"
    ANDROID_HOME = "/home/admin/.android/sdk" // Set the ANDROID_HOME environment variable
}

stages {
    stage('Notify Initial Deployment Notification') {
        steps {
            script {
                def userId = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).getUserId()

                emailext attachLog: true,
                    mimeType: 'text/html',
                    subject: "Deployment Started: Gradle for apk",
                    to: "${MAINTAINER}",
                    body: "<p>The deployment process for Gradle has started by ${userId}.</p><p>Please stay tuned for updates: ${env.BUILD_URL} </p>"
            }
        }
    }

    stage('Git Pull') {
        steps {
            git branch: 'devops', credentialsId: 'ms-git-cred', url: 'https://github.com/support/hmobileapp_customerlatest.git'
            echo "Git Pull Success"
        }
    }

    stage('Build APK') {
        steps {
            sh 'chmod +x *'
            sh '''
                # Set the sdk.dir property in local.properties
                echo "sdk.dir=${ANDROID_HOME}" > local.properties

                # Run Gradle build
                ./gradlew clean assembleDebug
                ./gradlew build
            '''
        }
    }

    stage('Send APK via Email') {
        steps {
            emailext body: 'APK file attached.', 
                subject: 'APK Build Notification', 
                to: "${MAINTAINER}", 
                attachmentsPattern: '**/*.apk'
        }
    }
}
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.