tohokuaikiのチラシの裏

技術的ネタとか。

Confluenceのプラグイン開発で、別のプラグインで作ったComponentをimportする方法

別のプラグインで作成したpublicなcomponentを使うことができます。ただし、インストールしてあるjarを使うのではなくjarのコンパイル時に組み込むので呼び出し先のjarファイルが必要です。

こんな感じです。
f:id:tohokuaiki:20141216191337g:plain

2つプラグインを作っていくので、atlas-runし直さなければいけなかったりで結構面倒です。*1

最初、
Accessing Confluence Components from Plugin Modules - Confluence Development - Atlassian Developer Documentation
Component Plugin Modules - Crowd Development - Atlassian Developer Documentation
Component Import Module - Confluence Development - Atlassian Developer Documentation
とかを読んでたのですが、全然できず
Import / Export component from one plugin to another - Atlassian Answers
このQ&Aを読んで、「あ、pom.xmlを変えないといけないんだ」という事に気付くまでにかなり時間が掛かりました。といか、いつものことながら、AtlassianのDocumentはどうも片手落ちなところがあって懇切丁寧というわけではないので大変です。

コンポーネント提供側プラグイン

コンポーネント公開の宣言

atlassian-plugin.xml でpublicなコンポーネントとして提供可能という事を宣言します。

<component key="pluginApublicComponent1"
     class="foo.PublicComponentImpl"
     public="true">
    <interface>foo.PublicComponent</interface>
</component>
コンポーネントの実装

interfaceとそれを実装したクラスを作ります。上記のatlassian-plugin.xmlの場合ですと
interface

package foo;

public interface PublicComponent
{
    String getName();
    
    void printDebug();
}

実装したクラス

package foo;

public class PublicComponentImpl implements PublicComponent
{

    public PublicComponentImpl()
    {
    }

    @Override
    public String getName()
    {
        return "myComponent";
    }

    @Override
    public void printDebug() {
        System.out.println("print debug");
    }
}

これを作って、一旦atlas-packageなどでjarを作っておきます。

コンポーネントを使う側のプラグイン

pom.xmlに依存性の追加

先ほどのプラグインを使うので、依存性を追加します。

        <dependency>
            <groupId>提供側プラグインのgroupId</groupId>
            <artifactId>提供側プラグインのartifactId</artifactId>
            <version>提供側プラグインのversion</version>
            <scope>system</scope>
            <type>jar</type>
            <systemPath>さっき作ったjarファイルのパス</systemPath>
        </dependency>

systemPathは、Windowsでやってると/だとダメでバックスラッシュでフルパスで書きました。jarを移動してしまって、${basedir}で指定すればいいのかもしれませんが普通の人だとそんなことは気持ち悪いのでsystemPathは普通にフルパスが入ると思います。何か相対パスを使える方法をご存知の方がいらっしゃればぜひ教えてください。

atlassian-plugin.xmlコンポーネントを使う宣言

userManagerやapplicationPropertiesをcomponent-importしてるのと同様に、

<component-import
     key="pluginApublicComponent1">
    interface="foo.PublicComponent"/>

を加えます。この時、key(この例ではpluginApublicComponent1)を同じものにするのを気をつけてください。

使用するJavaコード側

普通にimportで使えます。

import foo.PublicComponent;

// あとは、Constructorやsetterでinjectionさせる

*1:開発用Confluenceの1990ポート変えるとかすれば2つのプラグインを同時に作れるのかしら?