--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.File;
+
+class Dirs {
+
+ void init(Runner runner) {
+ boolean onProject = ScanProperties.SCAN_TASK.equals(runner.property(RunnerProperties.TASK, null));
+ if (onProject) {
+ initProjectDirs(runner);
+ } else {
+ initTaskDirs(runner);
+ }
+ }
+
+ private void initProjectDirs(Runner runner) {
+ String path = runner.property(ScanProperties.PROJECT_BASEDIR, ".");
+ File projectDir = new File(path);
+ if (!projectDir.isDirectory()) {
+ throw new IllegalStateException("Project home must be an existing directory: " + path);
+ }
+ runner.setProperty(ScanProperties.PROJECT_BASEDIR, projectDir.getAbsolutePath());
+
+ File workDir;
+ path = runner.property(RunnerProperties.WORK_DIR, "");
+ if ("".equals(path.trim())) {
+ workDir = new File(projectDir, ".sonar");
+
+ } else {
+ workDir = new File(path);
+ if (!workDir.isAbsolute()) {
+ workDir = new File(projectDir, path);
+ }
+ }
+ FileUtils.deleteQuietly(workDir);
+ runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath());
+ }
+
+ /**
+ * Non-scan task
+ */
+ private void initTaskDirs(Runner runner) {
+ String path = runner.property(RunnerProperties.WORK_DIR, ".");
+ File workDir = new File(path);
+ runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath());
+ }
+}
*/
package org.sonar.runner.api;
-import org.sonar.runner.impl.Constants;
-import org.sonar.runner.impl.Logs;
+import org.sonar.runner.impl.InternalProperties;
import javax.annotation.Nullable;
-
-import java.nio.charset.Charset;
-import java.util.Locale;
import java.util.Properties;
/**
private final Properties properties = new Properties();
protected Runner() {
- initProperties();
- }
-
- private void initProperties() {
- // default values
- properties.put(Constants.HOST_URL, "http://localhost:9000");
- properties.put(Constants.TASK, "scan");
- properties.put(Constants.RUNNER_APP, "SonarRunner");
- properties.put(Constants.RUNNER_APP_VERSION, RunnerVersion.version());
}
public Properties properties() {
/**
* Declare Sonar properties, for example sonar.projectKey=>foo.
+ *
+ * @see #setProperty(String, String)
*/
public T addProperties(Properties p) {
properties.putAll(p);
return (T) this;
}
+ /**
+ * Declare a Sonar property.
+ *
+ * @see RunnerProperties
+ * @see ScanProperties
+ */
public T setProperty(String key, String value) {
properties.setProperty(key, value);
return (T) this;
* User-agent used in the HTTP requests to the Sonar server
*/
public T setApp(String app, String version) {
- setProperty(Constants.RUNNER_APP, app);
- setProperty(Constants.RUNNER_APP_VERSION, version);
+ setProperty(InternalProperties.RUNNER_APP, app);
+ setProperty(InternalProperties.RUNNER_APP_VERSION, version);
return (T) this;
}
public String app() {
- return property(Constants.RUNNER_APP, null);
+ return property(InternalProperties.RUNNER_APP, null);
}
public String appVersion() {
- return property(Constants.RUNNER_APP_VERSION, null);
+ return property(InternalProperties.RUNNER_APP_VERSION, null);
}
public void execute() {
- initSourceEncoding();
+ initDefaultValues();
+ new SourceEncoding().init(this);
+ new Dirs().init(this);
doExecute();
}
- private void initSourceEncoding() {
- String sourceEncoding = property(Constants.SOURCE_ENCODING, null);
- boolean platformDependent = false;
- if (sourceEncoding == null || sourceEncoding.equals("")) {
- sourceEncoding = Charset.defaultCharset().name();
- platformDependent = true;
- setProperty(Constants.SOURCE_ENCODING, sourceEncoding);
+ protected abstract void doExecute();
+
+ private void initDefaultValues() {
+ setDefaultValue(RunnerProperties.HOST_URL, "http://localhost:9000");
+ setDefaultValue(RunnerProperties.TASK, "scan");
+ setDefaultValue(InternalProperties.RUNNER_APP, "SonarRunner");
+ setDefaultValue(InternalProperties.RUNNER_APP_VERSION, RunnerVersion.version());
+ }
+
+ private void setDefaultValue(String key, String value) {
+ if (!properties.containsKey(key)) {
+ setProperty(key, value);
}
- Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
- + (platformDependent ? " (analysis is platform dependent)" : ""));
}
- protected abstract void doExecute();
}
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+/**
+ * Mostly used properties that can be injected in {@link Runner#setProperty(String, String)}.
+ * See <a href="http://docs.codehaus.org/pages/viewinfo.action?pageId=194314339">documentation</a> for more properties.
+ *
+ * @since 2.2
+ */
+public interface RunnerProperties {
+ /**
+ * HTTP URL of Sonar server, "http://localhost:9000" by default
+ */
+ String HOST_URL = "sonar.host.url";
+
+ /**
+ * Task to execute, "scan" by default
+ */
+ String TASK = "sonar.task";
+
+ /**
+ * Encoding of source and test files. By default it's the platform encoding.
+ */
+ String SOURCE_ENCODING = "sonar.sourceEncoding";
+
+ /**
+ * Working directory containing generated reports and temporary data.
+ */
+ String WORK_DIR = "sonar.working.directory";
+}
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+/**
+ * Most commonly used properties of the task "scan". These properties are injected in {@link Runner#setProperty(String, String)}.
+ * See <a href="http://docs.codehaus.org/pages/viewinfo.action?pageId=194314339">documentation</a> for more properties.
+ *
+ * @since 2.2
+ */
+public interface ScanProperties {
+
+ /**
+ * Default task
+ * @see RunnerProperties#TASK
+ */
+ String SCAN_TASK = "scan";
+
+ /**
+ * Required project key
+ */
+ String PROJECT_KEY = "sonar.projectKey";
+
+
+ String PROJECT_NAME = "sonar.projectName";
+
+ String PROJECT_VERSION = "sonar.projectVersion";
+
+ /**
+ * Optional description
+ */
+ String PROJECT_DESCRIPTION = "sonar.projectDescription";
+
+ /**
+ * Required paths to source directories, separated by commas, for example: "srcDir1,srcDir2"
+ */
+ String PROJECT_SOURCE_DIRS = "sonar.sources";
+
+ /**
+ * Optional paths to test directories, separated by commas, for example: "testDir1,testDir2"
+ */
+ String PROJECT_TEST_DIRS = "sonar.tests";
+
+ /**
+ * Optional paths to binaries, for example to declare the directory of Java bytecode. Example : "binDir"
+ */
+ String PROJECT_BINARY_DIRS = "sonar.binaries";
+
+ /**
+ * Optional comma-separated list of paths to libraries. Example : <code>path/to/library/*.jar,path/to/specific/library/myLibrary.jar,parent/*.jar</code>
+ */
+ String PROJECT_LIBRARIES = "sonar.libraries";
+
+ String PROJECT_LANGUAGE = "sonar.language";
+
+ /**
+ * It becomes quickly necessary to input historical data and to highlight some events. It is possible by going for example in a subversion tag
+ * and use this property. Format is yyyy-MM-dd, for example 2010-12-25.
+ */
+ String PROJECT_DATE = "sonar.projectDate";
+
+ /**
+ * Property used to specify the base directory of the project to analyse. Default is ".".
+ */
+ String PROJECT_BASEDIR = "sonar.projectBaseDir";
+}
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+import org.sonar.runner.impl.Logs;
+
+import java.nio.charset.Charset;
+import java.util.Locale;
+
+class SourceEncoding {
+
+ void init(Runner runner) {
+ String sourceEncoding = runner.property(RunnerProperties.SOURCE_ENCODING, "");
+ boolean platformDependent = false;
+ if ("".equals(sourceEncoding)) {
+ sourceEncoding = Charset.defaultCharset().name();
+ platformDependent = true;
+ runner.setProperty(RunnerProperties.SOURCE_ENCODING, sourceEncoding);
+ }
+ Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
+ + (platformDependent ? " (analysis is platform dependent)" : ""));
+ }
+
+}
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+import org.apache.commons.io.FilenameUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DirsTest {
+
+ Runner runner = new SimpleRunner();
+ Dirs dirs = new Dirs();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void should_init_default_task_work_dir() throws Exception {
+ runner.setProperty("sonar.task", "views");
+ dirs.init(runner);
+
+ File workDir = new File(runner.property("sonar.working.directory", null));
+ assertThat(workDir).isNotNull().isDirectory();
+ assertThat(workDir.getCanonicalPath()).isEqualTo(new File(".").getCanonicalPath());
+ }
+
+ @Test
+ public void should_use_parameterized_task_work_dir() throws Exception {
+ runner.setProperty("sonar.task", "views");
+ runner.setProperty("sonar.working.directory", "generated/reports");
+ dirs.init(runner);
+
+ File workDir = new File(runner.property("sonar.working.directory", null));
+ assertThat(workDir).isNotNull();
+ assertThat(FilenameUtils.separatorsToUnix(workDir.getCanonicalPath())).contains("generated/reports");
+ }
+
+ @Test
+ public void should_init_default_project_dirs() throws Exception {
+ runner.setProperty("sonar.task", "scan");
+ dirs.init(runner);
+
+
+ File projectDir = new File(runner.property("sonar.projectBaseDir", null));
+ File workDir = new File(runner.property("sonar.working.directory", null));
+
+ assertThat(projectDir).isNotNull().isDirectory();
+ assertThat(workDir).isNotNull();
+
+ assertThat(projectDir.getCanonicalPath()).isEqualTo(new File(".").getCanonicalPath());
+ assertThat(workDir.getName()).isEqualTo(".sonar");
+ assertThat(workDir.getParentFile()).isEqualTo(projectDir);
+ }
+
+ @Test
+ public void should_set_relative_path_to_project_work_dir() throws Exception {
+ File initialProjectDir = temp.newFolder();
+ runner.setProperty("sonar.task", "scan");
+ runner.setProperty("sonar.working.directory", "relative/path");
+ runner.setProperty("sonar.projectBaseDir", initialProjectDir.getAbsolutePath());
+ dirs.init(runner);
+
+
+ File projectDir = new File(runner.property("sonar.projectBaseDir", null));
+ File workDir = new File(runner.property("sonar.working.directory", null));
+
+ assertThat(projectDir).isNotNull().isDirectory();
+ assertThat(projectDir.getCanonicalPath()).isEqualTo(initialProjectDir.getCanonicalPath());
+
+ assertThat(workDir).isNotNull();
+ assertThat(workDir.getCanonicalPath()).isEqualTo(new File(projectDir, "relative/path").getCanonicalPath());
+ }
+}
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.sonar.runner.impl.BatchLauncher;
-import org.sonar.runner.impl.Constants;
+import org.sonar.runner.impl.InternalProperties;
import java.util.List;
import java.util.Properties;
@Test
public void should_set_unmasked_packages() {
EmbeddedRunner runner = EmbeddedRunner.create();
- assertThat(runner.property(Constants.RUNNER_UNMASKED_PACKAGES, null)).isNull();
+ assertThat(runner.property(InternalProperties.RUNNER_UNMASKED_PACKAGES, null)).isNull();
runner = EmbeddedRunner.create().setUnmaskedPackages("org.apache.ant", "org.ant");
- assertThat(runner.property(Constants.RUNNER_UNMASKED_PACKAGES, null)).isEqualTo("org.apache.ant,org.ant");
+ assertThat(runner.property(InternalProperties.RUNNER_UNMASKED_PACKAGES, null)).isEqualTo("org.apache.ant,org.ant");
}
@Test
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+class SimpleRunner extends Runner {
+ @Override
+ protected void doExecute() {
+ }
+}
--- /dev/null
+/*
+ * Sonar Runner - API
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.api;
+
+import org.junit.Test;
+
+import java.nio.charset.Charset;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class SourceEncodingTest {
+
+ SourceEncoding encoding = new SourceEncoding();
+ Runner runner = new SimpleRunner();
+
+ @Test
+ public void should_set_default_platform_encoding() throws Exception {
+ encoding.init(runner);
+ assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name());
+ }
+
+ @Test
+ public void should_use_parameterized_encoding() throws Exception {
+ runner.setProperty("sonar.sourceEncoding", "THE_ISO_1234");
+ encoding.init(runner);
+ assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234");
+ }
+
+}
*/
package org.sonar.runner;
-import org.sonar.runner.impl.Constants;
+import org.sonar.runner.api.RunnerProperties;
import org.sonar.runner.impl.Logs;
import java.util.Properties;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (i == 0 && !arg.startsWith("-")) {
- props.setProperty(Constants.TASK, arg);
+ props.setProperty(RunnerProperties.TASK, arg);
} else if ("-h".equals(arg) || "--help".equals(arg)) {
printUsage();
if (serverVersion.is35Compatible()) {
jarFiles = new Jars35(fileDownloader, new JarExtractor()).download();
} else if (serverVersion.is30Compatible()) {
- String workDir = properties.getProperty(Constants.RUNNER_WORK_DIR);
+ String workDir = properties.getProperty("sonar.working.directory");
jarFiles = new Jars30(fileDownloader).download(new File(workDir), new JarExtractor());
} else {
throw new IllegalStateException("Sonar " + serverVersion.version()
+ " is not supported. Please upgrade Sonar to version 3.0 or more.");
}
- String unmaskedPackages = properties.getProperty(Constants.RUNNER_UNMASKED_PACKAGES, "");
+ String unmaskedPackages = properties.getProperty(InternalProperties.RUNNER_UNMASKED_PACKAGES, "");
IsolatedClassloader classloader = new IsolatedClassloader(getClass().getClassLoader(), unmaskedPackages.split(":"));
classloader.addFiles(jarFiles);
delegateExecution(classloader, properties, extensions);
try {
props.load(input);
// just to be clean, do not forward properties that do not make sense in fork mode
- props.remove(Constants.RUNNER_UNMASKED_PACKAGES);
+ props.remove(InternalProperties.RUNNER_UNMASKED_PACKAGES);
} finally {
IOUtils.closeQuietly(input);
+++ /dev/null
-/*
- * Sonar Runner - Implementation
- * Copyright (C) 2011 SonarSource
- * dev@sonar.codehaus.org
- *
- * 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 02
- */
-package org.sonar.runner.impl;
-
-public interface Constants {
- String HOST_URL = "sonar.host.url";
- String TASK = "sonar.task";
- String SOURCE_ENCODING = "sonar.sourceEncoding";
-
- String RUNNER_APP = "sonarRunner.app";
- String RUNNER_APP_VERSION = "sonarRunner.appVersion";
- String RUNNER_UNMASKED_PACKAGES = "sonarRunner.unmaskedPackages";
- String RUNNER_WORK_DIR = "sonarRunner.workDir";
-}
}
static FileDownloader create(Properties properties) {
- String serverUrl = properties.getProperty(Constants.HOST_URL);
- String app = properties.getProperty(Constants.RUNNER_APP);
- String appVersion = properties.getProperty(Constants.RUNNER_APP_VERSION);
+ String serverUrl = properties.getProperty("sonar.host.url");
+ String app = properties.getProperty(InternalProperties.RUNNER_APP);
+ String appVersion = properties.getProperty(InternalProperties.RUNNER_APP_VERSION);
return new FileDownloader(serverUrl, app, appVersion);
}
--- /dev/null
+/*
+ * Sonar Runner - Implementation
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * 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 02
+ */
+package org.sonar.runner.impl;
+
+public interface InternalProperties {
+ String RUNNER_APP = "sonarRunner.app";
+ String RUNNER_APP_VERSION = "sonarRunner.appVersion";
+ String RUNNER_UNMASKED_PACKAGES = "sonarRunner.unmaskedPackages";
+}