0
0
JenkinsConceptBeginner · 3 min read

What is src Directory in Shared Library in Jenkins Explained

In Jenkins shared libraries, the src directory holds reusable Groovy or Java classes and code that support your pipeline scripts. It is used to organize helper functions and complex logic separately from pipeline steps, making your code cleaner and easier to maintain.
⚙️

How It Works

The src directory in a Jenkins shared library acts like a toolbox where you keep your custom code helpers. Imagine you have a kitchen where you cook meals (your pipeline scripts). The src directory is like the pantry where you store ingredients and tools (Groovy or Java classes) that you use repeatedly to prepare different dishes.

When Jenkins runs a pipeline that uses a shared library, it can load the code inside src to perform tasks like complex calculations, API calls, or data processing. This keeps your main pipeline scripts simple and focused on the steps, while the src directory holds the detailed code behind the scenes.

💻

Example

This example shows a simple Groovy class inside the src directory of a Jenkins shared library. The class provides a method to greet a user, which can be called from a pipeline script.

groovy
package org.example

class Greeter {
    static String greet(String name) {
        return "Hello, ${name}! Welcome to Jenkins shared libraries."
    }
}
🎯

When to Use

Use the src directory when you want to keep your pipeline code clean and reusable by moving complex logic or helper functions out of the main pipeline scripts. It is ideal for:

  • Storing utility classes that perform calculations or data formatting.
  • Organizing API clients or service wrappers used across multiple pipelines.
  • Encapsulating business logic that might change independently from pipeline steps.

This approach helps teams avoid repeating code and makes maintenance easier as your Jenkins pipelines grow.

Key Points

  • The src directory contains Groovy or Java classes for reusable code.
  • It separates helper logic from pipeline step definitions.
  • Code in src is compiled and loaded by Jenkins automatically.
  • Helps keep pipeline scripts simple and maintainable.

Key Takeaways

The src directory stores reusable Groovy or Java classes in Jenkins shared libraries.
It helps separate complex logic from pipeline scripts for cleaner code.
Use src for utility functions, API clients, and business logic.
Jenkins automatically compiles and loads code from src during pipeline runs.