Bird
Raised Fist0
Jenkinsdevops~20 mins

Tools directive in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tools Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Jenkins Tools Directive Usage Output
What is the output of this Jenkins pipeline snippet using the tools directive?

pipeline {
  agent any
  tools {
    maven 'Maven_3.8.1'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}
Jenkins
pipeline {
  agent any
  tools {
    maven 'Maven_3.8.1'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}
AThe pipeline fails with 'mvn: command not found' error
BThe pipeline skips the build stage without error
CThe pipeline runs but prints Java version instead of Maven
DThe pipeline runs and prints Maven version 3.8.1 details
Attempts:
2 left
💡 Hint
The tools directive sets up the specified tool version in the environment.
❓ Configuration
intermediate
2:00remaining
Correct Tools Directive Syntax
Which Jenkins pipeline snippet correctly uses the tools directive to set JDK 11 and Maven 3.6.3?
Atools { jdk 'JDK11' maven 'Maven_3.6.3' }
Btools { jdk: 'JDK11', maven: 'Maven_3.6.3' }
Ctools { jdk 'JDK11'; maven 'Maven_3.6.3' }
Dtools { jdk = 'JDK11'; maven = 'Maven_3.6.3' }
Attempts:
2 left
💡 Hint
The tools directive uses Groovy method call syntax without colons or equals.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting Tools Directive Failure
A Jenkins pipeline with tools { maven 'Maven_3.8.1' } fails with:

ERROR: Maven tool 'Maven_3.8.1' is not configured

What is the most likely cause?
AThe Jenkinsfile is missing the <code>agent any</code> directive
BThe Jenkins agent does not have Maven installed locally
CThe Maven installation named 'Maven_3.8.1' is not defined in Jenkins global tool configuration
DThe pipeline syntax is incorrect and causes the tools directive to be ignored
Attempts:
2 left
💡 Hint
Check Jenkins global tool configuration for the named tool installation.
🔀 Workflow
advanced
2:00remaining
Tools Directive Effect on Environment Variables
In a Jenkins pipeline using tools { jdk 'JDK11' }, which environment variable is automatically set for the build steps?
APATH including the Maven bin directory
BJAVA_HOME pointing to the JDK11 installation path
CMAVEN_HOME pointing to the Maven installation
DJDK_HOME pointing to the JDK8 installation path
Attempts:
2 left
💡 Hint
The tools directive sets environment variables related to the tool used.
✅ Best Practice
expert
2:00remaining
Best Practice for Using Tools Directive in Shared Libraries
You maintain a Jenkins shared library that runs builds requiring Maven. What is the best practice to ensure the tools directive works correctly when used inside the shared library's pipeline code?
ADefine the required tools in the Jenkinsfile that calls the shared library, not inside the library code
BHardcode the tools directive inside the shared library with fixed tool names
CUse environment variables to manually set tool paths inside the shared library
DAvoid using the tools directive and install tools manually on agents
Attempts:
2 left
💡 Hint
Consider how Jenkins processes the tools directive in pipeline and shared library contexts.

Practice

(1/5)
1. What is the main purpose of the tools directive in a Jenkins pipeline?
easy
A. To trigger other Jenkins jobs
B. To define environment variables manually
C. To specify the build agent's operating system
D. To automatically install and configure software tools needed for the build

Solution

  1. Step 1: Understand the role of the tools directive

    The tools directive in Jenkins pipelines is used to specify software tools like JDK, Maven, or Gradle that Jenkins should automatically install or configure for the build.
  2. Step 2: Compare options with the directive's purpose

    The other options describe features like defining environment variables, specifying agent OS, or triggering jobs, but do not match the automatic setup of software tools.
  3. Final Answer:

    To automatically install and configure software tools needed for the build -> Option D
  4. Quick Check:

    Tools directive = automatic tool setup [OK]
Hint: Tools directive = auto setup of build tools [OK]
Common Mistakes:
  • Confusing tools directive with environment variable setup
  • Thinking it triggers other jobs
  • Assuming it sets agent OS
2. Which of the following is the correct syntax to specify a Maven tool version using the tools directive in a Jenkins declarative pipeline?
easy
A. tools { 'maven' = 'Maven 3.6.3' }
B. tools { maven: 'Maven 3.6.3' }
C. tools { maven 'Maven 3.6.3' }
D. tools { maven => 'Maven 3.6.3' }

Solution

  1. Step 1: Recall Jenkins tools directive syntax

    The correct syntax uses the tool name as a method and the version as a string argument inside curly braces: tools { maven 'Maven 3.6.3' }.
  2. Step 2: Identify incorrect syntax forms

    The incorrect options use invalid syntax like equals signs (=), colons (:), or arrows (=>), which are not supported in Jenkins declarative pipelines.
  3. Final Answer:

    tools { maven 'Maven 3.6.3' } -> Option C
  4. Quick Check:

    Correct tools syntax uses method and string [OK]
Hint: Use method name and version string inside tools block [OK]
Common Mistakes:
  • Using colons or equals signs instead of method call syntax
  • Putting version in quotes incorrectly
  • Confusing with environment variable syntax
3. Given this Jenkins pipeline snippet, what will be the output of the sh 'mvn -version' command if the tools directive is correctly configured?
pipeline {
  agent any
  tools {
    maven 'Maven 3.8.1'
  }
  stages {
    stage('Build') {
      steps {
        sh 'mvn -version'
      }
    }
  }
}
medium
A. Maven 3.8.1 version details printed
B. Error: mvn command not found
C. Maven 3.6.3 version details printed
D. Pipeline fails to start due to syntax error

Solution

  1. Step 1: Understand tools directive effect

    The tools directive installs and sets the PATH for Maven version 'Maven 3.8.1' automatically on the agent.
  2. Step 2: Predict command output

    Running sh 'mvn -version' will print the version details of Maven 3.8.1, confirming the correct tool is used.
  3. Final Answer:

    Maven 3.8.1 version details printed -> Option A
  4. Quick Check:

    Tools directive sets Maven version = output version [OK]
Hint: Tools directive sets PATH, so mvn shows specified version [OK]
Common Mistakes:
  • Assuming mvn is not found without manual install
  • Expecting wrong Maven version output
  • Thinking syntax error causes failure
4. Identify the error in this Jenkins pipeline snippet using the tools directive:
pipeline {
  agent any
  tools {
    jdk = 'JDK 11'
  }
  stages {
    stage('Build') {
      steps {
        sh 'java -version'
      }
    }
  }
}
medium
A. Missing agent label
B. Using equals sign (=) instead of method call syntax in tools block
C. Incorrect shell command syntax
D. tools directive cannot be used with jdk

Solution

  1. Step 1: Check tools directive syntax

    The tools directive requires method call syntax without equals sign, like jdk 'JDK 11', not jdk = 'JDK 11'.
  2. Step 2: Verify other parts

    The agent is specified, shell command is valid, and jdk can be used in tools directive, so no other errors.
  3. Final Answer:

    Using equals sign (=) instead of method call syntax in tools block -> Option B
  4. Quick Check:

    tools syntax = method call, no equals sign [OK]
Hint: Tools directive uses method calls, not equals signs [OK]
Common Mistakes:
  • Using = instead of method call syntax
  • Thinking agent is missing
  • Assuming jdk is unsupported in tools
5. You want to ensure your Jenkins pipeline uses a specific JDK and Maven version without manual setup on agents. Which pipeline snippet correctly uses the tools directive to achieve this?
hard
A. tools { jdk 'JDK 17'; maven 'Maven 3.8.4' }
B. tools { jdk = 'JDK 17'; maven = 'Maven 3.8.4' }
C. tools { 'jdk': 'JDK 17', 'maven': 'Maven 3.8.4' }
D. tools { jdk: 'JDK 17' maven: 'Maven 3.8.4' }

Solution

  1. Step 1: Recall correct tools directive syntax for multiple tools

    Use method calls separated by semicolons or new lines inside the tools block: tools { jdk 'JDK 17'; maven 'Maven 3.8.4' }.
  2. Step 2: Identify incorrect syntax in other options

    The incorrect options use equals signs, colons, or JSON-like syntax (commas and quotes), which are invalid in the Jenkins declarative pipeline tools directive.
  3. Final Answer:

    tools { jdk 'JDK 17'; maven 'Maven 3.8.4' } -> Option A
  4. Quick Check:

    Multiple tools = method calls separated by semicolons [OK]
Hint: Separate multiple tools with semicolons inside tools block [OK]
Common Mistakes:
  • Using equals signs or colons instead of method calls
  • Forgetting semicolon between tools
  • Using JSON-like syntax inside tools block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard