diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-03 05:11:51 +0000 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-03 05:11:51 +0000 |
commit | 8f0aa10d500cb33f0c3d31537f3be8001011401c (patch) | |
tree | edd1a17f8455d483fe27114c54a056766566f3cb /src | |
parent | a0c622bc8666ccb0983fce374dfd01f03ca37703 (diff) | |
download | sonar-scanner-cli-8f0aa10d500cb33f0c3d31537f3be8001011401c.tar.gz sonar-scanner-cli-8f0aa10d500cb33f0c3d31537f3be8001011401c.zip |
SONAR-RUNNER
Diffstat (limited to 'src')
-rw-r--r-- | src/it/example/pom.xml | 10 | ||||
-rw-r--r-- | src/it/example/sonar-project.properties | 7 | ||||
-rw-r--r-- | src/it/example/src/main/java/HelloWorld.java (renamed from src/it/example/src/org/sonar/cli/example/HelloWorld.java) | 2 | ||||
-rwxr-xr-x | src/main/assembly/bin/sonar-runner.sh | 6 | ||||
-rw-r--r-- | src/main/assembly/conf/sonar-runner.properties | 8 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Launcher.java | 25 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Main.java | 100 |
7 files changed, 116 insertions, 42 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.example</groupId> + <artifactId>example</artifactId> + <version>0.1-SNAPSHOT</version> + +</project> 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/org/sonar/cli/example/HelloWorld.java b/src/it/example/src/main/java/HelloWorld.java index beed553..4d4bbb8 100644 --- a/src/it/example/src/org/sonar/cli/example/HelloWorld.java +++ b/src/it/example/src/main/java/HelloWorld.java @@ -1,5 +1,3 @@ -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<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()); } /** @@ -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<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 } } |