I am quite new to java field learning.
Could you please help me to resolve my issue
Currently what i am doing is trying to create a custom scanner using java
I am good in python but not in java
I will add this code .jar
file to external/plugins
Step 1 : I have a python script
Step 2 : This script reads all the files identified when i clone my bitbucket repository
Step 3 : This script is built to scan that .fil files and it generates a generic issue report in .json format for sonarqube.
I am issue in below code
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>custom-sonarqube-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>custom-sonarqube-plugin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.sonarsource.scanner.api</groupId>
<artifactId>sonar-scanner-api</artifactId>
<version>2.15.0.2182</version>
</dependency>
</dependencies>
</project>
CustomScannerExecutor.java
import org.sonarsource.scanner.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ScannerSide
public class CustomScannerExecutor implements Sensor {
private static final Logger LOGGER = Loggers.get(CustomScannerExecutor.class);
@Override
public void describe(SensorDescriptor descriptor) {
descriptor.name("Custom Autosys Linter");
}
@Override
public void execute(SensorContext context) {
try {
// Define paths
String pythonScriptPath = "path/to/your/script.py"; // Adjust this path
String baseDirectory = context.fileSystem().baseDir().getAbsolutePath();
String outputFilePath = baseDirectory + "/sonar-issues.json";
// Execute the Python script
ProcessBuilder processBuilder = new ProcessBuilder(
"python", pythonScriptPath, baseDirectory, outputFilePath
);
Process process = processBuilder.start();
process.waitFor();
// Log the script output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
LOGGER.info(line);
}
// Import the JSON report
Path reportPath = Paths.get(outputFilePath);
if (Files.exists(reportPath)) {
context.newExternalIssuesLoader().importExternalIssues(
reportPath.toFile(),
context.config().get("sonar.projectKey").orElse("default")
);
LOGGER.info("External issues report imported successfully.");
} else {
LOGGER.warn("No issues report found at: " + outputFilePath);
}
} catch (Exception e) {
LOGGER.error("Error executing custom scanner", e);
}
}
}
CustomScannerPlugin.java
import org.sonar.api.Plugin;
public class CustomScannerPlugin implements Plugin {
@Override
public void define(Context context) {
context.addExtension(CustomScannerExecutor.class);
}
}