Search code examples
groovyimportjiraconfluenceatlassian-plugin-sdk

Unable to resolve class com.atlassian.jira.component.ComponentAccessor


I am trying to run a Groovy script on IntelliJ IDEA, the Groovy script I am trying to run is called UsersCount.groovy, it looks like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager

def userManager = ComponentAccessor.getUserManager() as UserManager
def message = "My instance contains ${userManager.totalUserCount} user(s)."
log.warn(message)

When I run this code, I get the following error message

    "C:\Program Files\Zulu\zulu-8\bin\java.exe" "-Dtools.jar=C:\Program Files\Zulu\zulu-8\lib\tools.jar" -Dgroovy.home=C:\Users\mouh\.m2\repository\org\codehaus\groovy\groovy-all\2.4.6 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=59917:C:\Program Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\mouh\.m2\repository\org\codehaus\groovy\groovy-all\2.4.6\groovy-all-2.4.6.jar org.codehaus.groovy.tools.GroovyStarter --main groovy.ui.GroovyMain --classpath .;C:\Users\mouh\IdeaProjects\scriptrunner-samples\jira\target\classes --encoding=UTF-8 C:\Users\mouh\IdeaProjects\scriptrunner-samples\jira\src\main\resources\UsersCount.groovy
    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    C:\Users\mouh\IdeaProjects\scriptrunner-samples\jira\src\main\resources\UsersCount.groovy: 2: unable to resolve class com.atlassian.jira.user.util.UserManager
     @ line 2, column 1.
       import com.atlassian.jira.user.util.UserManager
       ^
    
    C:\Users\mouh\IdeaProjects\scriptrunner-samples\jira\src\main\resources\UsersCount.groovy: 1: unable to resolve class com.atlassian.jira.component.ComponentAccessor
     @ line 1, column 1.
       import com.atlassian.jira.component.ComponentAccessor
       ^
    
    2 errors

How can I fix this error and make sure that the imports are resolved? I made sure to include the dependency for com.atlassian.jira in my pom.xml file. My pom.xml file looks like this

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- I added the parent pom.xml, which does all the magic. I took this pom.xml from the ScriptRunner sample plugin -->
    <parent>
        <groupId>com.adaptavist.pom</groupId>
        <artifactId>scriptrunner-jira-standard</artifactId>
        <version>10</version>
        <relativePath/>
    </parent>
    <groupId>ru.matveev.alexey.scriptrunner</groupId>
    <artifactId>scriptrunner-plugin</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>scriptrunner-plugin</name>
    <description>This is the ru.matveev.alexey.scriptrunner:scriptrunner-plugin plugin for Atlassian JIRA.</description>
    <packaging>atlassian-plugin</packaging>
    <dependencies>
        <!-- I excluded a couple of dependencies from the dependency below, because the plugin did not want to start for ScriptRunner versions higher than 5.3.0 -->
        <dependency>
            <groupId>com.onresolve.jira.groovy</groupId>
            <artifactId>groovyrunner</artifactId>
            <version>${scriptrunner.version}</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.onresolve.scriptrunner.platform</groupId>
                    <artifactId>scriptrunner-test-libraries-jira</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jndi</groupId>
                    <artifactId>jndi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jta</groupId>
                    <artifactId>jta</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>is.origo.jira</groupId>
                    <artifactId>tempo-plugin</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.tempoplugin</groupId>
                    <artifactId>tempo-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>groovyrunner</groupId>
                    <artifactId>test</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.atlassian.plugin.automation</groupId>
                    <artifactId>automation-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-annotation</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.jira</groupId>
            <artifactId>jira-api</artifactId>
            <version>8.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>maven-jira-plugin</artifactId>
                <version>${amps.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <productVersion>${jira.version}</productVersion>
                    <productDataVersion>${jira.version}</productDataVersion>
                    <!-- I increased JVM memory, because Jira 7.9.0 does not want to run with the default settings -->
                    <jvmArgs>-Xms512M -Xmx1g</jvmArgs>
                    <enableQuickReload>true</enableQuickReload>
                    <enableFastdev>false</enableFastdev>
                    <applications>
                        <!-- I added Jira Software to the plugin because I want Jira Software to start on the atlas-run command. -->
                        <application>
                            <applicationKey>jira-software</applicationKey>
                            <version>${jira.version}</version>
                        </application>
                        <!-- I added Jira Service Desk to the plugin because I want Jira Service Desk to start on the atlas-run command. -->
                        <application>
                            <applicationKey>jira-servicedesk</applicationKey>
                            <version>${jira.servicedesk.application.version}</version>
                        </application>
                    </applications>
                    <instructions>
                        <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
                        <Export-Package>
                            ru.matveev.alexey.scriptrunner.api,
                        </Export-Package>
                        <Import-Package>
                            org.springframework.osgi.*;resolution:="optional",
                            org.eclipse.gemini.blueprint.*;resolution:="optional",
                            *
                        </Import-Package>
                        <Spring-Context>*</Spring-Context>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.atlassian.plugin</groupId>
                <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
                <version>${atlassian.spring.scanner.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>atlassian-spring-scanner</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
                <configuration>
                    <scannedDependencies>
                        <dependency>
                            <groupId>com.atlassian.plugin</groupId>
                            <artifactId>atlassian-spring-scanner-external-jar</artifactId>
                        </dependency>
                    </scannedDependencies>
                    <verbose>false</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jira.version>7.9.0</jira.version>
        <jira.servicedesk.application.version>3.12.0</jira.servicedesk.application.version>
        <scriptrunner.version>5.3.9</scriptrunner.version>
        <amps.version>6.3.6</amps.version>
        <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
        <atlassian.spring.scanner.version>2.0.0</atlassian.spring.scanner.version>
        <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
        <testkit.version>6.3.11</testkit.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <!-- This is required to find the parent pom and ScriptRunner dependencies -->
        <repository>
            <id>adaptavist-external</id>
            <url>https://nexus.adaptavist.com/content/repositories/external</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
                <checksumPolicy>fail</checksumPolicy>
            </releases>
        </repository>
    </repositories>
</project>

Solution

  • You should specify the path to the Atlassian SDK maven:

    1. Go to Settings (Alt+Ctrl+S) -> Build, Execution, Deployment -> Build -> Maven
    2. In the "Maven home path" set the path to the location where your installed Atlassian SDK is and its direct child maven sub-folder.
    3. In the User settings file checkbox the Override option and menu below specify the path to the "settings.xml" file inside the maven sub-folder of the path above
    4. Click Save to save the changes of the IDEA settings

    enter image description here

    As your local Maven is not configured to see Atlassian repos.