diff options
7 files changed, 72 insertions, 26 deletions
diff --git a/plugins/sonar-clover-plugin/pom.xml b/plugins/sonar-clover-plugin/pom.xml index 463e3b47601..972298145e6 100644 --- a/plugins/sonar-clover-plugin/pom.xml +++ b/plugins/sonar-clover-plugin/pom.xml @@ -52,6 +52,11 @@ <artifactId>sonar-plugin-api</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-java-api</artifactId> + <version>${project.version}</version> + </dependency> <!-- TODO http://jira.codehaus.org/browse/SONAR-2011 We need following dependency, otherwise we will receive compilation error diff --git a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenPluginHandler.java b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenPluginHandler.java index 09b1893bfbe..bd1db7af013 100644 --- a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenPluginHandler.java +++ b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenPluginHandler.java @@ -27,7 +27,7 @@ import org.sonar.api.batch.maven.MavenPlugin; import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.batch.maven.MavenSurefireUtils; import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectUtils; +import org.sonar.java.api.JavaUtils; import java.io.File; import java.io.IOException; @@ -58,7 +58,7 @@ public class CloverMavenPluginHandler implements MavenPluginHandler { } public String[] getGoals() { - return new String[]{"instrument", "clover"}; + return new String[] { "instrument", "clover" }; } public void configure(Project project, MavenPlugin cloverPlugin) { @@ -75,13 +75,14 @@ public class CloverMavenPluginHandler implements MavenPluginHandler { skipCloverLaunch = StringUtils.isNotBlank(skipInPomConfig) ? Boolean.parseBoolean(skipInPomConfig) : false; } if (!project.getConfiguration().containsKey(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY) && !skipCloverLaunch) { - project.getConfiguration().setProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY, new File(project.getFileSystem().getBuildDir(), "clover/surefire-reports").getAbsolutePath()); + project.getConfiguration().setProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY, + new File(project.getFileSystem().getBuildDir(), "clover/surefire-reports").getAbsolutePath()); } } protected void configureParameters(Project project, MavenPlugin cloverPlugin) { cloverPlugin.setParameter("generateXml", "true"); - String javaVersion = ProjectUtils.getJavaVersion(project); + String javaVersion = JavaUtils.getTargetVersion(project); if (javaVersion != null) { cloverPlugin.setParameter("jdk", javaVersion); } @@ -101,7 +102,8 @@ public class CloverMavenPluginHandler implements MavenPluginHandler { } private boolean hasLicense(MavenPlugin cloverPlugin) { - return StringUtils.isNotBlank(cloverPlugin.getParameter("license")) || StringUtils.isNotBlank(cloverPlugin.getParameter("licenseLocation")); + return StringUtils.isNotBlank(cloverPlugin.getParameter("license")) + || StringUtils.isNotBlank(cloverPlugin.getParameter("licenseLocation")); } private File writeLicenseToDisk(Project project, String license) { diff --git a/plugins/sonar-pmd-plugin/pom.xml b/plugins/sonar-pmd-plugin/pom.xml index 6e32d6c4a01..b296abe8230 100644 --- a/plugins/sonar-pmd-plugin/pom.xml +++ b/plugins/sonar-pmd-plugin/pom.xml @@ -25,6 +25,11 @@ </dependency> <dependency> <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-java-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.codehaus.sonar</groupId> <artifactId>sonar-plugin-api</artifactId> <version>${project.version}</version> </dependency> diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index ea55bee3499..d68e61ddddd 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -29,8 +29,8 @@ import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectUtils; import org.sonar.api.utils.TimeProfiler; +import org.sonar.java.api.JavaUtils; import java.io.*; import java.util.List; @@ -60,17 +60,17 @@ public class PmdExecutor implements BatchExtension { ruleContext.setReport(report); RuleSets rulesets = createRulesets(); - + for (File file : project.getFileSystem().getSourceFiles(Java.INSTANCE)) { ruleContext.setSourceCodeFilename(file.getAbsolutePath()); Reader fileReader = new InputStreamReader(new FileInputStream(file), project.getFileSystem().getSourceCharset()); try { pmd.processFile(fileReader, rulesets, ruleContext); - - } catch(PMDException e) { + + } catch (PMDException e) { LOG.error("Fail to execute PMD. Following file is ignored: " + file, e.getCause()); - } catch(Exception e) { + } catch (Exception e) { LOG.error("Fail to execute PMD. Following file is ignored: " + file, e); } finally { @@ -143,7 +143,7 @@ public class PmdExecutor implements BatchExtension { } private void setJavaVersion(PMD pmd, Project project) { - String javaVersion = ProjectUtils.getJavaSourceVersion(project); + String javaVersion = JavaUtils.getSourceVersion(project); if (StringUtils.isNotBlank(javaVersion)) { if ("1.1".equals(javaVersion) || "1.2".equals(javaVersion)) { javaVersion = "1.3"; diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java index 4e138e5cfec..d10a4b35516 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java @@ -30,6 +30,7 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; +import org.sonar.java.api.JavaUtils; import java.text.DateFormat; import java.text.ParseException; @@ -77,8 +78,19 @@ public class MavenProjectBuilder { Date analysisDate = loadAnalysisDate(projectConfiguration); MavenProject pom = project.getPom(); if (pom != null) { - projectConfiguration.setProperty("sonar.java.sourceVersion", MavenUtils.getJavaSourceVersion(pom)); - projectConfiguration.setProperty("sonar.java.targetVersion", MavenUtils.getJavaVersion(pom)); + /* + * TODO actually this is a dirty hack to get Java source and target versions from maven-compiler-plugin + * See http://jira.codehaus.org/browse/SONAR-2148 + * In fact we should try to get rid of it and use concept of configurator, + * which would be active only in Maven environment + * and would be responsible for setting values from corresponding maven-plugin. + */ + if (projectConfiguration.getProperty(JavaUtils.JAVA_SOURCE_PROPERTY) == null) { + projectConfiguration.setProperty(JavaUtils.JAVA_SOURCE_PROPERTY, MavenUtils.getJavaSourceVersion(pom)); + } + if (projectConfiguration.getProperty(JavaUtils.JAVA_TARGET_PROPERTY) == null) { + projectConfiguration.setProperty(JavaUtils.JAVA_TARGET_PROPERTY, MavenUtils.getJavaVersion(pom)); + } } project.setConfiguration(projectConfiguration) .setExclusionPatterns(loadExclusionPatterns(projectConfiguration)) diff --git a/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java index 9fa234f5626..26ee5d17425 100644 --- a/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java +++ b/sonar-java-api/src/main/java/org/sonar/java/api/JavaUtils.java @@ -20,7 +20,11 @@ package org.sonar.java.api; import org.apache.commons.lang.StringUtils; +import org.sonar.api.resources.Project; +/** + * @since 2.6 + */ public final class JavaUtils { public static final String PACKAGE_SEPARATOR = "."; @@ -37,6 +41,16 @@ public final class JavaUtils { */ public static final String BARRIER_AFTER_SQUID = "squid"; + /** + * To determine value of this property use {@link #getSourceVersion(Project)}. + */ + public static final String JAVA_SOURCE_PROPERTY = "sonar.java.source"; + + /** + * To determine value of this property use {@link #getTargetVersion(Project)}. + */ + public static final String JAVA_TARGET_PROPERTY = "sonar.java.target"; + private JavaUtils() { // only static methods } @@ -44,12 +58,20 @@ public final class JavaUtils { public static String abbreviatePackage(String packageName) { String[] parts = StringUtils.split(packageName, PACKAGE_SEPARATOR); StringBuilder sb = new StringBuilder(); - if (parts.length>=1) { + if (parts.length >= 1) { sb.append(parts[0]); } - for (int index=1 ; index<parts.length ; index++) { + for (int index = 1; index < parts.length; index++) { sb.append(PACKAGE_SEPARATOR).append(parts[index].charAt(0)); } return sb.toString(); } -}
\ No newline at end of file + + public static String getSourceVersion(Project project) { + return project.getConfiguration().getString(JAVA_SOURCE_PROPERTY); + } + + public static String getTargetVersion(Project project) { + return project.getConfiguration().getString(JAVA_TARGET_PROPERTY); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java index 1998fbbdaf1..b81c3dc7b46 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectUtils.java @@ -20,9 +20,8 @@ package org.sonar.api.resources; /** - * FIXME - * Actually this class incorrectly named, because provides information not about project, but about Java project. - * And seems that only core plugins use this class. + * @TODO Actually this class incorrectly named, because provides information not about project, but about Java project. + * And seems that only core plugins use this class. * * @since 1.10 */ @@ -33,22 +32,23 @@ public final class ProjectUtils { } /** - * Java version as defined in maven-compiler-plugin + * @deprecated since 2.6 use JavaUtils.getTargetVersion() instead. */ + @Deprecated public static String getJavaVersion(Project project) { - // target version - // TODO was return MavenUtils.getJavaVersion(project.getPom()); if (project.getConfiguration() != null) { - return project.getConfiguration().getString("sonar.java.targetVersion"); + return project.getConfiguration().getString("sonar.java.target"); } return null; } + /** + * @deprecated since 2.6 use JavaUtils.getSourceVersion() instead. + */ + @Deprecated public static String getJavaSourceVersion(Project project) { - // source version - // TODO was return MavenUtils.getJavaSourceVersion(project.getPom()); if (project.getConfiguration() != null) { - return project.getConfiguration().getString("sonar.java.sourceVersion"); + return project.getConfiguration().getString("sonar.java.source"); } return null; } |