Search code examples
javapluginssonarqubeanalysissonar-runner

Own Sonar Plugin - How to set the Language?


I'm relative new to Sonar - especially to writing my own Sonar Plugin. After some successful trials, I got stuck now. I have written a simple Plugin which only should transfer source files (*.abap -Files) to the sonar server.

and in my plugin I have an AbapLangauge (extended from AbstractLangauge)

package sonarplugin;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.resources.AbstractLanguage;

public class AbapLanguage extends AbstractLanguage {
  public static final String DEFAULT_SOURCE_SUFFIXES = "abap";
  public static final String KEY = "sap";

  private String[] fileSuffixes;
  private static final Logger LOG = LoggerFactory.getLogger(AbapLanguage.class);

  public AbapLanguage() {   
    super(KEY, "sap");
    LOG.info("SETTING LANGUAGE TO SAP: " + KEY);
    fileSuffixes = createStringArray(null, DEFAULT_SOURCE_SUFFIXES);
  }

  public String[] getFileSuffixes() {
      return fileSuffixes;
  }

  private String[] createStringArray(String[] values, String defaultValues) {
    if (values == null || values.length == 0) {
      return StringUtils.split(defaultValues, ",");
    }
    return values;
  }

}

And I also have an SourceImporter

package sonarplugin;

import org.sonar.api.batch.AbstractSourceImporter;

public final class AbapSourceImporter extends AbstractSourceImporter {

public AbapSourceImporter(AbapLanguage language) {
    super(language);
}
}

I registered these two classes as extension points. In my testproject which I want to analyse with the sonar-runner I have set in the sonar-project.properties

sonar.language=sap

But when I start the sonar runner I get the follwing error:

15:11:36.148 INFO  - -------------  Executing Project Scan
15:11:36.386 INFO  - ABAPIMPORTGERPLUGIN has just started.
15:11:36.924 INFO  - Install JDBC driver
15:11:36.929 INFO  - Apply project exclusions
15:11:36.937 WARN  - H2 database should be used for evaluation purpose only
15:11:36.937 INFO  - Create JDBC datasource for jdbc:h2:tcp://localhost:9092/sonar
15:11:37.051 INFO  - Initializing Hibernate
15:11:42.669 INFO  - -------------  Inspecting MeinTestprojekt
15:11:42.683 INFO  - Load module settings
15:11:43.062 INFO  - ABAPIMPORTGERPLUGIN has just started.
15:11:43.244 INFO  - SETTING LANGUAGE TO SAP: sap
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 15.544s
INFO: Final Memory: 10M/158M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must install a plugin that supports the language 'sap'

Can somebody help me? Why sonar-runner says that there is no plugin that supports the language 'sap'? Isn't my plugin configured right?

thank in advanced bernhard


Solution

  • In your specific case, the problem comes from the fact that you do not have defined any rule repository (even an empty one) for your new language, so Sonar thinks that your new language is not supported.

    More generally, the best option for you is to take a look at some open-source language plugins that we develop for Sonar. For instance, you can go and see the Javascript plugin.