From 1c2189b45f0a81436fcaab886b7072aea77a7d01 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 28 Jan 2011 13:16:11 +0300 Subject: [PATCH] SONAR-2151: Add new property to manage source encoding --- .../org/sonar/batch/MavenProjectBuilder.java | 18 ++++++++++----- .../java/org/sonar/api/CoreProperties.java | 9 ++++++++ .../org/sonar/api/batch/maven/MavenUtils.java | 23 ++++++++++++------- .../resources/DefaultProjectFileSystem.java | 4 ++-- .../org/sonar/api/test/MavenTestUtils.java | 9 +++++++- 5 files changed, 46 insertions(+), 17 deletions(-) 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 d10a4b35516..7fde1c352b7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectBuilder.java @@ -20,6 +20,7 @@ package org.sonar.batch; import org.apache.commons.configuration.*; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; import org.apache.maven.project.MavenProject; import org.sonar.api.CoreProperties; @@ -79,18 +80,23 @@ public class MavenProjectBuilder { MavenProject pom = project.getPom(); if (pom != null) { /* - * TODO actually this is a dirty hack to get Java source and target versions from maven-compiler-plugin + * TODO actually this is a dirty hack * 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. + * Get Java source and target versions from maven-compiler-plugin. */ - if (projectConfiguration.getProperty(JavaUtils.JAVA_SOURCE_PROPERTY) == null) { + if (StringUtils.isBlank(projectConfiguration.getString(JavaUtils.JAVA_SOURCE_PROPERTY))) { projectConfiguration.setProperty(JavaUtils.JAVA_SOURCE_PROPERTY, MavenUtils.getJavaSourceVersion(pom)); } - if (projectConfiguration.getProperty(JavaUtils.JAVA_TARGET_PROPERTY) == null) { + if (StringUtils.isBlank(projectConfiguration.getString(JavaUtils.JAVA_TARGET_PROPERTY))) { projectConfiguration.setProperty(JavaUtils.JAVA_TARGET_PROPERTY, MavenUtils.getJavaVersion(pom)); } + /* + * See http://jira.codehaus.org/browse/SONAR-2151 + * Get source encoding from POM + */ + if (StringUtils.isBlank(projectConfiguration.getString(CoreProperties.ENCODING_PROPERTY))) { + projectConfiguration.setProperty(CoreProperties.ENCODING_PROPERTY, MavenUtils.getSourceEncoding(pom)); + } } project.setConfiguration(projectConfiguration) .setExclusionPatterns(loadExclusionPatterns(projectConfiguration)) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 235126c79e1..f39be2c6c7a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -19,6 +19,8 @@ */ package org.sonar.api; +import org.sonar.api.resources.ProjectFileSystem; + /** * CoreProperties is used to group various properties of Sonar as well * as default values of configuration in a single place @@ -37,6 +39,13 @@ public interface CoreProperties { */ String PROJECT_KEY_PROPERTY = "sonar.projectKey"; + /** + * To determine value of this property use {@link ProjectFileSystem#getSourceCharset()}. + * + * @since 2.6 + */ + String ENCODING_PROPERTY = "sonar.sourceEncoding"; + /** * Value format is yyyy-MM-dd */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenUtils.java index 58719ab80bb..cfcb7e81485 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/MavenUtils.java @@ -30,7 +30,7 @@ import java.util.Collection; /** * An utility class to manipulate Maven concepts - * + * * @since 1.10 */ public final class MavenUtils { @@ -44,7 +44,7 @@ public final class MavenUtils { /** * Returns the version of Java used by the maven compiler plugin - * + * * @param pom the project pom * @return the java version */ @@ -66,7 +66,7 @@ public final class MavenUtils { /** * Queries a collection of plugins based on a group id and an artifact id and returns the plugin if it exists - * + * * @param plugins the plugins collection * @param groupId the group id * @param artifactId the artifact id @@ -85,7 +85,7 @@ public final class MavenUtils { /** * Tests whether a plugin has got a given artifact id and group id - * + * * @param plugin the plugin to test * @param groupId the group id * @param artifactId the artifact id @@ -103,7 +103,7 @@ public final class MavenUtils { /** * Tests whether a ReportPlugin has got a given artifact id and group id - * + * * @param plugin the ReportPlugin to test * @param groupId the group id * @param artifactId the artifact id @@ -119,14 +119,21 @@ public final class MavenUtils { return false; } + /** + * @return source encoding + */ + public static String getSourceEncoding(MavenProject pom) { + return pom.getProperties().getProperty("project.build.sourceEncoding"); + } + /** * Returns the charset of a pom - * + * * @param pom the project pom - * @return thee charset + * @return the charset */ public static Charset getSourceCharset(MavenProject pom) { - String encoding = pom.getProperties().getProperty("project.build.sourceEncoding"); + String encoding = getSourceEncoding(pom); if (StringUtils.isNotEmpty(encoding)) { try { return Charset.forName(encoding); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index 8252b60bf87..d63e8918d2c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -25,6 +25,7 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.filefilter.*; import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; import org.sonar.api.batch.FileFilter; import org.sonar.api.utils.Logs; import org.sonar.api.utils.SonarException; @@ -63,8 +64,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { } public Charset getSourceCharset() { - // TODO was return MavenUtils.getSourceCharset(project.getPom()); - String encoding = project.getConfiguration().getString("project.build.sourceEncoding"); + String encoding = project.getConfiguration().getString(CoreProperties.ENCODING_PROPERTY); if (StringUtils.isNotEmpty(encoding)) { try { return Charset.forName(encoding); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java b/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java index 7ab0604e223..609d86cc913 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/test/MavenTestUtils.java @@ -19,11 +19,14 @@ */ package org.sonar.api.test; +import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.MapConfiguration; import org.apache.commons.io.IOUtils; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.MavenProject; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.maven.MavenUtils; import org.sonar.api.resources.DefaultProjectFileSystem; import org.sonar.api.resources.Java; import org.sonar.api.resources.Languages; @@ -66,9 +69,13 @@ public final class MavenTestUtils { public static Project loadProjectFromPom(Class clazz, String path) { MavenProject pom = loadPom(clazz, path); + Configuration configuration = new MapConfiguration(pom.getProperties()); Project project = new Project(pom.getGroupId() + ":" + pom.getArtifactId()) .setPom(pom) - .setConfiguration(new MapConfiguration(pom.getProperties())); + .setConfiguration(configuration); + configuration.setProperty("sonar.java.source", MavenUtils.getJavaSourceVersion(pom)); + configuration.setProperty("sonar.java.target", MavenUtils.getJavaVersion(pom)); + configuration.setProperty(CoreProperties.ENCODING_PROPERTY, MavenUtils.getSourceEncoding(pom)); DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE)); project.setFileSystem(fs); return project; -- 2.39.5