Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

calling java jar from script listner or creating custom plug-in

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Jira version 7.7.2.

I have requirement to create the defect in another system (Rational Jazz)when creating the defect in Jira so I have the rest api for Jazz written in JAVA. I exported that as jar file. 

My question is how I can call jar file to run from script listener inline script? Or Is there a method to import this jar as plugin and then use it in our script listener. 

Thanks,

Om

 

1 answer

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Hello,

Put your jar file in the lib folder of the jira installation directory and restart Jira.

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Hi  @Alexey Matveev Ok. Once I place there and restart . How to call that jar in script listener inline ?

Thanks,

Om

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

As usual. Import necessary classes and instantinate objects.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Just noticed. It is a questin about Bitbucket. I did not try it on Bitbucket. But in Jira it works. Just try it.

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

This question is not about bitbucket. Sorry for confusion. It is for Jira only. Let me give a try that importing necessary classes ..

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

@Alexey Matveev Should I needs to copy the jar file in below path JIRA\atlassian-jira\WEB-INF\lib ?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

There is the lib folder in the Jira folder. You can put there

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

@Alexey MatveevYes, I placed there and restarted the Jira. Now I needs to find the way to call that jar in script listner. I copied CreateWorkItem.jar file in jira\lib. Can you guide me how I can call this in jira script listener ? sorry

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Do you have a script working in Java? Just paste it? There is no special thing. 

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

@Alexey MatveevYes, Here is my java code that working. I created this code as executable Jar and placed in jira\lib. Now I need to call this jar or class in script listner to run .

 

package com;

import java.net.URI;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.client.IProcessClientService;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.workitem.client.IAuditableClient;
import com.ibm.team.workitem.client.IWorkItemClient;
import com.ibm.team.workitem.client.IWorkItemWorkingCopyManager;
import com.ibm.team.workitem.client.WorkItemOperation;
import com.ibm.team.workitem.client.WorkItemWorkingCopy;
import com.ibm.team.workitem.common.model.CategoryId;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.ICategoryHandle;
import com.ibm.team.workitem.common.model.IEnumeration;
import com.ibm.team.workitem.common.model.ILiteral;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemHandle;
import com.ibm.team.workitem.common.model.IWorkItemType;
import com.ibm.team.workitem.common.model.Identifier;


public class CreateWorkItem {

private static String REPOSITORY_ADDRESS = System.getProperty(
"repositoryAddress", "https://jazzserverdev.xxxx.xxx:9443/ccm");

private static String USER = System.getProperty("snippetUser", "");
private static String PASSWORD = System.getProperty("snippetPassword","");

public static void main(String[] args) {
TeamPlatform.startup();
IProgressMonitor monitor = new SysoutProgressMonitor();
ITeamRepository repository = TeamPlatform
.getTeamRepositoryService().getTeamRepository(
REPOSITORY_ADDRESS);

try {
login(monitor,repository);
createWorkItem(args);
} catch (TeamRepositoryException e) {

e.printStackTrace();
}
}


public static ITeamRepository login(IProgressMonitor monitor,
ITeamRepository repository) throws TeamRepositoryException {

repository.registerLoginHandler(new ITeamRepository.ILoginHandler() {
public ILoginInfo challenge(ITeamRepository repository) {
return new ILoginInfo() {
public String getUserId() {
return USER;
}

public String getPassword() {
return PASSWORD;
}
};
}
});
monitor.subTask("Contacting " + repository.getRepositoryURI() + "...");
repository.login(monitor);
monitor.subTask("Connected");
return repository;
}

private static class WorkItemInitialization extends WorkItemOperation {

private String fSummary;
private ICategoryHandle fCategory;

public WorkItemInitialization(String summary, ICategoryHandle category) {
super("Initializing Work Item");
fSummary= summary;
fCategory= category;
}

@Override
protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem= workingCopy.getWorkItem();
workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary));
workItem.setCategory(fCategory);
}
}

public static int createWorkItem(String[] args) throws TeamRepositoryException {
// String projectAreaName= args[0];//"JKE Banking (Change Management)";
// String typeIdentifier= args[1];//"defect";
// String summary= args[2];//"Test";
// String fieldAgainst= args[3];//"BRM";


IWorkItem workItem = null;

String projectAreaName= "JKE Banking (Change Management)";
String typeIdentifier= "defect";
String summary= "Test";
String fieldAgainst= "JKE/BRM";
String description= "Test creation";
String priority="Low";
String datetime="07/17/2018 08:08:00";


ITeamRepository teamRepository= TeamPlatform.getTeamRepositoryService().getTeamRepository(REPOSITORY_ADDRESS);
IProgressMonitor monitor = new SysoutProgressMonitor();
// login(monitor, teamRepository);

IProcessClientService processClient= (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.class);
IAuditableClient auditableClient= (IAuditableClient) teamRepository.getClientLibrary(IAuditableClient.class);
IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);

URI uri= URI.create(projectAreaName.replaceAll(" ", "%20"));
IProjectArea projectArea= (IProjectArea) processClient.findProcessArea(uri, null, null);

List<String> path= Arrays.asList(fieldAgainst.split("/"));
ICategoryHandle categoryHandle= workItemClient.findCategoryByNamePath(projectArea, path, null);
WorkItemInitialization operation= new WorkItemInitialization(summary, categoryHandle);

IWorkItemType workItemType= workItemClient.findWorkItemType(projectArea, typeIdentifier, null);
IWorkItemHandle handle= operation.run(workItemType, null);
workItem= auditableClient.resolveAuditable(handle, IWorkItem.FULL_PROFILE, null);


System.out.println("Created work item " + workItem.getId() + ".");


IWorkItemWorkingCopyManager manager= workItemClient.getWorkItemWorkingCopyManager();
manager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());

WorkItemWorkingCopy copy= manager.getWorkingCopy(workItem);
workItem= copy.getWorkItem();

// Set Description
copy.getWorkItem().setHTMLDescription(XMLString.createFromPlainText(description));


// Set Priority
if(!priority.isEmpty() || priority.equals(null)){
for (IAttribute ia : workItemClient.findAttributes(projectArea,
monitor)) {
if (workItem.hasAttribute(ia) && !ia.isInternal())
{
if (ia.getDisplayName().equalsIgnoreCase("Priority"))

{
Object value = workItem.getValue(ia);
ILiteral targetLiteral = null;
Identifier literalID = (Identifier) value;
// System.out.println(literalID);
IEnumeration enumeration = workItemClient
.resolveEnumeration(ia, null);

List literals = enumeration.getEnumerationLiterals();
for (Iterator iterator = literals.iterator(); iterator
.hasNext();) {
ILiteral iLiteral = (ILiteral) iterator.next();


if (iLiteral.getName().equals(priority)) {
targetLiteral = iLiteral;
copy.getWorkItem().setValue(ia,targetLiteral.getIdentifier2());
copy.save(monitor);
}

}
}
}

}
}



//Set Time
if(!datetime.isEmpty() || datetime.equals(null)){

try {
Date dueDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(datetime);

Object dueDateToSet = new Timestamp(dueDate.getTime());
for (IAttribute ia : workItemClient.findAttributes(projectArea,
monitor)) {

if (workItem.hasAttribute(ia) && !ia.isInternal())
{

if (ia.getDisplayName().equalsIgnoreCase("Due Date"))
{


copy.getWorkItem().setValue(ia,dueDateToSet);

copy.save(monitor);

}
}
}
teamRepository.logout();

} catch (ParseException e) {

e.printStackTrace();
}
}





return workItem.getId();

}
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

try to write in your script like this and look for errors:

import com.CreateWorkItem

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

@Alexey Matveev please see the attachment.creatworkitem.png

Alexey Matveev July 19, 2018

Try to execute this script

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Look like this error is from my jar .. but I am sure all classes added in jar while creating jar . even I am able to run from my command prompt to test it. It work that way. Do you have any idea on this.

 

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-07-19 14:29:30,691 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-07-19 14:29:30,692 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NoClassDefFoundError: com/ibm/team/repository/common/TeamRepositoryException
 at com.onresolve.scriptrunner.runner.JoinClassLoader.findClass(JoinClassLoader.groovy:23)
 at com.onresolve.scriptrunner.runner.JoinClassLoader.super$2$loadClass(JoinClassLoader.groovy)
 at com.onresolve.scriptrunner.runner.JoinClassLoader.loadClass(JoinClassLoader.groovy:41)
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.compileScript(ScriptRunnerImpl.groovy:219)
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:158)
 at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$7.callCurrent(Unknown Source)
 at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$7.callCurrent(Unknown Source)
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:148)
 at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$6.call(Unknown Source)
 at com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener.doScript(CustomListener.groovy:123)
Caused by: java.lang.ClassNotFoundException: com.ibm.team.repository.common.TeamRepositoryException
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
 ... 10 more
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Yes, it seems like you do not have the com/ibm/team/repository/common/TeamRepositoryException class in your jar

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

let me work on it to include all imports and classes in my jar file. Hope that fix.

Once I fix that then How I can call CreateWorkItem class after import com.Createworkitem fix in script?

I may send some arguments like summary , severity like that into that CreateWorkItem. As of now I hard coded to check to run this class in Jira through script listener.

 

Thanks,

Om

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

You can write pure Java in the script. You imported the class. Now you can work with it as you want.

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

Ok. I know getting the summary,severity and priority etc.. field values through groovy so same way we can use the class/objects to get the values to pass as argument to java (CreateWorkItem). Can you please give me one example for getting value for summary field using java. Sorry to bug you..

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2018

It is ok.

You can get the issue summary in a listener like this:

event.issue.getSummary()

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 1, 2018

@Alexey MatveevI just verified in my jar file the class is there.

com.ibm.team.repository.common.TeamRepositoryException

 I am not understanding why it is not looking into included lib folder in Jar.

Any idea ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events