Browse Source

Use CoreProperties for in-memory POM construction

tags/2.6
Evgeny Mandrikov 13 years ago
parent
commit
bd940b0436

+ 27
- 9
sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java View File

@@ -21,11 +21,14 @@ 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;

@@ -60,27 +63,34 @@ public class ProjectTree {
return new MavenReactor(mavenProjects);
}

private static MavenProject createInMemoryPom(ProjectDefinition project) {
MavenProject pom = new MavenProject();
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();
};
};

String key = project.getProperties().getProperty("project.key"); // TODO constant
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("0.1-SNAPSHOT"); // TODO hard-coded value
pom.setVersion(getPropertyOrDie(properties, CoreProperties.PROJECT_VERSION_PROPERTY));

pom.getModel().setProperties(project.getProperties());
pom.getModel().setProperties(properties);

pom.setArtifacts(Collections.EMPTY_SET);

// Configure fake directories
String buildDirectory = project.getProperties().getProperty("project.build.directory");
File sonarDir = new File(buildDirectory, "sonar");
pom.setFile(new File(sonarDir, "fake-pom.xml"));
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 + "/target/site"); // TODO hard-coded value
reporting.setOutputDirectory(buildDirectory + "/site"); // TODO hard-coded value
pom.setReporting(reporting);

// Configure source directories
@@ -96,6 +106,14 @@ public class ProjectTree {
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
*/

+ 67
- 0
sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java View File

@@ -0,0 +1,67 @@
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);
}

}

+ 5
- 0
sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java View File

@@ -32,6 +32,11 @@ public interface CoreProperties {
String PROJECT_BRANCH_PROPERTY = "sonar.branch";
String PROJECT_VERSION_PROPERTY = "sonar.projectVersion";

/**
* @since 2.6
*/
String PROJECT_KEY_PROPERTY = "sonar.projectKey";

/**
* Value format is yyyy-MM-dd
*/

Loading…
Cancel
Save