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;
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))
*/
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
*/
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
*/
/**
* An utility class to manipulate Maven concepts
- *
+ *
* @since 1.10
*/
public final class MavenUtils {
/**
* Returns the version of Java used by the maven compiler plugin
- *
+ *
* @param pom the project pom
* @return the java version
*/
/**
* 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
/**
* 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
/**
* 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
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);
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;
}
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);
*/
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;
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;