diff options
author | Godin <mandrikov@gmail.com> | 2010-11-01 22:02:13 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-11-01 22:02:13 +0000 |
commit | f4a655bb737d89ee8503b63ec0eecd01ef9402c3 (patch) | |
tree | c833c334eb610dfc0d6a545010ae1b4d487d6ce7 /plugins | |
parent | eca9e1bc2cea58b31341d7f6dc5d12515a4b8bf9 (diff) | |
download | sonarqube-f4a655bb737d89ee8503b63ec0eecd01ef9402c3.tar.gz sonarqube-f4a655bb737d89ee8503b63ec0eecd01ef9402c3.zip |
SONAR-1772: Add annotations.jar and jsr305.jar to auxiliary classpath before launching Findbugs analysis
Diffstat (limited to 'plugins')
5 files changed, 81 insertions, 19 deletions
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java index 3505002353e..0ea12682b85 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java @@ -1,5 +1,9 @@ package org.sonar.plugins.findbugs; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; + import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; @@ -11,10 +15,6 @@ import org.sonar.plugins.findbugs.xml.ClassFilter; import org.sonar.plugins.findbugs.xml.FindBugsFilter; import org.sonar.plugins.findbugs.xml.Match; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; - /** * @since 2.4 */ @@ -24,12 +24,15 @@ public class FindbugsConfiguration implements BatchExtension { private RulesProfile profile; private FindbugsProfileExporter exporter; private ProjectClasspath projectClasspath; + private FindbugsDownloader downloader; - public FindbugsConfiguration(Project project, RulesProfile profile, FindbugsProfileExporter exporter, ProjectClasspath classpath) { + public FindbugsConfiguration(Project project, RulesProfile profile, FindbugsProfileExporter exporter, ProjectClasspath classpath, + FindbugsDownloader downloader) { this.project = project; this.profile = profile; this.exporter = exporter; this.projectClasspath = classpath; + this.downloader = downloader; } public File getTargetXMLReport() { @@ -56,6 +59,9 @@ public class FindbugsConfiguration implements BatchExtension { findbugsProject.addAuxClasspathEntry(file.getAbsolutePath()); } } + for (File file : downloader.getLibs()) { + findbugsProject.addAuxClasspathEntry(file.getAbsolutePath()); + } findbugsProject.setCurrentWorkingDirectory(project.getFileSystem().getBuildDir()); return findbugsProject; } @@ -78,7 +84,8 @@ public class FindbugsConfiguration implements BatchExtension { } public String getEffort() { - return StringUtils.lowerCase(project.getConfiguration().getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY, CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE)); + return StringUtils.lowerCase(project.getConfiguration().getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY, + CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE)); } public long getTimeout() { diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsDownloader.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsDownloader.java new file mode 100644 index 00000000000..30d8ad238d5 --- /dev/null +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsDownloader.java @@ -0,0 +1,59 @@ +package org.sonar.plugins.findbugs; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.configuration.Configuration; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.BatchExtension; +import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.SonarException; + +public class FindbugsDownloader implements BatchExtension { + + private static final String FINDBUGS_URL = "/deploy/plugins/findbugs/"; + + private static List<File> libs; + + private HttpDownloader downloader; + private String host; + + public FindbugsDownloader(Configuration configuration, HttpDownloader downloader) { + this.downloader = downloader; + host = StringUtils.chomp(configuration.getString("sonar.host.url", "http://localhost:9000"), "/"); + } + + public synchronized List<File> getLibs() { + if (libs == null) { + libs = Arrays.asList(downloadLib(getUrlForAnnotationsJar()), downloadLib(getUrlForJsrJar())); + } + return libs; + } + + private String getUrlForAnnotationsJar() { + return host + FINDBUGS_URL + "/annotations-" + FindbugsVersion.getVersion() + ".jar"; + } + + private String getUrlForJsrJar() { + return host + FINDBUGS_URL + "/jsr305-" + FindbugsVersion.getVersion() + ".jar"; + } + + private File downloadLib(String url) { + try { + URI uri = new URI(url); + File temp = File.createTempFile("findbugs", ".jar"); + FileUtils.forceDeleteOnExit(temp); + downloader.download(uri, temp); + return temp; + } catch (URISyntaxException e) { + throw new SonarException(e); + } catch (IOException e) { + throw new SonarException(e); + } + } +} diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java index 01789858855..912cf15899f 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsPlugin.java @@ -34,18 +34,13 @@ import org.sonar.api.Property; defaultValue = CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE, name = "Effort", description = "Effort of the bug finders. Valid values are Min, Default and Max. Setting 'Max' increases precision but also increases memory consumption.", - project = true, - module = true, - global = true), + project = true, module = true, global = true), @Property( key = CoreProperties.FINDBUGS_TIMEOUT_PROPERTY, defaultValue = CoreProperties.FINDBUGS_TIMEOUT_DEFAULT_VALUE + "", name = "Timeout", description = "Specifies the amount of time, in milliseconds, that FindBugs may run before it is assumed to be hung and is terminated. The default is 600,000 milliseconds, which is ten minutes.", - project = true, - module = true, - global = true) -}) + project = true, module = true, global = true) }) public class FindbugsPlugin implements Plugin { public String getKey() { @@ -64,6 +59,7 @@ public class FindbugsPlugin implements Plugin { List<Class<? extends Extension>> list = new ArrayList<Class<? extends Extension>>(); list.add(FindbugsSensor.class); list.add(FindbugsConfiguration.class); + list.add(FindbugsDownloader.class); list.add(FindbugsExecutor.class); list.add(FindbugsRuleRepository.class); list.add(FindbugsProfileExporter.class); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java index 00ead6916fa..21ae80ec149 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsConfigurationTest.java @@ -5,6 +5,8 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.io.File; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -13,8 +15,6 @@ import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; import org.sonar.api.test.SimpleProjectFileSystem; -import java.io.File; - public class FindbugsConfigurationTest { @Rule @@ -32,7 +32,7 @@ public class FindbugsConfigurationTest { @Test public void shouldSaveConfigFiles() throws Exception { - FindbugsConfiguration conf = new FindbugsConfiguration(project, RulesProfile.create(), new FindbugsProfileExporter(), null); + FindbugsConfiguration conf = new FindbugsConfiguration(project, RulesProfile.create(), new FindbugsProfileExporter(), null, null); conf.saveIncludeConfigXml(); conf.saveExcludeConfigXml(); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java index e03cde1a667..aaadc523843 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java @@ -6,6 +6,8 @@ import static org.junit.internal.matchers.StringContains.containsString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.io.File; + import org.apache.commons.io.FileUtils; import org.junit.Test; import org.sonar.api.CoreProperties; @@ -13,8 +15,6 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.utils.SonarException; -import java.io.File; - public class FindbugsExecutorTest { @Test @@ -45,7 +45,7 @@ public class FindbugsExecutorTest { Project project = mock(Project.class); ProjectFileSystem fs = mock(ProjectFileSystem.class); when(project.getFileSystem()).thenReturn(fs); - FindbugsConfiguration conf = new FindbugsConfiguration(project, null, null, null); + FindbugsConfiguration conf = new FindbugsConfiguration(project, null, null, null, null); new FindbugsExecutor(conf).execute(); } |