Skip to content

Commit

Permalink
Merge branch 'cdelmas-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Jan 12, 2016
2 parents 74e0d3b + 43bf8ba commit 6e3772b
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/it/organization-pom/first/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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/xsd/maven-4.0.0.xsd">

<parent>
<groupId>com.smartcodeltd</groupId>
<artifactId>sample-project-with-organization-pom</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>


<modelVersion>4.0.0</modelVersion>

<artifactId>first</artifactId>

</project>
8 changes: 8 additions & 0 deletions src/it/organization-pom/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
invoker.name = Updates child versions but not org parent version
invoker.description = https://github.com/smartcodeltd/release-candidate-maven-plugin/issues/5

invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:updateVersion
invoker.buildResult = success
invoker.mavenOpts = -Dbuild_number=2
invoker.debug = false
invoker.nonRecursive = false
13 changes: 13 additions & 0 deletions src/it/organization-pom/org-parent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.smartcodeltd.parent</groupId>
<artifactId>org-parent</artifactId>
<version>1.5</version>

<packaging>pom</packaging>

</project>
37 changes: 37 additions & 0 deletions src/it/organization-pom/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.smartcodeltd.parent</groupId>
<artifactId>org-parent</artifactId>
<version>1.5</version>
<relativePath>./org-parent</relativePath>
</parent>

<groupId>com.smartcodeltd</groupId>
<artifactId>sample-project-with-organization-pom</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
<module>first</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>com.smartcodeltd</groupId>
<artifactId>release-candidate-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<configuration>
<releaseVersionFormat>{{ api_version }}-${build_number}</releaseVersionFormat>
</configuration>
</plugin>
</plugins>
</build>

</project>
9 changes: 9 additions & 0 deletions src/it/organization-pom/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
new GroovyShell().parse(new File('./src/it/integration_tests.groovy')).with {

parent = parse(basedir, 'pom.xml')
first_child = parse(basedir, 'first/pom.xml')

expect parent.parent.version, "1.5", "version of the organization POM"
expect parent.version, "1.0-2", "version of parent POM"
expect first_child.parent.version, "1.0-2", "version of parent POM referenced from first child POM"
}
17 changes: 10 additions & 7 deletions src/main/java/com/smartcodeltd/ReleaseCandidateMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.smartcodeltd;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.smartcodeltd.domain.Version;
Expand All @@ -11,11 +15,6 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;

abstract public class ReleaseCandidateMojo
extends AbstractMojo
{
Expand Down Expand Up @@ -49,11 +48,15 @@ protected Version versionOf(MavenProject project) {
}

protected MavenProject root(MavenProject project) {
return project.hasParent()
? project.getParent()
return project.hasParent() && !isOrganizationPom(project.getParent())
? root(project.getParent())
: project;
}

private boolean isOrganizationPom(MavenProject parent) {
return parent.getPackaging().equals("pom") && parent.getModules().isEmpty();
}

protected <T> T getOrElse(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
Expand Down
42 changes: 35 additions & 7 deletions src/main/java/com/smartcodeltd/UpdateVersionMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.smartcodeltd;

import static java.util.Arrays.asList;

import java.io.File;
import java.io.IOException;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
Expand All @@ -9,11 +14,7 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.IOException;

import static java.util.Arrays.asList;
import org.apache.maven.project.MavenProject;

/**
* <p>
Expand Down Expand Up @@ -147,13 +148,40 @@ private void update(File pom, String newVersion) throws IOException {
Document doc = parsed(pom);

firstExisting(
doc.getChild("project/parent/version"),
doc.getChild("project/version")
parentVersion(doc),
projectVersion(doc)
).setText(newVersion);

Files.write(doc.toString(), pom, charset);
}

private Element projectVersion(Document doc) {
return doc.getChild("project/version");
}

private Element parentVersion(Document doc) {
if(project.hasParent()
&& !isOrganizationPom(project.getParent())
&& isSnapshot(project.getParent())
&& sameGroupId(project, project.getParent())) {
return doc.getChild("project/parent/version");
} else {
return null;
}
}

private boolean sameGroupId(MavenProject project, MavenProject parent) {
return project.getGroupId().equals(parent.getGroupId());
}

private boolean isSnapshot(MavenProject project) {
return project.getVersion().endsWith("SNAPSHOT");
}

private boolean isOrganizationPom(MavenProject parent) {
return "pom".equals(parent.getPackaging()) && parent.getModules().isEmpty();
}

private Element firstExisting(Element... elements) {
return Iterables.find(asList(elements), Predicates.notNull(), new Element("dummy"));
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/smartcodeltd/sugar/Difference.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.smartcodeltd.sugar;

import com.google.common.base.Objects;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import com.google.common.base.Objects;

public class Difference {
public final String left;
Expand All @@ -24,8 +24,8 @@ public static Difference difference(String left, String right) {
public static List<Difference> differenceOf(String originalContent, String newContent) {
List<Difference> differences = new ArrayList<Difference>();

String[] originalContentLines = originalContent.split("\\n");
String[] newContentLines = newContent.split("\\n");
String[] originalContentLines = originalContent.split(System.getProperty("line.separator", "\\n"));
String[] newContentLines = newContent.split(System.getProperty("line.separator", "\\n"));

assertThat("Number of lines differs", originalContentLines.length, is(newContentLines.length));

Expand Down

0 comments on commit 6e3772b

Please sign in to comment.