SonarQube 7.0 / sonar-packaging-maven-plugin :ClassNotFoundException on Custom plugin

java

(asish) #1

I am trying to build a Java Custom Sonar Plugin. This Plugin code builds fine,
but when I am trying to run the analysis using this plugin,
I have got the java.lang.NoClassDefFoundError: com/google/common/base/Preconditions.
I have given the pom.xml of custom plugin and its Maven Dependency Tree snapshots below.
Let me know what could the issue in the pom.xml that causes this issue?

Error During Analysis:

2018.08.20 19:26:51 ERROR web[][o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
	at com.nrift.sonar.rules.impl.CheckQueryBuilderUtils.<clinit>(CheckQueryBuilderUtils.java:72)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:259)
	at com.nrift.sonar.rules.base.SonarJavaFileScannersFactory.checkClasses(SonarJavaFileScannersFactory.java:100)
	at com.nrift.sonar.rules.base.JavaRulesDefinition.define(JavaRulesDefinition.java:37)
	at org.sonar.server.rule.RuleDefinitionsLoader.load(RuleDefinitionsLoader.java:56)
	at org.sonar.server.rule.RegisterRules.start(RegisterRules.java:103)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(ReflectionLifecycleStrategy.java:110)
	at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.start(ReflectionLifecycleStrategy.java:89)
	at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.start(AbstractInjectionFactory.java:84)
	at org.picocontainer.behaviors.AbstractBehavior.start(AbstractBehavior.java:169)
	at org.picocontainer.behaviors.Stored$RealComponentLifecycle.start(Stored.java:132)
	at org.picocontainer.behaviors.Stored.start(Stored.java:110)
	at org.picocontainer.DefaultPicoContainer.potentiallyStartAdapter(DefaultPicoContainer.java:1016)
	at org.picocontainer.DefaultPicoContainer.startAdapters(DefaultPicoContainer.java:1009)
	at org.picocontainer.DefaultPicoContainer.start(DefaultPicoContainer.java:767)
	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:134)
	at org.sonar.server.platform.platformlevel.PlatformLevel.start(PlatformLevel.java:90)
	at org.sonar.server.platform.platformlevel.PlatformLevelStartup.access$001(PlatformLevelStartup.java:44)
	at org.sonar.server.platform.platformlevel.PlatformLevelStartup$1.doPrivileged(PlatformLevelStartup.java:81)
	at org.sonar.server.user.DoPrivileged.execute(DoPrivileged.java:45)
	at org.sonar.server.platform.platformlevel.PlatformLevelStartup.start(PlatformLevelStartup.java:78)
	at org.sonar.server.platform.Platform.executeStartupTasks(Platform.java:196)
	at org.sonar.server.platform.Platform.access$400(Platform.java:46)
	at org.sonar.server.platform.Platform$1.lambda$doRun$1(Platform.java:121)
	at org.sonar.server.platform.Platform$1$$Lambda$911/1347091866.run(Unknown Source)
	at org.sonar.server.platform.Platform$AutoStarterRunnable.runIfNotAborted(Platform.java:371)
	at org.sonar.server.platform.Platform$1.doRun(Platform.java:121)
	at org.sonar.server.platform.Platform$AutoStarterRunnable.run(Platform.java:355)
	at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
	at org.sonar.classloader.ParentFirstStrategy.loadClass(ParentFirstStrategy.java:39)
	at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:87)
	at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:76)
	... 34 common frames omitted
2018.08.20 19:26:51 INFO  web[][o.s.p.StopWatcher] Stopping process

While building the custom plugin, below warning is logged

