+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.sonar.batch.scan;
-
-import org.sonar.batch.analysis.AnalysisProperties;
-
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-
-/**
- * Class that creates a project definition based on a set of properties. This class is intended to work with
- * SQ Runner <= 2.3. Starting from SQ Runner 2.4 loading of sonar-project.properties file should not be done by
- * the core.
- * @deprecated since 4.3 should be removed when we will require SQ Runner 2.4+
- */
-@Deprecated
-public class DeprecatedProjectReactorBuilder extends ProjectReactorBuilder {
-
- private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
-
- public DeprecatedProjectReactorBuilder(AnalysisProperties props) {
- super(props);
- }
-
- @Override
- protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) {
- final File baseDir;
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_BASEDIR));
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- try {
- if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Error when resolving baseDir", e);
- }
- } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
- baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
- } else {
- baseDir = new File(parentProject.getBaseDir(), moduleId);
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
- }
-
- setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey());
-
- // and finish
- checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
- validateDirectories(moduleProps, baseDir, moduleId);
-
- mergeParentProperties(moduleProps, parentProject.properties());
-
- return defineRootProject(moduleProps, parentProject);
- }
-
- /**
- * @return baseDir
- */
- private File loadPropsFile(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) {
- File propertyFile = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_CONFIG_FILE));
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey().toString(), entry.getValue().toString());
- }
- File baseDir = null;
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = resolvePath(propertyFile.getParentFile(), moduleProps.get(PROPERTY_PROJECT_BASEDIR));
- } else {
- baseDir = propertyFile.getParentFile();
- }
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- return baseDir;
- } else {
- throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
- }
- }
-
- private void tryToFindAndLoadPropsFile(File baseDir, Map<String, String> moduleProps, String moduleId) {
- File propertyFile = new File(baseDir, "sonar-project.properties");
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey().toString(), entry.getValue().toString());
- }
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- File overwrittenBaseDir = resolvePath(propertyFile.getParentFile(), moduleProps.get(PROPERTY_PROJECT_BASEDIR));
- setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
- }
- }
- }
-
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.sonar.batch.scan;
-
-import org.sonar.batch.analysis.AnalysisProperties;
-
-import com.google.common.collect.Maps;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.bootstrap.ProjectReactor;
-import org.sonar.test.TestUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class DeprecatedProjectReactorBuilderTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void shouldDefineMultiModuleProjectWithDefinitionsAllInEachModule() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module");
-
- // CHECK ROOT
- assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
- assertThat(rootProject.getName()).isEqualTo("Foo Project");
- assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
- // root project must not contain some properties - even if they are defined in the root properties file
- assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
- assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
- assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
- // and module properties must have been cleaned
- assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // CHECK MODULES
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(2);
-
- // Module 1
- ProjectDefinition module1 = modules.get(0);
- assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
- assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- assertThat(module1.getName()).isEqualTo("Foo Module 1");
- assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
- // Description should not be inherited from parent if not set
- assertThat(module1.getDescription()).isEqualTo("Description of Module 1");
- assertThat(module1.getSourceDirs()).contains("sources");
- assertThat(module1.getTestDirs()).contains("tests");
- assertThat(module1.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // Module 2
- ProjectDefinition module2 = modules.get(1);
- assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module2/newBaseDir"));
- assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
- assertThat(module2.getName()).isEqualTo("Foo Module 2");
- assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
- assertThat(module2.getSourceDirs()).contains("src");
- assertThat(module2.getTestDirs()).contains("tests");
- assertThat(module2.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
- }
-
- @Test
- public void shouldDefineMultiModuleProjectWithDefinitionsModule1Inherited() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module-inherited");
-
- // CHECK ROOT
- assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
- assertThat(rootProject.getName()).isEqualTo("Foo Project");
- assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
- // root project must not contain some properties - even if they are defined in the root properties file
- assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
- assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
- assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
- // and module properties must have been cleaned
- assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // CHECK MODULES
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(2);
-
- // Module 1
- ProjectDefinition module1 = modules.get(0);
- assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module1"));
- assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
- assertThat(module1.getName()).isEqualTo("module1");
- assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
- // Description should not be inherited from parent if not set
- assertThat(module1.getDescription()).isNull();
- assertThat(module1.getSourceDirs()).contains("sources");
- assertThat(module1.getTestDirs()).contains("tests");
- assertThat(module1.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // Module 2
- ProjectDefinition module2 = modules.get(1);
- assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module2/newBaseDir"));
- assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
- assertThat(module2.getName()).isEqualTo("Foo Module 2");
- assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
- assertThat(module2.getSourceDirs()).contains("src");
- assertThat(module2.getTestDirs()).contains("tests");
- assertThat(module2.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
- }
-
- @Test
- public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(1);
- ProjectDefinition module = modules.get(0);
- assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- // verify the base directory that has been changed in this config file
- assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
- }
-
- @Test
- public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(1);
- ProjectDefinition module = modules.get(0);
- assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- // verify the base directory that has been changed in this config file
- assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
- }
-
- @Test
- public void shouldFailIfUnexistingModuleFile() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("The properties file of the module 'module1' does not exist: "
- + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
- + File.separator + "any-file.properties");
-
- loadProjectDefinition("multi-module-with-unexisting-file");
- }
-
- private ProjectDefinition loadProjectDefinition(String projectFolder) {
- Map<String, String> props = Maps.<String, String>newHashMap();
- Properties runnerProps = ProjectReactorBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
- for (final String name : runnerProps.stringPropertyNames()) {
- props.put(name, runnerProps.getProperty(name));
- }
- props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
- AnalysisProperties taskProps = new AnalysisProperties(props, null);
- ProjectReactor projectReactor = new DeprecatedProjectReactorBuilder(taskProps).execute();
- return projectReactor.getRoot();
- }
-
-}
*/
package org.sonar.batch.scan;
-import org.sonar.batch.analysis.AnalysisProperties;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.batch.analysis.AnalysisProperties;
import com.google.common.collect.Maps;
import org.junit.Rule;
import org.junit.Test;
assertThat(props.get("sonar.projectName")).isEqualTo("foo");
}
- @Test
- public void shouldFailToLoadPropertiesFile() {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Impossible to read the property file");
-
- ProjectReactorBuilder.toProperties(new File("foo.properties"));
- }
-
private ProjectDefinition loadProjectDefinition(String projectFolder) {
Map<String, String> props = loadProps(projectFolder);
AnalysisProperties bootstrapProps = new AnalysisProperties(props, null);
ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps).execute();
return projectReactor.getRoot();
}
+
+ protected static Properties toProperties(File propertyFile) {
+ Properties propsFromFile = new Properties();
+ try (FileInputStream fileInputStream = new FileInputStream(propertyFile)) {
+ propsFromFile.load(fileInputStream);
+ } catch (IOException e) {
+ throw new IllegalStateException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e);
+ }
+ // Trim properties
+ for (String propKey : propsFromFile.stringPropertyNames()) {
+ propsFromFile.setProperty(propKey, StringUtils.trim(propsFromFile.getProperty(propKey)));
+ }
+ return propsFromFile;
+ }
private Map<String, String> loadProps(String projectFolder) {
Map<String, String> props = Maps.<String, String>newHashMap();
- Properties runnerProps = ProjectReactorBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
+ Properties runnerProps = toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
for (final String name : runnerProps.stringPropertyNames()) {
props.put(name, runnerProps.getProperty(name));
}