aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>2012-10-05 09:53:10 +0200
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>2012-10-05 12:19:36 +0200
commit4603796ea1fbd7b9410c000bd738331a65dc4481 (patch)
treebb51a21d75d514e1c41f16adad837d58911e3b03 /sonar-batch
parent1bd03f2eebdfa1b56c1ce410081223b2b646754f (diff)
downloadsonarqube-4603796ea1fbd7b9410c000bd738331a65dc4481.tar.gz
sonarqube-4603796ea1fbd7b9410c000bd738331a65dc4481.zip
SONAR-3676 Create properties to define project links
=> even for non-Maven projects. The new properties are: - sonar.links.homepage - sonar.links.ci - sonar.links.issue - sonar.links.scm - sonar.links.scm_dev
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java61
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java33
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinks/pom.xml18
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml27
4 files changed, 128 insertions, 11 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
index a05c3c5dc11..1c25beabf6a 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
@@ -19,21 +19,27 @@
*/
package org.sonar.batch;
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.model.CiManagement;
+import org.apache.maven.model.IssueManagement;
+import org.apache.maven.model.Scm;
import org.apache.maven.project.MavenProject;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.utils.SonarException;
-import com.google.common.collect.Maps;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
public final class MavenProjectConverter {
private static final String UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE = "Unable to determine structure of project." +
- " Probably you use Maven Advanced Reactor Options, which is not supported by Sonar and should not be used.";
+ " Probably you use Maven Advanced Reactor Options, which is not supported by Sonar and should not be used.";
public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
Map<String, MavenProject> paths = Maps.newHashMap(); // projects by canonical path to pom.xml
@@ -78,24 +84,57 @@ public final class MavenProjectConverter {
return rootProject;
}
- /**
- * Visibility has been relaxed for tests.
- */
+ @VisibleForTesting
static ProjectDefinition convert(MavenProject pom) {
String key = new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString();
ProjectDefinition definition = ProjectDefinition.create();
// IMPORTANT NOTE : reference on properties from POM model must not be saved, instead they should be copied explicitly - see SONAR-2896
+ Properties properties = pom.getModel().getProperties();
+ convertMavenLinksToProperties(pom, properties);
definition
- .setProperties(pom.getModel().getProperties())
+ .setProperties(properties)
.setKey(key)
.setVersion(pom.getVersion())
.setName(pom.getName())
.setDescription(pom.getDescription())
.addContainerExtension(pom);
synchronizeFileSystem(pom, definition);
+
return definition;
}
+ /**
+ * For SONAR-3676
+ */
+ private static void convertMavenLinksToProperties(MavenProject pom, Properties properties) {
+ setPropertyIfNotAlreadyExists(properties, CoreProperties.LINKS_HOME_PAGE, pom.getUrl());
+
+ Scm scm = pom.getScm();
+ if (scm == null) {
+ scm = new Scm();
+ }
+ setPropertyIfNotAlreadyExists(properties, CoreProperties.LINKS_SOURCES, scm.getUrl());
+ setPropertyIfNotAlreadyExists(properties, CoreProperties.LINKS_SOURCES_DEV, scm.getDeveloperConnection());
+
+ CiManagement ci = pom.getCiManagement();
+ if (ci == null) {
+ ci = new CiManagement();
+ }
+ setPropertyIfNotAlreadyExists(properties, CoreProperties.LINKS_CI, ci.getUrl());
+
+ IssueManagement issues = pom.getIssueManagement();
+ if (issues == null) {
+ issues = new IssueManagement();
+ }
+ setPropertyIfNotAlreadyExists(properties, CoreProperties.LINKS_ISSUE_TRACKER, issues.getUrl());
+ }
+
+ private static void setPropertyIfNotAlreadyExists(Properties properties, String propertyKey, String propertyValue) {
+ if (StringUtils.isBlank(properties.getProperty(propertyKey))) {
+ properties.setProperty(propertyKey, StringUtils.defaultString(propertyValue));
+ }
+ }
+
public static void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) {
into.setBaseDir(pom.getBasedir());
into.setWorkDir(new File(resolvePath(pom.getBuild().getDirectory(), pom.getBasedir()), "sonar"));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
index 38abbf99fd0..91c77da73fa 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
@@ -37,6 +37,7 @@ import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Properties;
+import static org.fest.assertions.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
@@ -150,6 +151,38 @@ public class MavenProjectConverterTest {
assertThat(rootDef.getBaseDir(), is(rootDir));
}
+ @Test
+ public void shouldConvertLinksToProperties() throws Exception {
+ MavenProject pom = loadPom("/org/sonar/batch/MavenProjectConverterTest/projectWithLinks/pom.xml", true);
+
+ ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+
+ Properties props = rootDef.getProperties();
+ assertThat(props.getProperty(CoreProperties.LINKS_HOME_PAGE)).isEqualTo("http://home.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_CI)).isEqualTo("http://ci.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_ISSUE_TRACKER)).isEqualTo("http://issues.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_SOURCES)).isEqualTo("http://sources.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_SOURCES_DEV)).isEqualTo("http://sources-dev.com");
+ }
+
+ @Test
+ public void shouldNotConvertLinksToPropertiesIfPropertyAlreadyDefined() throws Exception {
+ MavenProject pom = loadPom("/org/sonar/batch/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml", true);
+
+ ProjectDefinition rootDef = MavenProjectConverter.convert(Arrays.asList(pom), pom);
+
+ Properties props = rootDef.getProperties();
+
+ // Those properties have been fed by the POM elements <ciManagement>, <issueManagement>, ...
+ assertThat(props.getProperty(CoreProperties.LINKS_CI)).isEqualTo("http://ci.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_ISSUE_TRACKER)).isEqualTo("http://issues.com");
+ assertThat(props.getProperty(CoreProperties.LINKS_SOURCES_DEV)).isEqualTo("http://sources-dev.com");
+
+ // ... but those ones have been overridden by <properties> in the POM
+ assertThat(props.getProperty(CoreProperties.LINKS_SOURCES)).isEqualTo("http://sources.com-OVERRIDEN-BY-PROPS");
+ assertThat(props.getProperty(CoreProperties.LINKS_HOME_PAGE)).isEqualTo("http://home.com-OVERRIDEN-BY-PROPS");
+ }
+
private MavenProject loadPom(String pomPath, boolean isRoot) throws URISyntaxException, IOException, XmlPullParserException {
File pomFile = new File(getClass().getResource(pomPath).toURI());
Model model = new MavenXpp3Reader().read(new StringReader(FileUtils.readFileToString(pomFile)));
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinks/pom.xml b/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinks/pom.xml
new file mode 100644
index 00000000000..460e8967e5c
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinks/pom.xml
@@ -0,0 +1,18 @@
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.test</groupId>
+ <artifactId>parent</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <packaging>pom</packaging>
+ <url>http://home.com</url>
+ <ciManagement>
+ <url>http://ci.com</url>
+ </ciManagement>
+ <issueManagement>
+ <url>http://issues.com</url>
+ </issueManagement>
+ <scm>
+ <url>http://sources.com</url>
+ <developerConnection>http://sources-dev.com</developerConnection>
+ </scm>
+</project> \ No newline at end of file
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml b/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml
new file mode 100644
index 00000000000..5b024e5c4a7
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/MavenProjectConverterTest/projectWithLinksAndProperties/pom.xml
@@ -0,0 +1,27 @@
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.test</groupId>
+ <artifactId>parent</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <packaging>pom</packaging>
+ <url>http://home.com</url>
+ <ciManagement>
+ <url>http://ci.com</url>
+ </ciManagement>
+ <issueManagement>
+ <url>http://issues.com</url>
+ </issueManagement>
+ <scm>
+ <url>http://sources.com</url>
+ <developerConnection>http://sources-dev.com</developerConnection>
+ </scm>
+
+
+
+ <properties>
+ <sonar.links.homepage>http://home.com-OVERRIDEN-BY-PROPS</sonar.links.homepage>
+ <sonar.links.scm>http://sources.com-OVERRIDEN-BY-PROPS</sonar.links.scm>
+ </properties>
+
+
+</project> \ No newline at end of file