[WARNING] org.codehaus.woodstox:woodstox-core-lgpl:jar:4.4.0:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] com.thoughtworks.xstream:xstream:jar:1.3.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.slf4j:jcl-over-slf4j:jar:1.5.6:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] commons-io:commons-io:jar:2.4:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] xpp3:xpp3:jar:1.1.3.3:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-plugin-api:jar:4.5.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-colorizer:jar:3.7:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] dom4j:dom4j:jar:1.6.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] com.google.code.findbugs:jsr305:jar:1.3.9:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] commons-configuration:commons-configuration:jar:1.6:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] com.google.guava:guava:jar:19.0:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] commons-collections:commons-collections:jar:3.2.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-check-api:jar:4.5.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] xml-apis:xml-apis:jar:1.3.03:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.slf4j:slf4j-api:jar:1.6.2:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-duplications:jar:4.5.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.woodstox:stax2-api:jar:3.1.4:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] javax.xml.stream:stax-api:jar:1.0-2:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.slf4j:log4j-over-slf4j:jar:1.7.5:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] commons-codec:commons-codec:jar:1.8:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-graph:jar:4.5.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.staxmate:staxmate:jar:2.0.1:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[WARNING] org.codehaus.sonar:sonar-channel:jar:3.7:compile is provided by SonarQube plugin API and will not be packaged in your plugin
[INFO]     Dependencies: META-INF/lib/serializer-2.7.1.jar META-INF/lib/jaxen-1.1.4.jar META-INF/lib/maven-project-2.0.7.jar META-INF/lib/maven-model-2.0.7.jar META-INF/lib/gson-2.6.2.jar META-INF/lib/sslr-core-1.20.jar META-INF/lib/classworlds-1.1-alpha-2.jar META-INF/lib/sonar-analyzer-commons-1.9.0.327.jar META-INF/lib/asm-debug-all-5.0.3.jar META-INF/lib/sslr-core-1.21.jar META-INF/lib/java-squid-3.5.jar META-INF/lib/sslr-xpath-1.20.jar META-INF/lib/maven-profile-2.0.7.jar META-INF/lib/picocontainer-2.14.1.jar META-INF/lib/maven-artifact-manager-2.0.7.jar META-INF/lib/cglib-nodep-3.1.jar META-INF/lib/xercesImpl-2.8.1.jar META-INF/lib/sslr-squid-bridge-2.6.1.jar META-INF/lib/maven-repository-metadata-2.0.7.jar META-INF/lib/xalan-2.7.1.jar META-INF/lib/maven-plugin-registry-2.0.7.jar META-INF/lib/maven-artifact-2.0.7.jar META-INF/lib/maven-settings-2.0.7.jar META-INF/lib/wagon-provider-api-1.0-beta-2.jar META-INF/lib/sslr-squid-bridge-2.6.jar META-INF/lib/plexus-container-default-1.0-alpha-9-stable-1.jar META-INF/lib/plexus-utils-1.4.1.jar
[INFO] -------------------------------------------------------
[INFO] Building jar: D:\Sonar-Custom-Rule-Project\sonar-rule\target\sonar-rule-2.0-SNAPSHOT.jar
[INFO]  already added, skipping
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ sonar-rule ---
[INFO] Installing D:\Sonar-Custom-Rule-Project\sonar-rule\target\sonar-rule-2.0-SNAPSHOT.jar to C:\Users\asishs\.m2\repository\org\sonar\samples\sonar-rule\2.0-SNAPSHOT\sonar-rule-2.0-SNAPSHOT.jar
[INFO] Installing D:\Sonar-Custom-Rule-Project\sonar-rule\pom.xml to C:\Users\asishs\.m2\repository\org\sonar\samples\sonar-rule\2.0-SNAPSHOT\sonar-rule-2.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.654s
[INFO] Finished at: Mon Aug 20 21:08:30 IST 2018
[INFO] Final Memory: 29M/165M
[INFO] ------------------------------------------------------------------------

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>

	<groupId>org.sonar.samples</groupId>
	<artifactId>sonar-rule</artifactId>
	<version>2.0-SNAPSHOT</version>
	<packaging>sonar-plugin</packaging>

	<name>Java Custom Rules</name>
	<description>Java custom rules</description>

	<properties>
		<sslr.version>1.21</sslr.version>
		<gson.version>2.6.2</gson.version>

		<sonar.version>7.0</sonar.version>
		<sonarjava.version>5.5.0.14655</sonarjava.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.sonarsource.sonarqube</groupId>
			<artifactId>sonar-plugin-api</artifactId>
			<version>${sonar.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.sonarsource.java</groupId>
			<artifactId>sonar-java-plugin</artifactId>
			<type>sonar-plugin</type>
			<version>${sonarjava.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.sonarsource.sslr-squid-bridge</groupId>
			<artifactId>sslr-squid-bridge</artifactId>
			<version>2.6.1</version>
			<exclusions>
				<exclusion>
					<groupId>org.codehaus.sonar.sslr</groupId>
					<artifactId>sslr-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.codehaus.sonar</groupId>
					<artifactId>sonar-plugin-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.codehaus.sonar.sslr</groupId>
					<artifactId>sslr-xpath</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>jcl-over-slf4j</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.sonarsource.java</groupId>
			<artifactId>java-checks-testkit</artifactId>
			<version>${sonarjava.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>${gson.version}</version>
		</dependency>

		<dependency>
			<groupId>org.sonarsource.sslr</groupId>
			<artifactId>sslr-testing-harness</artifactId>
			<version>${sslr.version}</version>
			<scope>test</scope>
		</dependency>
	<!-- https://mvnrepository.com/artifact/org.sonarsource.java/java-squid -->
		<dependency>
			<groupId>org.sonarsource.java</groupId>
			<artifactId>java-squid</artifactId>
			<version>3.5</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.2</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.assertj</groupId>
			<artifactId>assertj-core</artifactId>
			<version>3.6.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>0.9.30</version>
			<scope>test</scope>
		</dependency>
		<dependency>
		    <groupId>commons-io</groupId>
		    <artifactId>commons-io</artifactId>
		    <version>2.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
		<dependency>
		    <groupId>commons-collections</groupId>
		    <artifactId>commons-collections</artifactId>
		    <version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xercesImpl</artifactId>
			<version>2.8.1</version>
		</dependency>
		<dependency>
			<groupId>xalan</groupId>
			<artifactId>serializer</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>xalan</groupId>
			<artifactId>xalan</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
		    <groupId>org.sonarsource.analyzer-commons</groupId>
		    <artifactId>sonar-analyzer-commons</artifactId>
		    <version>1.9.0.327</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
				<artifactId>sonar-packaging-maven-plugin</artifactId>
				<version>1.17</version>
				<extensions>true</extensions>
				<configuration>
					<pluginKey>java-custom</pluginKey>
					<pluginName>Java Custom Rules</pluginName>
					<pluginClass>com.nrift.sonar.rules.base.JavaRulesPlugin</pluginClass>
					<sonarLintSupported>true</sonarLintSupported>
					<sonarQubeMinVersion>7.0</sonarQubeMinVersion>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>

			<!-- only required to run UT - these are UT dependencies -->
			<plugin>
				  <groupId>org.apache.maven.plugins</groupId>
				  <artifactId>maven-surefire-plugin</artifactId>
				  <version>2.17</version>
				  <configuration>
				    <forkCount>3</forkCount>
				    <reuseForks>true</reuseForks>
				    <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
				  </configuration>
			</plugin>
					
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.6.3.201306030806</version>
				<executions>
					<execution>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
					</execution>
					<execution>
						<id>report</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>

(Julien Henry) #2

Hi,

This is the problem. Guava used to be provided at runtime for plugins, but this is no more the case since SonarQube 5.2.

Please update the version of the sonar-packaging-maven-plugin to 1.18.0.372, and run your build again. Ensure that Guava is packaged in your plugin.