org.sonarsource.scanner.cli.Conf Maven / Gradle / Ivy
/*
* SonarQube Scanner
* Copyright (C) 2011-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.scanner.cli;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
class Conf {
private static final String SCANNER_HOME = "scanner.home";
private static final String SCANNER_SETTINGS = "scanner.settings";
private static final String PROJECT_HOME = "project.home";
private static final String PROJECT_SETTINGS = "project.settings";
private static final String PROPERTY_MODULES = "sonar.modules";
private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
private final Cli cli;
private final Logs logger;
Conf(Cli cli, Logs logger) {
this.cli = cli;
this.logger = logger;
}
Properties properties() throws IOException {
Properties result = new Properties();
result.putAll(loadGlobalProperties());
result.putAll(loadProjectProperties());
result.putAll(System.getProperties());
result.putAll(cli.properties());
// root project base directory must be present and be absolute
result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString());
result.remove(PROJECT_HOME);
return result;
}
private Properties loadGlobalProperties() throws IOException {
Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties", SCANNER_SETTINGS);
if (settingsFile != null && Files.isRegularFile(settingsFile)) {
logger.info("Scanner configuration file: " + settingsFile);
return toProperties(settingsFile);
}
logger.info("Scanner configuration file: NONE");
return new Properties();
}
private Properties loadProjectProperties() throws IOException {
Properties rootProps = new Properties();
Properties knownProps = new Properties();
knownProps.putAll(System.getProperties());
knownProps.putAll(cli.properties());
Path defaultRootSettingsFile = getRootProjectBaseDir(knownProps).resolve(SONAR_PROJECT_PROPERTIES_FILENAME);
Path rootSettingsFile = locatePropertiesFile(defaultRootSettingsFile, knownProps, PROJECT_SETTINGS);
if (rootSettingsFile != null && Files.isRegularFile(rootSettingsFile)) {
logger.info("Project root configuration file: " + rootSettingsFile);
rootProps.putAll(toProperties(rootSettingsFile));
} else {
logger.info("Project root configuration file: NONE");
}
Properties projectProps = new Properties();
// include already root base directory and eventually props loaded from root config file
projectProps.putAll(rootProps);
rootProps.putAll(knownProps);
rootProps.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(rootProps).toString());
// projectProps will be overridden by any properties found in child project settings
loadModulesProperties(rootProps, projectProps, "");
return projectProps;
}
private static Path getRootProjectBaseDir(Properties cliProps) {
Path absoluteProjectHome;
if (cliProps.containsKey(PROJECT_HOME)) {
absoluteProjectHome = Paths.get(cliProps.getProperty(PROJECT_HOME)).toAbsolutePath();
} else {
// this should always be avoided, as it will resolve symbolic links
absoluteProjectHome = Paths.get("").toAbsolutePath();
}
if (!cliProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
return absoluteProjectHome;
}
return getAbsolutePath(cliProps.getProperty(PROPERTY_PROJECT_BASEDIR), absoluteProjectHome);
}
private void loadModulesProperties(Properties parentProps, Properties projectProps, String prefix) {
Path parentBaseDir = Paths.get(parentProps.getProperty(PROPERTY_PROJECT_BASEDIR));
if (parentProps.containsKey(PROPERTY_MODULES)) {
for (String module : getListFromProperty(parentProps, PROPERTY_MODULES)) {
Properties moduleProps = extractModuleProperties(module, parentProps);
// higher priority to child configuration files
loadModuleConfigFile(parentBaseDir, moduleProps, module);
// the child project may have children as well
loadModulesProperties(moduleProps, projectProps, prefix + module + ".");
// and finally add this child properties to global props
merge(projectProps, prefix, module, moduleProps);
}
}
}
private static void merge(Properties projectProps, String prefix, String module, Properties moduleProps) {
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy