How to integrate Atlassian JIRA REST client library with Gradle build system?

I’m currently working on a Gradle script that creates versions in JIRA using REST API calls. However, I discovered the Atlassian JIRA REST Java client library and want to switch to using this instead of direct API calls.

I’m running into issues when trying to implement this library in my Java project. Here’s what I’m trying to accomplish:

import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.*;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.api.domain.input.WorkflowTransitionInput;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;

import java.net.URI;
import java.util.concurrent.ExecutionException;

public class JiraClientDemo {
    
    private static final String JIRA_URL = "http://localhost:8080/jira";
    private static final String USERNAME = "testuser";
    private static final String PASSWORD = "testpass";
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        
        AsynchronousJiraRestClientFactory clientFactory = new AsynchronousJiraRestClientFactory();
        JiraRestClient jiraClient = clientFactory.createWithBasicHttpAuthentication(
            URI.create(JIRA_URL), USERNAME, PASSWORD);
        
        try {
            // Get server information
            ServerInfo serverData = jiraClient.getMetadataClient().getServerInfo().get();
            System.out.println("Connected to: " + serverData.getServerTitle());
            
            // Fetch all available projects
            Iterable<BasicProject> projectList = jiraClient.getProjectClient().getAllProjects().get();
            for (BasicProject proj : projectList) {
                if (proj.getKey().equals("DEMO")) {
                    System.out.println("Found project: " + proj.getName());
                } else {
                    System.out.println("Project not matching criteria");
                }
            }
            
            // Search for issues using JQL
            SearchResult searchResults = jiraClient.getSearchClient().searchJql("reporter = currentUser()").get();
            for (BasicIssue ticket : searchResults.getIssues()) {
                System.out.println("Issue found: " + ticket.getKey());
            }
            
            // Get specific issue details
            Issue ticketDetails = jiraClient.getIssueClient().getIssue("DEMO-123").get();
            System.out.println("Issue status: " + ticketDetails.getStatus().getName());
            
        } finally {
            jiraClient.close();
        }
    }
}

When I run this code, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/atlassian/sal/api/executor/ThreadLocalContextManager
    at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.create(AsynchronousJiraRestClientFactory.java:35)
    at JiraClientDemo.main(JiraClientDemo.java:18)
Caused by: java.lang.ClassNotFoundException: com.atlassian.sal.api.executor.ThreadLocalContextManager
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

I’ve added the JIRA REST client JARs to my external libraries but I’m still getting this ClassNotFoundException. What dependencies am I missing and how should I properly configure my Gradle build file to include all necessary libraries for this to work?

This error’s super common with Atlassian’s client libraries. Don’t manually add JARs to external libraries - let build.gradle handle the dependency chain automatically. Use this config:

repositories {
    mavenCentral()
    maven { url 'https://packages.atlassian.com/mvn/maven-external/' }
}

dependencies {
    implementation 'com.atlassian.jira:jira-rest-java-client-app:5.2.4'
}

I’m using jira-rest-java-client-app here because it bundles everything you need - sal-api, httpclient, the works. I wasted hours trying to fix each missing class one by one before figuring this out. Clean your build directory after making these changes so it doesn’t clash with any JARs you added manually.

you’re missing the sal-api dependency. had the same issue last week - add implementation 'com.atlassian.sal:sal-api:4.4.0' to your build.gradle. also make sure you’ve got the full atlassian-jira-rest-java-client dependency, not just the core jars.

ClassNotFoundException indicates that you’re missing some dependencies required by the JIRA REST client. It’s crucial not to add JARs manually; instead, let Gradle manage it for you. In your build.gradle, include the following dependencies:

implementation 'com.atlassian.jira:jira-rest-java-client-core:5.2.4'
implementation 'com.atlassian.jira:jira-rest-java-client-api:5.2.4'

This will pull in all necessary components, including sal-api and other related Atlassian libraries. I faced a similar issue when trying to select individual JARs, and I found that Gradle efficiently handles the dependency tree better than manual inclusion. Additionally, ensure your repositories block has Atlassian’s Maven repository listed, as some dependencies might not be available in Maven Central.