import ch.qos.logback.core.joran.spi.JoranException;
import org.apache.commons.configuration.*;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.platform.Environment;
import org.sonar.api.utils.SonarException;
jc.setContext(context);
context.reset();
InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
- // System.setProperty("ROOT_LOGGER_LEVEL", getLog().isDebugEnabled() ? "DEBUG" : "INFO");
- System.setProperty("ROOT_LOGGER_LEVEL", "INFO");
+ System.setProperty("ROOT_LOGGER_LEVEL", task.isDebugEnabled() ? "DEBUG" : "INFO");
try {
jc.doConfigure(input);
File baseDir = task.getProjectDir();
Properties properties = task.getProperties();
ProjectDefinition definition = new ProjectDefinition(baseDir, task.getWorkDir(), properties);
- // TODO for some reason it can't be relative
- definition.addSourceDir(new File(baseDir, "src").getAbsolutePath()); // TODO hard-coded value
- // TODO definition.addTestDir(path);
- // TODO definition.addBinaryDir(path);
- // TODO definition.addLibrary(path);
+ for (String dir : getList(properties, "sources")) {
+ definition.addSourceDir(dir);
+ }
+ for (String dir : getList(properties, "tests")) {
+ definition.addTestDir(dir);
+ }
+ for (String dir : getList(properties, "binaries")) {
+ definition.addBinaryDir(dir);
+ }
+ for (String file : getList(properties, "libraries")) {
+ definition.addLibrary(file);
+ }
return definition;
}
+ private String[] getList(Properties properties, String key) {
+ return StringUtils.split(properties.getProperty(key, ""), ',');
+ }
+
private Configuration getInitialConfiguration(ProjectDefinition project) {
CompositeConfiguration configuration = new CompositeConfiguration();
configuration.addConfiguration(new SystemConfiguration());
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
public class Main {
- private String[] args;
+ private static boolean debug = false;
private File workDir;
-
- private Properties properties;
-
+ private Properties properties = new Properties();
private Bootstrapper bootstrapper;
- public static void main(String[] args) throws Exception {
- new Main(args).execute();
+ public static void main(String[] args) {
+ log("Sonar Standalone Runner version: " + getRunnerVersion());
+ Map<String, String> cmdProps = parseArguments(args);
+ new Main().execute(cmdProps);
}
- public Main(String[] args) {
- this.args = args;
+ public void execute(Map<String, String> cmdProps) {
String home = System.getProperty("runner.home");
- properties = new Properties();
+ // Load global configuration
loadProperties(new File(home + "/conf/sonar-runner.properties"));
+ // Load project configuration
loadProperties(new File(getProjectDir(), "sonar-project.properties"));
- // TODO load properties from command-line
+ // Load properties from command-line
+ for (Map.Entry<String, String> entry : cmdProps.entrySet()) {
+ properties.setProperty(entry.getKey(), entry.getValue());
+ }
+
+ String serverUrl = properties.getProperty("sonar.host.url", "http://localhost:9000");
+ log("Sonar server: " + serverUrl);
+ log("Sonar work directory: " + getWorkDir().getAbsolutePath());
+ bootstrapper = new Bootstrapper(serverUrl, getWorkDir());
+ checkSonarVersion();
+ delegateExecution(createClassLoader());
}
/**
return properties;
}
+ public boolean isDebugEnabled() {
+ return debug;
+ }
+
/**
* Loads {@link Launcher} from specified {@link BootstrapClassLoader} and passes control to it.
*
* @see Launcher#execute()
*/
- private void delegateExecution(BootstrapClassLoader sonarClassLoader) throws Exception {
+ private void delegateExecution(BootstrapClassLoader sonarClassLoader) {
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(sonarClassLoader);
}
}
- public void execute() throws Exception {
- String serverUrl = properties.getProperty("sonar.host.url", "http://localhost:9000");
- log("Sonar Standalone Runner version: " + getRunnerVersion());
- log("Sonar server: " + serverUrl);
- log("Sonar work directory: " + getWorkDir().getAbsolutePath());
- bootstrapper = new Bootstrapper(serverUrl, getWorkDir());
- checkSonarVersion();
- delegateExecution(createClassLoader());
- }
-
private void checkSonarVersion() {
String serverVersion = bootstrapper.getServerVersion();
log("Sonar version: " + serverVersion);
}
}
- private void log(String message) {
- System.out.println(message);
+ private static Map<String, String> parseArguments(String[] args) {
+ HashMap<String, String> cmdProps = new HashMap<String, String>();
+ for (int i = 0; i < args.length; i++) {
+ String arg = args[i];
+ if ("-h".equals(arg) || "--help".equals(arg)) {
+ printUsage();
+ } else if ("-X".equals(arg) || "--debug".equals(arg)) {
+ debug = true;
+ } else if ("-D".equals(arg) || "--define".equals(arg)) {
+ i++;
+ if (i >= args.length) {
+ printError("Missing argument for option --define");
+ }
+ arg = args[i];
+ final String key, value;
+ int j = arg.indexOf('=');
+ if (j == -1) {
+ key = arg;
+ value = "true";
+ } else {
+ key = arg.substring(0, j);
+ value = arg.substring(j + 1);
+ }
+ cmdProps.put(key, value);
+ } else {
+ printError("Unrecognized option: " + arg);
+ }
+ }
+ return cmdProps;
+ }
+
+ private static void printUsage() {
+ log("");
+ log("usage: sonar-runner [options]");
+ log("");
+ log("Options:");
+ log(" -h,--help Display help information");
+ log(" -X,--debug Produce execution debug output");
+ log(" -D,--define <arg> Define property");
+ System.exit(0); // NOSONAR
+ }
+
+ private static void printError(String message) {
+ log("");
+ log(message);
+ printUsage();
+ }
+
+ private static void log(String message) {
+ System.out.println(message); // NOSONAR
}
}