Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12366

Spring scanner not detecting component

I'm trying to write an event listener plugin for jira. When I go the old way (which the latest Atlassian SDK 6.2.9 does) and put these 2 lines

<component key="eventListener" class="jira.plugins.listeners.MyEventListener"/>
<component-import key="eventPublisher" class="com.atlassian.event.api.EventPublisher"/>

and try to package the plugin I get a warning saying that I cannot use component/component-import statement inside plugin descriptor file when Atlassian plugin key is set. The latest SDK uses Spring Scanner, which is added to the pom.xml file automatically during the skeleton creation and which documentation strongly recommends. So I remove those two lines from the atlassian-plugin.xml file and try to substitute them with corresponding annotations:

@Component
public class MyEventListener{
   @Inject
   public MyEventListener(@ComponentImport EventPublisher eventPublisher){
        eventPublisher.register(this);
   }

}

I can compile and package it this way, but when I install it on a running Jira instance, in the description of the plugin it says This plugin has no modules. I've tried changing @Component to @Named , addind @ExportAsService to the class all to no avail. It seems spring scanner does not detect my class as a component. Has anyone been able to overcome this issue? I've written to atlassian community but haven't gotten any news so far.

Upvotes: 2

Views: 2121

Answers (1)

pharsfalvi
pharsfalvi

Reputation: 807

Configure the Spring Scanner maven plugin to execute in verbose mode, and make sure that your class is processed during the build using the inclusion patterns.

    <plugin>
        <groupId>com.atlassian.plugin</groupId>
        <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>atlassian-spring-scanner</goal>
                </goals>
                <phase>process-classes</phase>
            </execution>
        </executions>
        <configuration>
            <includeExclude>+your.package.goes.here.*</includeExclude>
            <verbose>true</verbose>
        </configuration>
    </plugin>

If everything fine, after the build your component will be listed in the file target/classes/META-INF/plugin-components/component

In case the @Component is defined in a library module (as a dependency of the hosting plugin), you can also generate the component metadata using the configuration element

            <scannedDependencies>
                <dependency>
                    <groupId>your.library.group.id</groupId>
                    <artifactId>your-library</artifactId>
                </dependency>
            </scannedDependencies>

Note: there is a difference between the V1 and V2 spring scanner, make sure you use the right version. See reference.

Upvotes: 1

Related Questions