【发布时间】:2018-07-16 19:35:08
【问题描述】:
我正在尝试使用此代码从 sonarqube 上的文件名中获取问题
public class SonarqubeServiceImpl implements SonarqubeService {
private final String SONAR_URL = "http://localhost:9000/";
private final String PROJECT_KEY = "refactor2";
private final String SRC_FOLDER = "src/main/java/com/uca/refactor2/activities";
private String EXECUTE_SONAR;
private String GET_ISSUES_URL = SONAR_URL + "api/issues/search?q=";
private static String POM_PATH = "pom.xml";
private RestTemplate restTemplate = new RestTemplate();
private InvocationRequest request;
private Invoker invoker;
@PostConstruct
private void init() {
System.out.println("PostConstruct");
buildSonarCommand();
configureMavenWithSonar();
}
@Override
public void runAnalysis() throws MavenInvocationException {
// Assuming everything is set up (done in init)
invoker.execute(request);
}
@Override
public IssuesResponse getIssuesFromFileName(String fileName) {
String URL = GET_ISSUES_URL + fileName;
return restTemplate.getForObject(URL, IssuesResponse.class);
}
private void configureMavenWithSonar() {
request = new DefaultInvocationRequest();
request.setPomFile(new File(POM_PATH));
request.setGoals(Collections.singletonList(EXECUTE_SONAR));
invoker = new DefaultInvoker();
// Set maven home in case env variables are not set
// (and using own installation)
invoker.setMavenHome(new File("apache-maven-3.5.2"));
}
private void buildSonarCommand() {
StringBuilder builder = new StringBuilder();
builder.append("sonar:sonar ");
builder.append(String.format("-Dsonar.host.url=%s ", SONAR_URL));
builder.append(String.format("-Dsonar.projectKey=%s ", PROJECT_KEY));
builder.append(String.format("-Dsonar.sources=%s ", SRC_FOLDER));
EXECUTE_SONAR = builder.toString();
}
}
这段代码有两个问题。第一个是这是从很久以前删除的源文件夹的文件名中检索问题,我不明白为什么,因为如果我在 localhost/9000 上输入 sonarqube 本地 web api,则该代码不存在
第二个也是最重要的,我正在尝试从文件名中获取问题,但是 sonarqube 给了我来自所有项目的问题(即使是我有多个项目的项目),包括我上面提到的第一个问题中删除的代码.
我正在获取此 URL 的问题
http://localhost:9000/api/issues/search?q=" + fileName;
我正在使用 SonarQube 版本:6.7.1
这是我第一次使用 sonarqube,所以也许我错过了什么
【问题讨论】:
-
以防万一(避免任何双重调查),也在这里讨论:community.sonarsource.com/t/…