JIRA使用java创建问题

时间:2016-07-01 08:24:45

标签: jira jira-rest-api jira-rest-java-api

我要做的是在JIRA上通过Java创建一个新问题。实际上我正在实习,之前没有使用过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$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.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
  

编辑:

 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));
        }
    }
  
    

编辑2

  
     

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

2 个答案:

答案 0 :(得分:2)

我能够使用以下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 junit:4.8.2 jira-rest-java-client- *:3.0.0 番石榴:14.0-rc1 commons-logging:4.0.6

答案 1 :(得分:1)

您从哪里获得有关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