Hi, I have a SonarQube Server where we have a bunch of rules. I want to write a small simple Java application that analyses a few local files based on my disk based on the rules for a particular projectKey given the serverUrl and loginToken.
I believe the sonarlint-core mvnrepo library can be used to develop it. I think sonarlint-intellij utilizes the same and locally analyzes files based on a SonarQube Server connection.
I just want a minimum reproducible example that works. I have tried going through the repository code (sonarlint-core and sonarlint-intellij) and even the test classes but haven’t been able to understand anything so far.
Apologies if this is not the right forum/category to post this question. I really need help. Thanks in advance!
class Main {
public static void main(String[] args) {
String serverUrl = "<SERVER_URL>";
String token = "<TOKEN>";
String projectKey = "<PROJECT_KEY>";
List<String> filePaths = List.of(
"/path/to/local/file1.java",
"/path/to/local/file2.java",
"/path/to/local/file3.java"
);
// analyse the files in `filePaths` based on the server
// identified by `serverUrl` and `projectKey`
// using sonarlint-core, SonarQube Web API, etc.
}
}
SonarQube for IDE (IntelliJ, VS Code, Visual Studio, Eclipse, and other IDEs based on these platforms) provides the feature that you describe - analyzing local files based on the rule configuration from the server. This is what we call Connected Mode.
Thanks for the response, Ms. Japharidze. Also, Happy New Year!
SonarQube for IDE (IntelliJ, VS Code, Visual Studio, Eclipse, and other IDEs based on these platforms) provides the feature that you describe
You’re absolutely right about that. However, what I want to do is - write a Java program that locally analyzes some project files (say /path/to/local/file1.java, /path/to/local/file2.java) on my disk based on the rules configured on a SonarQube server (given the serverUrl, token (auth) and projectKey).
I see that sonarlint-intellij uses some libraries like sonarlint-core, sonarlint-java-client-utils, etc to run the local analysis of the files. I want some help with how exactly I can use those open source libraries like sonarlint-core, sonarlint-java-client-utils etc to write a simple Java program/application that does the same.
For starting off, maybe let’s say I want to list all the Issues that the given list of files (/path/to/local/file1.java, /path/to/local/file2.java) have.
The use case is still not fully clear to me. I am just trying to understand if you are missing functionality that could be added to the product and would benefit other users as well or if you just want to build such an application for personal reasons and curiosity.
sonarlint-* repositories are more designed for IDE experience. You could still find how sonarlint-core backend is initialized and analysis triggered interesting in the sonarlint-language-server repository.
At the same time,sonar-scanner might be closer to what you are looking for?
Yes, you can say this is pretty close to what my actual motive/use-case is with respect to my requirement.
I have already come across sonar-scanner and tried to use it for my use-case. However, the issue is that it uploads some files to the SonarQube server and then again some analysis process happens on the server. This takes a lot of time, and also the analysis results are overwritten on the SonarQube Server every time I run a sonar-scanner analysis instance.
I want to be able to locally analyse my files on-the-fly without uploading a ton of things to the SonarQube Server. SonarLint does exactly that. Something really close to my use case would’ve been sonarlint-cli, but sadly it has been discontinued long ago.
Anyway, I’ll go through the highlighted parts in the sonarlint-language-server and see if I’m able to write a Java program/application based off that. If I need further help, I’ll definitely come back to this thread.
Thanks a lot for your help so far, and have a nice day!