liulin
2024-08-13 6b7b2214ef457a953381226dc42354d237d6b295
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.lunhan.xxx.host;
 
import com.cdancy.jenkins.rest.JenkinsClient;
import com.cdancy.jenkins.rest.domain.job.Job;
import com.cdancy.jenkins.rest.domain.job.JobList;
import com.cdancy.jenkins.rest.features.JobsApi;
 
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class JenkinsTest {
    public static void main(String[] args) {
        String url = "http://192.168.91.200:8080";
        String userName = "admin";
        String password = "lunhan.2023";
 
        JenkinsClient client = JenkinsClient.builder()
                .endPoint(url) // Optional. Defaults to http://127.0.0.1:8080
                .credentials(userName+ ":" + password) // Optional.
                .build();
        JobsApi jobsApi = client.api().jobsApi();
        JobList jobList = jobsApi.jobList("");
 
        Pattern regex = Pattern.compile("<command>(?<command>[^<]+)</command>");
 
        Map<String, String> map = new TreeMap<>();
        for (Job job : jobList.jobs()) {
            String config = jobsApi.config("", job.name());
            Matcher matcher = regex.matcher(config);
            if (matcher.find()) {
                map.put(job.name(), matcher.group("command"));
            }
        }
 
        System.out.println("END.");
    }
}