Jenkins : Hints for plugin-development newbies

This page is intended for people who start implementing Jenkins plugins. The information is presented in a question/answer style to give the page a structure. This way I hope that the required answers are found more easily. The hints presented have been collected during the implementation of my first Jenkins plugin which I did together with a friend of mine. Please feel free to add some of your hints too.

Jenkins Builder Plugin

How do I execute a shell command?

Every time a build is invoked the perform(Build, Launcher, BuildListener) method is executed. Within this method of your builder you can use the Launcher to execute your command. The Launcher offers several launch methods with different parameter sets. For more details have a look on the API. I used the following method:

 launcher.launch(cmd, env, out, workDir)

cmd:     the command that shall be executed as String
env:      the environment variables as array of Strings (the elements of the array look like this: "Variable=Value")
out:       an OutputStream to which the output of the command is directed
workDir: the working directory as FilePath

An easy example would be something like this:

 launcher.launch("dir", new String[0], listener.getLogger(), build.getProject().getWorkspace());

Whether a Jenkins-Build is successful or not depends on the return value of the perform method. In most cases this value depends on the exit code of the executed command or on thrown exceptions. If the launch method throws an exception this normally means that something went wrong so the return value should be a false (build fails). If no exception is thrown by the launch method the exit code has to be analysed. To get the exit code the following has to be done:

 Proc proc = launcher.launch("dir", build.getEnvVars(), listener.getLogger(), build.getProject().getWorkspace());
 int exitCode = proc.join();

The launch method returns a process to which we connect with the call of join(). This way we wait until the command has been finished and get its exit code. To put all parts of this question together the following example is given:

 public boolean perform(Build build, Launcher launcher, BuildListener listener) {
    try {
      Proc proc = launcher.launch("dir", build.getEnvVars(), listener.getLogger(),build.getProject().getWorkspace());
      int exitCode = proc.join();
      return exitCode == 0;
    } catch (IOException e) {
      e.printStackTrace();
      listener.getLogger().println("IOException !");
      return false;
    } catch (InterruptedException e) {
      e.printStackTrace();
      listener.getLogger().println("InterruptedException!");
      return false;
    }
How-to integrate your own images

Your images must be stored in a directory located in the src/main/webapp path.
To get the correct location of your webapp directory from any of your jelly or java pages, you can use the following method :

public String getIconPath() {
  PluginWrapper wrapper = Hudson.getInstance().getPluginManager().getPlugin([YOUR-PLUGIN-MAIN-CLASS].class);
  return Hudson.getInstance().getRootUrl() + "plugin/"+ wrapper.getShortName()+"/";
}
How-to add a HTML space

Very simple, just use the following jelly tag :

<st:nbsp/>