user777889944
user777889944

Reputation: 19

How to get the list of all projects/jobs in jenkins using java programmatically?

I am trying to access the list of all jobs/projects in jenkins and their project files in java not groovy and parsing XML files.

Upvotes: 0

Views: 10290

Answers (2)

Vignesh Dhamodaran
Vignesh Dhamodaran

Reputation: 110

Like @Vitalii said its better to do in groovy or some other scripting languages or to parse the api/xml file to get the workspace job list.

For your case you can get by making your class extending the Trigger and using the job object of the class trigger.

Note: include all the other default classes the jenkins plugin requires and make sure that the plugin runs every minute for this code to execute properly.

public class xyz extends Trigger<BuildableItem>
{
    @Override
    public void run()
    {       
       LOGGER.info("Project Name"+job.getName());        
    }
}

Upvotes: 1

Vitalii Elenhaupt
Vitalii Elenhaupt

Reputation: 7326

I suggest you to use other ways to do this rather then use Java. Consider to use Ruby or Python API wrappers, Groovy, CLI API, Script Console etc. Refer also to Remote Access API for more information.

But if you still need Java, well, there is no Java API, but there is Rest API. And you may use some Java's http client to communicate, for example. Here are required steps:

1. Get a list of jobs.

This can be done requesting http://jenkins_url:port/api/json?tree=jobs[name,url].

Response example:

{
  "jobs" : [
    {
      "name" : "JOB_NAME1",
      "url" : "http://jenkins_url:port/job/JOB_NAME1/"
    },
    {
      "name" : "JOB_NAME2",
      "url" : "http://jenkins_url:port/job/JOB_NAME2/"
    },
    ...
  }

From there you can retrieve job names and urls.

2. Get build artifacts.

Having job url, download from job_url/lastSuccessfulBuild/artifact/*zip*/archive.zip

3. Or get workspace files.

Having job url, download from job_url/JOB_NAME1/ws/*zip*/workspace.zip

Beware, some of this operations require proper Jenkins credentials, anonymous access. Otherwise, request will fail.

More detailed information about Rest API available at your Jenkins: http://jenkins_url:port/api/

Upvotes: 3

Related Questions