001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.plugins.core.batch;
021
022import org.apache.commons.configuration.Configuration;
023import org.apache.commons.lang.StringUtils;
024import org.apache.maven.project.MavenProject;
025import org.sonar.api.CoreProperties;
026import org.sonar.api.batch.Initializer;
027import org.sonar.api.batch.SupportedEnvironment;
028import org.sonar.api.batch.maven.MavenUtils;
029import org.sonar.api.resources.Project;
030import org.sonar.api.utils.Logs;
031import org.sonar.java.api.JavaUtils;
032
033@SupportedEnvironment("maven")
034public class MavenInitializer extends Initializer {
035
036  @Override
037  public void execute(Project project) {
038    MavenProject pom = project.getPom();
039    Configuration conf = project.getConfiguration();
040    /*
041     * See http://jira.codehaus.org/browse/SONAR-2148
042     * Get Java source and target versions from maven-compiler-plugin.
043     */
044    if (StringUtils.isBlank(conf.getString(JavaUtils.JAVA_SOURCE_PROPERTY))) {
045      String version = MavenUtils.getJavaSourceVersion(pom);
046      conf.setProperty(JavaUtils.JAVA_SOURCE_PROPERTY, version);
047      Logs.INFO.info("Java source version: {}", JavaUtils.getSourceVersion(project));
048    }
049    if (StringUtils.isBlank(conf.getString(JavaUtils.JAVA_TARGET_PROPERTY))) {
050      String version = MavenUtils.getJavaVersion(pom);
051      conf.setProperty(JavaUtils.JAVA_TARGET_PROPERTY, version);
052      Logs.INFO.info("Java target version: {}", JavaUtils.getTargetVersion(project));
053    }
054    /*
055     * See http://jira.codehaus.org/browse/SONAR-2151
056     * Get source encoding from POM
057     */
058    if (StringUtils.isBlank(conf.getString(CoreProperties.ENCODING_PROPERTY))) {
059      String encoding = MavenUtils.getSourceEncoding(pom);
060      conf.setProperty(CoreProperties.ENCODING_PROPERTY, encoding);
061      Logs.INFO.info("Source encoding: {}", encoding);
062    }
063  }
064
065}