无法解析 class com.atlassian.jira.component.ComponentAccessor

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

我正在尝试 运行 IntelliJ IDEA 上的 Groovy 脚本,我正在尝试 运行 的 Groovy 脚本称为 UsersCount.groovy,它看起来像这样

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)

当我运行这段代码时,我收到以下错误信息

    "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.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.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

如何修复此错误并确保导入已解决?我确保在我的 pom.xml 文件中包含 com.atlassian.jira 的依赖项。我的 pom.xml 文件看起来像这样

<?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>

您应该指定 Atlassian SDK maven 的路径:

  1. 转到设置 (Alt+Ctrl+S) -> 构建、执行、部署 -> 构建 -> Maven
  2. 在“Maven 主路径”中,将路径设置为您安装的 Atlassian SDK 及其直接子 maven sub-folder.
  3. 用户设置文件复选框中,覆盖选项和下面的菜单指定“settings.xml”文件的路径maven sub-folder 上面的路径里面
  4. 点击保存保存对IDEA设置的修改

因为您的本地 Maven 未配置为查看 Atlassian 存储库。