Unable to start sonarqube with custom java maven plugin

I am getting the following issue while starting sonarqube with custom java maven plugin.

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jdk.internal.loader.ClassLoaders$AppClassLoader@5ffd2b27-org.sonar.server.rule.RegisterRules’: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/sonar/api/internal/SonarRuntimeImpl

As per the issue log, the issue is due to following line of code:

SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(9, 8), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);

Hi,

The classes located in the package org.sonar.api.internal are not accessible to plugins during runtime and can only be used in testing scenarios.

I’m not sure what your objective is, but you don’t need to create a SonarRuntime instance manually. Instead, you can obtain a SonarRuntime instance through the plugin context. Here’s an example:

public class MyPlugin implements Plugin {
    
    @Override
    public void define(Plugin.Context context) {
        SonarRuntime runtime = context.getRuntime();

I am creating a custom RulesDefinition class for my plugin. My code looks like following:

public class SonarqubeJavaRulesDefinition implements RulesDefinition{
    @Override
    public void define(Context context) {
	    NewRepository repository = context.createRepository(PLUGIN_REPOSITORY_KEY, PLUGIN_LANGUAGE).setName(PLUGIN_NAME);
	
	    SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(9, 8), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
			
	    RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_BASE_PATH, runtime);
	
	    ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(SonarqubePluginRulesList.getChecks()));

There’s an example here:

I tried as you suggested for SonarRuntime object, but there is similar issue:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jdk.internal.loader.ClassLoaders$AppClassLoader@5ffd2b27-org.sonar.server.rule.RegisterRules’: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/sonarsource/analyzer/commons/RuleMetadataLoader

As per the log, the following code is giving the issue:

RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RULES_BASE_PATH, runtime);