001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.scan;
021
022 import org.sonar.api.batch.bootstrap.ProjectDefinition;
023 import org.sonar.batch.bootstrap.TaskProperties;
024
025 import java.io.File;
026 import java.io.IOException;
027 import java.util.Map.Entry;
028 import java.util.Properties;
029
030 /**
031 * Class that creates a project definition based on a set of properties. This class is intended to work with
032 * SQ Runner <= 2.3. Starting from SQ Runner 2.4 loading of sonar-project.properties file should not be done by
033 * the core.
034 * @deprecated since 4.3 should be removed when we will require SQ Runner 2.4+
035 */
036 @Deprecated
037 public class DeprecatedProjectReactorBuilder extends ProjectReactorBuilder {
038
039 private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
040
041 public DeprecatedProjectReactorBuilder(TaskProperties props) {
042 super(props);
043 }
044
045 @Override
046 protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
047 final File baseDir;
048 if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
049 baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
050 setProjectBaseDir(baseDir, moduleProps, moduleId);
051 try {
052 if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
053 tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
054 }
055 } catch (IOException e) {
056 throw new IllegalStateException("Error when resolving baseDir", e);
057 }
058 } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
059 baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
060 } else {
061 baseDir = new File(parentProject.getBaseDir(), moduleId);
062 setProjectBaseDir(baseDir, moduleProps, moduleId);
063 tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
064 }
065
066 setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey());
067
068 // and finish
069 checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
070 validateDirectories(moduleProps, baseDir, moduleId);
071
072 mergeParentProperties(moduleProps, parentProject.getProperties());
073
074 return defineProject(moduleProps, parentProject);
075 }
076
077 /**
078 * @return baseDir
079 */
080 private File loadPropsFile(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
081 File propertyFile = resolvePath(parentProject.getBaseDir(), moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE));
082 if (propertyFile.isFile()) {
083 Properties propsFromFile = toProperties(propertyFile);
084 for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
085 moduleProps.put(entry.getKey(), entry.getValue());
086 }
087 File baseDir = null;
088 if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
089 baseDir = resolvePath(propertyFile.getParentFile(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
090 } else {
091 baseDir = propertyFile.getParentFile();
092 }
093 setProjectBaseDir(baseDir, moduleProps, moduleId);
094 return baseDir;
095 } else {
096 throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
097 }
098 }
099
100 private void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
101 File propertyFile = new File(baseDir, "sonar-project.properties");
102 if (propertyFile.isFile()) {
103 Properties propsFromFile = toProperties(propertyFile);
104 for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
105 moduleProps.put(entry.getKey(), entry.getValue());
106 }
107 if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
108 File overwrittenBaseDir = resolvePath(propertyFile.getParentFile(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
109 setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
110 }
111 }
112 }
113
114 }