JIRA插件开发:组件、模块与日志配置全解析
立即解锁
发布时间: 2025-08-18 02:08:28 阅读量: 2 订阅数: 4 

### JIRA插件开发:组件、模块与日志配置全解析
在JIRA的使用和开发过程中,插件扮演着至关重要的角色。通过插件,我们可以扩展JIRA的功能,满足不同的业务需求。本文将深入探讨JIRA插件开发中的多个关键方面,包括组件的添加与导入、新模块类型的创建、日志的启用以及默认组件的覆盖等内容。
#### 1. JIRA中URL匹配与过滤器
在JIRA里,当URL与特定的url - pattern匹配时,相应的过滤器就会被调用。这在很多场景下非常有用,比如当执行特定操作时做一些特定的事情,或者监控谁在何时做了什么等。例如,在示例代码中,每当查看一个问题时,相关细节就会被打印出来。
#### 2. 添加和导入组件
JIRA拥有一个组件系统,其中有众多的Service类和Manager类注册在PicoContainer中,供核心类和插件使用。有时候,我们需要添加自定义组件,这些组件可以在同一插件的其他模块中使用,也可以被其他插件共享。
##### 2.1 准备工作
使用Atlassian Plugin SDK创建一个骨架插件,这里以之前使用过的RedirectAction webwork模块为例。
##### 2.2 在同一插件内定义和使用组件
步骤如下:
1. **创建接口**:定义所需的方法,组件在其他地方使用时会暴露这些方法。
```java
package com.jtricks.provider;
public interface MyComponent {
public void doSomething();
}
```
2. **创建实现类**:实现接口中的方法,并可以注入JIRA组件。
```java
public class MyComponentImpl implements MyComponent{
private final JiraAuthenticationContext authenticationContext;
public MyComponentImpl(JiraAuthenticationContext authenticationContext) {
this.authenticationContext = authenticationContext;
}
public void doSomething() {
System.out.println("Hey "+authenticationContext.getUser().getFullName()+", Sample method to check Components");
}
}
```
3. **在atlassian - plugin.xml中声明组件**:使用Component Plugin模块。
```xml
<component key="myComponent" name="My Component" class="com.jtricks.provider.MyComponentImpl">
<interface>com.jtricks.provider.MyComponent</interface>
</component>
```
之后,我们就可以在其他插件模块中使用这个组件了。例如在RedirectAction类中:
```java
public class RedirectAction extends JiraWebActionSupport {
private final MyComponent myComponent;
public RedirectAction(MyComponent myComponent) {
this.myComponent = myComponent;
}
@Override
protected String doExecute() throws Exception {
System.out.println("Execute the method in component!");
this.myComponent.doSomething();
System.out.println("Succesfully executed. Go to dashboard");
return getRedirect("/secure/Dashboard.jspa");
}
}
```
##### 2.3 向其他插件暴露组件
要将组件暴露给其他插件,需要做两件事:
1. 声明组件为公共组件。
2. 导出插件所需的包。
具体步骤如下:
1. 像之前一样创建接口和实现类。
2. 在atlassian - plugin.xml中使用Component Plugin模块声明为公共组件:
```xml
<component key="myComponent" name="My Component" class="com.jtricks.provider.MyComponentImpl" public="true">
<interface>com.jtricks.provider.MyComponent</interface>
</component>
```
3. 在atlassian - plugin.xml的plugin - info下使用bundle - instructions元素导出包:
```xml
<plugin-info>
<description>Adding and importing components to JIRA</description>
<version>2.0</version>
<vendor name="JTricks" url="http://www.j-tricks.com/" />
<bundle-instructions>
<Export-Package>com.jtricks.provider</Export-Package>
</bundle-instructions>
</plugin-info>
```
##### 2.4 导入公共组件
要在其他插件中使用公共组件,需要使用component - import插件模块在atlassian - plugin.xml中进行导入:
```xml
<component-import key="myComponent">
<interface>com.jtricks.provider.MyComponent</interface>
</component-import>
```
##### 2.5 使用组件中的服务属性
可以为公共组件定义属性Map,在导入组件时使用这些属性。例如:
```xml
<component key="dictionaryService" class="com.myapp.DefaultDictionaryService" interface="com.myapp.DictionaryService">
<description>Provides a dictionary service.</description>
<service-properties>
<entry key="language" value="English" />
</service-properties>
</component>
```
导入时可以使用filter属性进行过滤:
```xml
<component-import key="dictionaryService" interface="com.myapp.DictionaryService" filter="(language=English)" />
```
#### 3. 添加新模块类型
JIRA提供了Module Type插件模块,我们可以使用它动态地向插件框架添加新的模块类型。
##### 3.1 准备工作
使用Atlassian Plugin SDK创建一个骨架插件。
##### 3.2 定义新插件模块类型
步骤如下:
1. **在atlassin - plugin.xml中添加模块类型定义**:
```xml
<module-type key="dictionary" class="com.jtricks.DictionaryModuleDescriptor" />
```
2. **创建接口**:定义新模块所需的方法。
```java
public interface Dictionary {
String getDefinition(String text);
}
```
3. **创建模块描述符类**:继承AbstractModuleDescriptor类,并使用创建的接口作为泛型类型。
```java
public class DictionaryModuleDescriptor extends AbstractModuleDescriptor<Dictionary> {
...
}
```
4. **实现getModule方法**:创建模块。
```java
public class DictionaryModuleDescriptor extends AbstractModuleDescriptor<Dictionary> {
public DictionaryModuleDescriptor(ModuleFactory moduleFactory) {
super(moduleFactory);
}
public Dictionary getModule() {
return moduleFactory.createModule(moduleClassName, this);
}
}
```
5. **定义属性和元素**:在init方法中获取这些属性和元素。
```java
public class DictionaryModuleDescriptor extends AbstractModuleDescriptor<Dictionary> {
private String language;
public DictionaryModuleDescriptor(ModuleFactory moduleF
```
0
0
复制全文
相关推荐









