Closed JoopBL closed 5 years ago
Hi! Could you confirm what classes are imported in your source? The code included in the repository seems correct and I couldn't reproduce the issue here:
Hello Felipe,
the problem is in the unittest files, not the checks.
The class only contains a call of PlSqlCheckVerifier
` package nl.cb.sonarplsql.plsql;
import org.junit.Test; import org.sonar.plsqlopen.checks.verifier.PlSqlCheckVerifier;
public class NCConstantCheckTest {
@Test
public void test() {
PlSqlCheckVerifier.verify("src/test/resources/naming_convention_constant.sql", new NCConstantCheck());
}
} `
Well, that's strange... In the pom.xml you have a dependency on <artifactId>sonar-plsql-open-plugin</artifactId>
right? Isn't there any reference to other modules, like plsql-frontend?
the pom file contains the dependency:
I think the problem is due to the relocation.
The unit test gives the following error:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.349 s <<< FAILURE! - in nl.cb.sonarplsql.plsql.NCConstantCheckTest [ERROR] test(nl.cb.sonarplsql.plsql.NCConstantCheckTest) Time elapsed: 0.255 s <<< ERROR! java.lang.NoClassDefFoundError: com/sonar/sslr/api/AstNodeType at nl.cb.sonarplsql.plsql.NCConstantCheckTest.test(NCConstantCheckTest.java:10) Caused by: java.lang.ClassNotFoundException: com.sonar.sslr.api.AstNodeType at nl.cb.sonarplsql.plsql.NCConstantCheckTest.test(NCConstantCheckTest.java:10)
But the AstNodeType has been relocated at: \org\sonar\plugins\plsqlopen\api\sslr\
So the class won't be found.
Oh, I see... This was changed on purpose. See this page for reference: https://github.com/felipebz/zpa/wiki/Create-a-plugin-with-custom-rules#migrating-a-plugin-from-a-previous-version
Specifically:
In your classes, you will need to reimport many classes that are now located in the org.sonar.plugins.plsqlopen.api.* package.
I have the plsql and child projects also in my IDE. So when importing it will take the path from these projects, and will not take into account the relocation of the maven plugin.
When running the unit test stand-alone, it just works (due to the correct import). When building using Maven, the import is not correct due since it uses the jar file containing the relocated path.
I find it useful to have the plsql project open, for reference and debugging.
Thanks for the explanation. I'm used to open the custom plugin in a different window/workspace (without the main project) because of this issue. I don't how to avoid it since the packages need to be relocated...
You can use the maven-source-plugin to generate a jar file with sources, so you can see the sources when working in the custom plugin. I don't know if the shade plugin also reallocates the source jar, but it is worth a try.
I still can't get it working.
I have the main project and the custom project split now. The jar of the main project contains the relocated paths.
The custom project import is using the main project jar file. When deploying the custom project in SonarQube the following error occurs:
java.lang.NoClassDefFoundError: org/sonar/plsqlopen/checks/AbstractBaseCheck at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at org.sonar.classloader.ClassRealm.loadClassFromSelf(ClassRealm.java:125) at org.sonar.classloader.ParentFirstStrategy.loadClass(ParentFirstStrategy.java:37) at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:87) at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:76) at nl.cb.sonarplsql.plsql.PlSqlCustomRulesDefinition.checkClasses(PlSqlCustomRulesDefinition.java:21) at org.sonar.plugins.plsqlopen.api.CustomPlSqlRulesDefinition.define(CustomPlSqlRulesDefinition.java:41)
The custom plugin is being called and found, but the AbstractBaseCheck class can't be found. The main project jar file contains the class at the correct path.
Do you have any suggestions?
The AbstractBaseCheck isn't part of the public API. Your check must extend org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck
😉
Now it works! The checks were still based on the 2.2.0 version.
This issue concerns version 2.3.0.
When trying to build the plsql-custom-rules project, the unit tests fail. The reason are the missing com.sonar.sslr.api objects in the sonar-plsql-open-plugin.
In the sonar-plsql-open-plugin project these objects are relocated to org.sonar.plugins.plsqlopen.api.sslr by the shade plugin.
I can fix theproblem by removing the relocation option in the pom file, but probably you have an intension for it. A solution for the plsql-custom-rules project would be preferable.