JIRA 使用 java 创建问题

JIRA creating issue using java

我想做的是通过 Java 在 JIRA 上创建一个新问题。其实我在实习,之前没有接触过 API。这是我在研究JIRA的文档时找到的代码。不确定参数是否错误。

public class ExampleCreateIssuesAsynchronous {

    private static URI jiraServerUri = URI.create("https://stajtest.atlassian.net/");

    public static void main(String[] args) throws IOException {
        final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
        final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "internship2016****", "***************");

        try {
            final List<Promise<BasicIssue>> promises = Lists.newArrayList();
            final IssueRestClient issueClient = restClient.getIssueClient();

            System.out.println("Sending issue creation requests...");
            for (int i = 0; i < 100; i++) {
                final String summary = "NewIssue#" + i;
                final IssueInput newIssue = new IssueInputBuilder("TST", 1L, summary).build();
                System.out.println("\tCreating: " + summary);
                promises.add(issueClient.createIssue(newIssue));
            }
            System.out.println("Collecting responses...");
            final Iterable<BasicIssue> createdIssues = transform(promises, new Function<Promise<BasicIssue>, BasicIssue>() {
                @Override
                public BasicIssue apply(Promise<BasicIssue> promise) {
                    return promise.claim();
                }
            });
            System.out.println("Created issues:\n" + Joiner.on("\n").join(createdIssues));
        } finally {
            restClient.close();
        }
    }
}

我研究了2天,我得到的只是那个错误。任何帮助将不胜感激。

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 com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(AsynchronousJiraRestClientFactory.java:42)
    at ExampleCreateIssuesAsynchronous.main(ExampleCreateIssuesAsynchronous.java:25)
Caused by: java.lang.ClassNotFoundException: com.atlassian.sal.api.executor.ThreadLocalContextManager
    at java.net.URLClassLoader.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

Edit:

 import java.net.URI;
    import java.util.Optional;

    import com.atlassian.jira.rest.client.api.JiraRestClient;
    import com.atlassian.jira.rest.client.api.domain.Issue;
    import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
    import com.atlassian.util.concurrent.Promise;

    public class JRC
    {
        public Issue getIssue(String issueKey) throws Exception
        {
            final URI jiraServerUri = new URI("stajtest.atlassian.net");
            final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, "stajtest***", "********");
            @SuppressWarnings("rawtypes")
            Promise issuePromise = restClient.getIssueClient().getIssue(issueKey);
            return Optional.ofNullable((Issue) issuePromise.claim()).orElseThrow(() -> new Exception("No such issue"));
        }
    }

    import static org.hamcrest.CoreMatchers.is;
    import static org.junit.Assert.assertThat;

    import org.junit.Test;

    import com.atlassian.jira.rest.client.api.domain.Issue;    

public class JRCTest
    {
        private static final String jiraKey = "DEN-24";
        @Test
        public void testGetIssue() throws Exception {
            Issue issue = new JRC().getIssue(jiraKey);
            assertThat(issue.getKey(), is(jiraKey));
        }
    }

Edit 2

[INFO] 正在扫描项目...[INFO]
[信息] 构建 jrjc 1.0-SNAPSHOT [信息] [信息] [信息] --- maven-resources-plugin:2.6:resources (默认资源)@jrjc --- [警告] 使用平台编码 (实际上是 Cp1254)复制过滤后的资源,即构建依赖于平台! > [INFO] 正在复制 0 资源 [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @jrjc [信息] 检测到更改 - 重新编译模块! [警告] 尚未设置文件编码,使用平台编码 Cp1254,即构建依赖于平台! [INFO] 编译 1 个源文件到 C:\Users\Madara\workspace\jrjc-master\target\classes [错误] 编译错误 [错误] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] -source 1.5 不支持 lambda 表达式(使用 -source 8 或更高级别以启用 lambda 表达式)[信息] 1 个错误 [信息] ---------------------------------------------- ---------- [信息] ---------------------------------- ---------------------------------- [信息] 构建失败 [信息] [信息] 总时间:0.970 秒 [信息] 完成于: 2016-07-04T19:37:26+03:00 [INFO] 最终内存:11M/245M [INFO] [错误] 未能执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (默认编译)在项目 jrjc 上:编译失败 [错误] /C:/Users/Madara/workspace/jrjc-master/src/main/java/JRC.java:[17,81] -source 1.5 [错误] 不支持 lambda 表达式(使用 -source 8 或更高版本以启用 lambda 表达式) [ERROR] -> [Help 1] [ERROR] [ERROR] 查看完整的堆栈跟踪 错误,重新运行 带有 -e 开关的 Maven。 [ERROR] Re-运行 Maven 使用-X 切换到 启用完整的调试日志记录。 [错误] [错误] 更多信息 关于错误和可能的解决方案,请阅读以下内容 文章:[错误] [求助1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

您从哪里获得有关 API 的信息? atlassian 上的文档已过时。

首先你的 pom.xml 应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.company</groupId>
<artifactId>jrjc</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-rest-java-client-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-rest-java-client-core</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>atlassian-public</id>
        <url>https://m2proxy.atlassian.com/repository/public</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>atlassian-public</id>
        <url>https://m2proxy.atlassian.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>warn</checksumPolicy>
        </releases>
        <snapshots>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

先尝试更改您的 pom,看看是否可以解决问题。您可以在此处查看完整的示例:https://github.com/somaiah/jrjc

我能够使用以下 Java 代码在 Jira 中创建并获取错误:

public Issue getIssue(String issueKey) throws Exception
{
    final URI jiraServerUri = new URI("https://yourJiraURI");

    final JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(jiraServerUri, "yourUsername", "yourPassword");
    System.out.println("\n connecting... ");

    Promise issuePromise = restClient.getIssueClient().getIssue(issueKey);
    System.out.println("issuPromise... ");

    return (Issue) issuePromise.claim();
}

public Issue createIssue(String project, Long key, String summary, String description) throws Exception
{
    final URI jiraServerUri = new URI("https://yourJiraURI");

    System.out.println("\n connecting to create a bug... ");

    JiraRestClientFactory restClientFactory = new AsynchronousJiraRestClientFactory();
    JiraRestClient restClient = restClientFactory.createWithBasicHttpAuthentication(jiraServerUri, "yourUsername", "yourPassword");
    IssueRestClient issueClient = restClient.getIssueClient();

    IssueInputBuilder issueBuilder = new IssueInputBuilder(project, key, summary);
    issueBuilder.setDescription(description);   

    IssueType it = new IssueType(jiraServerUri, key, summary, false, "Testing the Issue creation", null);

    issueBuilder.setIssueType(it);
    IssueInput issueInput = issueBuilder.build();

    Promise<BasicIssue> promise = restClient.getIssueClient().createIssue(issueInput);
    BasicIssue basicIssue = promise.claim();
    Promise<Issue> promiseJavaIssue = restClient.getIssueClient().getIssue(basicIssue.getKey());

    Issue issue = promiseJavaIssue.claim();
    System.out.println(String.format("New issue created is: %s\r\n", issue.getSummary()));        


    return issue;
}

POM.xml 依赖关系: 球衣:1.9 联机:4.8.2 jira-rest-java-client-*: 3.0.0 番石榴:14.0-rc1 公共日志记录:4.0.6