diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-25 16:20:08 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-25 16:20:08 +0300 |
commit | 0effb8e5016195ace8a295d44eaa245e265ff0b7 (patch) | |
tree | 772e69bd9674030a648d92e8802698ee63c774e0 /sonar-batch/src | |
parent | 10b9b9914cb775cb892036d4ae895c47ff602946 (diff) | |
download | sonarqube-0effb8e5016195ace8a295d44eaa245e265ff0b7.tar.gz sonarqube-0effb8e5016195ace8a295d44eaa245e265ff0b7.zip |
Add new property "sonar.projectBinaries"
Diffstat (limited to 'sonar-batch/src')
4 files changed, 235 insertions, 123 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/InMemoryPomCreator.java b/sonar-batch/src/main/java/org/sonar/batch/InMemoryPomCreator.java new file mode 100644 index 00000000000..af82ba42faf --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/InMemoryPomCreator.java @@ -0,0 +1,111 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch; + +import org.apache.commons.lang.StringUtils; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.model.Reporting; +import org.apache.maven.project.MavenProject; +import org.sonar.api.CoreProperties; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.bootstrapper.ProjectDefinition; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +/** + * This is a dirty hack for for non-Maven environments, + * which allows to create {@link MavenProject} based on {@link ProjectDefinition}. + */ +public class InMemoryPomCreator { + + private ProjectDefinition project; + + public InMemoryPomCreator(ProjectDefinition project) { + this.project = project; + } + + public MavenProject create() { + Properties properties = project.getProperties(); + final String[] binaries = StringUtils.split(properties.getProperty("sonar.projectBinaries", ""), ','); + + final MavenProject pom = new MavenProject() { + /** + * This allows to specify base directory without specifying location of a pom.xml + */ + @Override + public File getBasedir() { + return project.getBaseDir(); + }; + + /** + * This allows to specify project binaries. + */ + @Override + public List<String> getCompileClasspathElements() throws DependencyResolutionRequiredException { + return Arrays.asList(binaries); + } + }; + + String key = getPropertyOrDie(properties, CoreProperties.PROJECT_KEY_PROPERTY); + String[] keys = key.split(":"); + pom.setGroupId(keys[0]); + pom.setArtifactId(keys[1]); + pom.setVersion(getPropertyOrDie(properties, CoreProperties.PROJECT_VERSION_PROPERTY)); + + pom.getModel().setProperties(properties); + + pom.setArtifacts(Collections.EMPTY_SET); + + // Configure fake directories + String buildDirectory = getPropertyOrDie(properties, "project.build.directory"); + pom.getBuild().setDirectory(buildDirectory); + String buildOutputDirectory = properties.getProperty("project.build.outputDirectory", buildDirectory + "/classes"); + pom.getBuild().setOutputDirectory(buildOutputDirectory); + Reporting reporting = new Reporting(); + String reportingOutputDirectory = properties.getProperty("project.reporting.outputDirectory", buildDirectory + "/site"); + reporting.setOutputDirectory(reportingOutputDirectory); + pom.setReporting(reporting); + + // Configure source directories + for (String dir : project.getSourceDirs()) { + pom.addCompileSourceRoot(dir); + } + + // Configure test directories + for (String dir : project.getTestDirs()) { + pom.addTestCompileSourceRoot(dir); + } + + return pom; + } + + private static String getPropertyOrDie(Properties properties, String key) { + String value = properties.getProperty(key); + if (StringUtils.isBlank(value)) { + throw new SonarException("Property '" + key + "' must be specified"); + } + return value; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java index d0c6f118d3b..a0601ae3060 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java @@ -21,14 +21,10 @@ package org.sonar.batch; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; -import org.apache.maven.model.Reporting; import org.apache.maven.project.MavenProject; import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.resources.Project; -import org.sonar.api.utils.SonarException; import org.sonar.batch.bootstrapper.ProjectDefinition; import org.sonar.batch.bootstrapper.Reactor; @@ -58,62 +54,11 @@ public class ProjectTree { List<ProjectDefinition> sonarProjects = sonarReactor.getSortedProjects(); List<MavenProject> mavenProjects = Lists.newArrayList(); for (ProjectDefinition project : sonarProjects) { - mavenProjects.add(createInMemoryPom(project)); + mavenProjects.add(new InMemoryPomCreator(project).create()); } return new MavenReactor(mavenProjects); } - static MavenProject createInMemoryPom(final ProjectDefinition project) { - MavenProject pom = new MavenProject() { - /** - * This allows to specify base directory without specifying location of a pom.xml - */ - public File getBasedir() { - return project.getBaseDir(); - }; - }; - - Properties properties = project.getProperties(); - - String key = getPropertyOrDie(properties, CoreProperties.PROJECT_KEY_PROPERTY); - String[] keys = key.split(":"); - pom.setGroupId(keys[0]); - pom.setArtifactId(keys[1]); - pom.setVersion(getPropertyOrDie(properties, CoreProperties.PROJECT_VERSION_PROPERTY)); - - pom.getModel().setProperties(properties); - - pom.setArtifacts(Collections.EMPTY_SET); - - // Configure fake directories - String buildDirectory = getPropertyOrDie(properties, "project.build.directory"); - pom.getBuild().setDirectory(buildDirectory); - pom.getBuild().setOutputDirectory(buildDirectory + "/classes"); // TODO hard-coded value - Reporting reporting = new Reporting(); - reporting.setOutputDirectory(buildDirectory + "/site"); // TODO hard-coded value - pom.setReporting(reporting); - - // Configure source directories - for (String dir : project.getSourceDirs()) { - pom.addCompileSourceRoot(dir); - } - - // Configure test directories - for (String dir : project.getTestDirs()) { - pom.addTestCompileSourceRoot(dir); - } - - return pom; - } - - private static String getPropertyOrDie(Properties properties, String key) { - String value = properties.getProperty(key); - if (StringUtils.isBlank(value)) { - throw new SonarException("Property '" + key + "' must be specified"); - } - return value; - } - /** * for unit tests */ diff --git a/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomCreatorTest.java b/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomCreatorTest.java new file mode 100644 index 00000000000..b9336e43deb --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomCreatorTest.java @@ -0,0 +1,123 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch; + +import org.apache.maven.project.MavenProject; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.bootstrapper.ProjectDefinition; + +import java.io.File; +import java.util.Properties; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class InMemoryPomCreatorTest { + + private Properties properties; + private ProjectDefinition project; + + @Before + public void setUp() { + properties = new Properties(); + File baseDir = new File("."); + project = new ProjectDefinition(baseDir, properties); + } + + @Test + public void shouldCreate() throws Exception { + createRequiredProperties("org.example:example", "1.0-SNAPSHOT", "target"); + properties.setProperty("sonar.projectBinaries", "junit.jar"); + + project.addSourceDir("src"); + project.addTestDir("test"); + + MavenProject pom = create(); + + assertThat(pom.getBasedir(), is(project.getBaseDir())); + assertThat(pom.getGroupId(), is("org.example")); + assertThat(pom.getArtifactId(), is("example")); + assertThat(pom.getProperties(), is(project.getProperties())); + assertThat(pom.getBuild().getDirectory(), is("target")); + assertThat(pom.getBuild().getOutputDirectory(), is("target/classes")); + assertThat(pom.getReporting().getOutputDirectory(), is("target/site")); + + assertThat(pom.getCompileSourceRoots().size(), is(1)); + assertThat((String) pom.getCompileSourceRoots().get(0), is("src")); + + assertThat(pom.getTestCompileSourceRoots().size(), is(1)); + assertThat((String) pom.getTestCompileSourceRoots().get(0), is("test")); + + assertThat(pom.getCompileClasspathElements().size(), is(1)); + assertThat((String) pom.getCompileClasspathElements().get(0), is("junit.jar")); + } + + @Test + public void nonStandardDirectoriesLayout() { + createRequiredProperties("org.example:example", "1.0-SNAPSHOT", "build"); + properties.setProperty("project.build.outputDirectory", "bin"); + properties.setProperty("project.reporting.outputDirectory", "build/reports"); + + MavenProject pom = create(); + + assertThat(pom.getBuild().getDirectory(), is("build")); + assertThat(pom.getBuild().getOutputDirectory(), is("bin")); + assertThat(pom.getReporting().getOutputDirectory(), is("build/reports")); + } + + @Test + public void shouldNotFailIfNoBinaries() throws Exception { + createRequiredProperties("org.example:example", "1.0-SNAPSHOT", "build"); + + MavenProject pom = create(); + assertThat(pom.getCompileClasspathElements().size(), is(0)); + } + + private void createRequiredProperties(String key, String version, String buildDirectory) { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, key); + properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, version); + properties.setProperty("project.build.directory", buildDirectory); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenKeyNotSpecified() { + create(); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenVersionNotSpecified() { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); + create(); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenBuildDirectoryNotSpecified() { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); + properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "0.1"); + create(); + } + + private MavenProject create() { + return new InMemoryPomCreator(project).create(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java b/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java deleted file mode 100644 index 69d5a32826d..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.sonar.batch; - -import org.apache.maven.project.MavenProject; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.CoreProperties; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.bootstrapper.ProjectDefinition; - -import java.io.File; -import java.util.Properties; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -public class InMemoryPomTest { - - private Properties properties; - private ProjectDefinition project; - - @Before - public void setUp() { - properties = new Properties(); - File baseDir = new File("."); - project = new ProjectDefinition(baseDir, properties); - } - - @Test - public void shouldCreate() { - properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); - properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0-SNAPSHOT"); - properties.setProperty("project.build.directory", "build"); - - project.addSourceDir("src"); - project.addTestDir("test"); - - MavenProject pom = ProjectTree.createInMemoryPom(project); - - assertThat(pom.getBasedir(), is(project.getBaseDir())); - assertThat(pom.getGroupId(), is("org.example")); - assertThat(pom.getArtifactId(), is("example")); - assertThat(pom.getProperties(), is(project.getProperties())); - assertThat(pom.getBuild().getDirectory(), is("build")); - - assertThat(pom.getCompileSourceRoots().size(), is(1)); - assertThat(pom.getTestCompileSourceRoots().size(), is(1)); - } - - @Test(expected = SonarException.class) - public void shouldFailWhenKeyNotSpecified() { - ProjectTree.createInMemoryPom(project); - } - - @Test(expected = SonarException.class) - public void shouldFailWhenVersionNotSpecified() { - properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); - ProjectTree.createInMemoryPom(project); - } - - @Test(expected = SonarException.class) - public void shouldFailWhenBuildDirectoryNotSpecified() { - properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); - properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "0.1"); - ProjectTree.createInMemoryPom(project); - } - -} |