From: Evgeny Mandrikov Date: Thu, 3 Feb 2011 05:11:51 +0000 (+0000) Subject: SONAR-RUNNER X-Git-Tag: 2.5-rc1~357 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8f0aa10d500cb33f0c3d31537f3be8001011401c;p=sonar-scanner-cli.git SONAR-RUNNER --- diff --git a/src/it/example/pom.xml b/src/it/example/pom.xml new file mode 100644 index 0000000..8789ee4 --- /dev/null +++ b/src/it/example/pom.xml @@ -0,0 +1,10 @@ + + + 4.0.0 + + org.example + example + 0.1-SNAPSHOT + + diff --git a/src/it/example/sonar-project.properties b/src/it/example/sonar-project.properties index f02c998..5ebbca2 100644 --- a/src/it/example/sonar-project.properties +++ b/src/it/example/sonar-project.properties @@ -1,10 +1,3 @@ sonar.projectKey=my:project sonar.projectName=My project sonar.projectVersion=1.0 - -#project.sources=src/main/java -#project.sources=src/main/resources -#project.binaries=output/classes -#project.language=java -#sonar.dynamicAnalysis=reuseReports -#sonar.surefire.reports=output/junit/ diff --git a/src/it/example/src/main/java/HelloWorld.java b/src/it/example/src/main/java/HelloWorld.java new file mode 100644 index 0000000..4d4bbb8 --- /dev/null +++ b/src/it/example/src/main/java/HelloWorld.java @@ -0,0 +1,5 @@ +class HelloWorld { + public void sayHello() { + System.out.println("Hello world"); + } +} diff --git a/src/it/example/src/org/sonar/cli/example/HelloWorld.java b/src/it/example/src/org/sonar/cli/example/HelloWorld.java deleted file mode 100644 index beed553..0000000 --- a/src/it/example/src/org/sonar/cli/example/HelloWorld.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sonar.cli.example; - -class HelloWorld { - public void sayHello() { - System.out.println("Hello world"); - } -} diff --git a/src/main/assembly/bin/sonar-runner.sh b/src/main/assembly/bin/sonar-runner.sh index abad57b..1012948 100755 --- a/src/main/assembly/bin/sonar-runner.sh +++ b/src/main/assembly/bin/sonar-runner.sh @@ -17,9 +17,9 @@ JAVACMD="`which java`" JAVACLASSPATH="${SONAR_RUNNER_HOME}"/lib/sonar-runner.jar JAVACLASSPATH=$JAVACLASSPATH:"${SONAR_RUNNER_HOME}"/lib/sonar-batch-bootstrapper.jar -echo "Info: Using sonar-runner at $SONAR_RUNNER_HOME" -echo "Info: Using java at $JAVACMD" -echo "Info: Using classpath $JAVACLASSPATH" +#echo "Info: Using sonar-runner at $SONAR_RUNNER_HOME" +#echo "Info: Using java at $JAVACMD" +#echo "Info: Using classpath $JAVACLASSPATH" exec "$JAVACMD" \ -classpath $JAVACLASSPATH \ diff --git a/src/main/assembly/conf/sonar-runner.properties b/src/main/assembly/conf/sonar-runner.properties index 58e14fd..5f01622 100644 --- a/src/main/assembly/conf/sonar-runner.properties +++ b/src/main/assembly/conf/sonar-runner.properties @@ -1,3 +1,11 @@ +#----- Default directory layout +sources=src/main/java +tests=src/test/java +binaries=target/classes + +sonar.sourceEncoding=UTF-8 + +#----- Default Sonar server #sonar.host.url=http://localhost:9000 #----- PostgreSQL diff --git a/src/main/java/org/sonar/runner/Launcher.java b/src/main/java/org/sonar/runner/Launcher.java index 04deaab..72f89a0 100644 --- a/src/main/java/org/sonar/runner/Launcher.java +++ b/src/main/java/org/sonar/runner/Launcher.java @@ -25,6 +25,7 @@ import ch.qos.logback.classic.joran.JoranConfigurator; 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; @@ -65,8 +66,7 @@ public class Launcher { 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); @@ -82,14 +82,25 @@ public class Launcher { 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()); diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index eb38033..ba4c63e 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -30,29 +30,41 @@ import java.lang.reflect.Constructor; 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 cmdProps = parseArguments(args); + new Main().execute(cmdProps); } - public Main(String[] args) { - this.args = args; + public void execute(Map 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 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()); } /** @@ -79,12 +91,16 @@ public class Main { 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); @@ -121,16 +137,6 @@ public class Main { } } - 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); @@ -176,7 +182,55 @@ public class Main { } } - private void log(String message) { - System.out.println(message); + private static Map parseArguments(String[] args) { + HashMap cmdProps = new HashMap(); + 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 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 } }