package org.sonar.runner.internal;
-import org.sonar.runner.internal.bootstrapper.BootstrapException;
-
import org.sonar.runner.Runner;
import org.sonar.runner.RunnerException;
-
+import org.sonar.runner.internal.bootstrapper.BootstrapException;
import org.sonar.runner.internal.bootstrapper.utils.PrivateIOUtils;
-
import org.sonar.runner.utils.SonarRunnerVersion;
import java.io.File;
result.putAll(loadProjectProperties(commandLineProps));
result.putAll(commandLineProps);
- if (result.contains(PROJECT_HOME)) {
+ if (result.containsKey(PROJECT_HOME)) {
// the real property of the Sonar Runner is "sonar.projectDir"
String baseDir = result.getProperty(PROJECT_HOME);
result.remove(PROJECT_HOME);
}
private ProjectDefinition loadChildProjectFromProperties(ProjectDefinition parentProject, Properties childProps, String moduleId) {
+ setProjectKeyIfNotDefined(childProps, moduleId);
checkMandatoryProperties(moduleId, childProps, MANDATORY_PROPERTIES_FOR_CHILD);
mergeParentProperties(childProps, parentProject.getProperties());
return defineProject(childProps);
}
+ @VisibleForTesting
+ protected static void setProjectKeyIfNotDefined(Properties childProps, String moduleId) {
+ if (!childProps.containsKey(PROPERTY_PROJECT_KEY)) {
+ childProps.put(PROPERTY_PROJECT_KEY, moduleId);
+ }
+ }
+
@VisibleForTesting
protected static void checkUnicityOfChildKey(ProjectDefinition childProject, ProjectDefinition parentProject) {
for (ProjectDefinition definition : parentProject.getSubProjects()) {
SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
}
+ @Test
+ public void shouldSetProjectKeyIfNotPresent() {
+ Properties props = new Properties();
+ props.put("sonar.projectVersion", "1.0");
+
+ // should be set
+ SonarProjectBuilder.setProjectKeyIfNotDefined(props, "foo");
+ assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+
+ // but not this 2nd time
+ SonarProjectBuilder.setProjectKeyIfNotDefined(props, "bar");
+ assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+ }
+
}