From 7d924a378c4132525e21cb587a65c9cc402bd230 Mon Sep 17 00:00:00 2001 From: Richard Vowles Date: Sun, 27 Jul 2014 08:03:24 +1200 Subject: [PATCH] Fix working with reactor builds and error detect bad GAVs Each module when processed by the plugin needs to ensure it has a clean set of tiles to work with. This fixes this problem. The code has also been modified to ensure we test for correctly formed GAVs and to error if they aren't there. Change-Id: Ibd65a5e429de37eea1c8730b770404adb9ea28d0 --- .../TilesMavenLifecycleParticipant.groovy | 45 ++++++++++++------- .../TilesMavenLifecycleParticipantTest.groovy | 43 ++++++++++++++---- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/main/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipant.groovy b/src/main/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipant.groovy index e956f84..ddbb77f 100644 --- a/src/main/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipant.groovy +++ b/src/main/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipant.groovy @@ -99,9 +99,18 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic * We store the groupId:artifactId -> Artifact of those tiles we have discovered in our meanderings through * the */ - Map processedTiles = new HashMap() - List tileDiscoveryOrder = new ArrayList() - Map unprocessedTiles = new HashMap() + Map processedTiles = [:] + List tileDiscoveryOrder = [] + Map unprocessedTiles = [:] + + /** + * reactor builds can/will have their own tile structures + */ + protected void resetTiles() { + processedTiles = [:] + tileDiscoveryOrder = [] + unprocessedTiles = [:] + } protected Artifact getArtifactFromCoordinates(String groupId, String artifactId, String version) { return new DefaultArtifact(groupId, artifactId, VersionRange.createFromVersion(version), "compile", @@ -121,13 +130,18 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic return tileArtifact } - protected Artifact turnPropertyIntoUnprocessedTile(String propertyValue) { + protected Artifact turnPropertyIntoUnprocessedTile(String artifactGav, File pomFile) + throws MavenExecutionException { - StringTokenizer propertyTokens = new StringTokenizer(propertyValue, ":") + String[] gav = artifactGav.tokenize(":") - String groupId = propertyTokens.nextToken() - String artifactId = propertyTokens.nextToken() - String version = propertyTokens.nextToken() + if (gav.size() != 3) { + throw new MavenExecutionException("${artifactGav} does not have the form group:artifact:version-range", pomFile) + } + + String groupId = gav[0] + String artifactId = gav[1] + String version = gav[2] return getArtifactFromCoordinates(groupId, artifactId, version) } @@ -164,6 +178,7 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic //We're in a multi-module build, we need to trigger model merging on all sub-modules for (MavenProject subModule : mavenSession.getProjects()) { if (subModule != topLevelProject) { + resetTiles() orchestrateMerge(subModule) } } @@ -186,7 +201,7 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic Model propertyCollectionModel = project.getModel().clone() // collect the first set of tiles - collectTiles(propertyCollectionModel) + collectTiles(propertyCollectionModel, project.getFile()) // collect any unprocessed tiles, and process them causing them to potentially load more unprocessed ones resolveTiles() @@ -303,7 +318,7 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic processedTiles.put(artifactName(resolvedTile), new ArtifactModel(resolvedTile, tileModel)) - collectTiles(tileModel) + collectTiles(tileModel, resolvedTile.getFile()) } } @@ -319,7 +334,7 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic return String.format("%s:%s:%s", model.groupId, model.artifactId, model.version) } - protected void collectTiles(Model model) { + protected void collectTiles(Model model, File pomFile) { Xpp3Dom configuration = model?.build?.plugins?. find({ Plugin plugin -> return plugin.groupId == TILEPLUGIN_GROUP && @@ -327,15 +342,15 @@ public class TilesMavenLifecycleParticipant extends AbstractMavenLifecyclePartic if (configuration) { configuration.getChild("tiles")?.children?.each { Xpp3Dom tile -> - if (tile.getName() == "tile") { - collectConfigurationTile(model, tile.getValue()) + if (tile.name == "tile") { + collectConfigurationTile(model, tile.value, pomFile) } } } } - protected void collectConfigurationTile(Model model, String tileDependencyName) { - Artifact unprocessedTile = turnPropertyIntoUnprocessedTile(tileDependencyName) + protected void collectConfigurationTile(Model model, String tileDependencyName, File pomFile) { + Artifact unprocessedTile = turnPropertyIntoUnprocessedTile(tileDependencyName, pomFile) String depName = artifactName(unprocessedTile) diff --git a/src/test/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipantTest.groovy b/src/test/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipantTest.groovy index e13f3ca..395fdeb 100644 --- a/src/test/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipantTest.groovy +++ b/src/test/groovy/com/bluetrainsoftware/maven/tiles/TilesMavenLifecycleParticipantTest.groovy @@ -34,6 +34,7 @@ import org.junit.runner.RunWith import org.mockito.runners.MockitoJUnitRunner import static org.mockito.Mockito.mock +import static groovy.test.GroovyAssert.shouldFail /** * If testMergeTile fails with java.io.FileNotFoundException: src/test/resources/licenses-tiles-pom.xml @@ -99,25 +100,36 @@ public class TilesMavenLifecycleParticipantTest { } @Test - public void testMerge() throws MavenExecutionException { - Model model = new Model() + public void testBadGav() { + Model model = createBasicModel() + addTileAndPlugin(model, "groupid:artifactid") + participant = new TilesMavenLifecycleParticipant() + MavenProject project = new MavenProject(model) - model.setGroupId("com.bluetrainsoftware.maven") - model.setArtifactId("maven-tiles-example") - model.setVersion("1.1-SNAPSHOT") + Throwable failure = shouldFail { + participant.orchestrateMerge(project) + } - Properties model1Properties = new Properties() - model1Properties.setProperty("property1", "property1") - model.setProperties(model1Properties) + assert failure.message == "groupid:artifactid does not have the form group:artifact:version-range" + } + public void addTileAndPlugin(Model model, String gav) { // add our plugin model.build = new Build() model.build.addPlugin(new Plugin()) model.build.plugins[0].with { groupId = TilesMavenLifecycleParticipant.TILEPLUGIN_GROUP artifactId = TilesMavenLifecycleParticipant.TILEPLUGIN_ARTIFACT - configuration = Xpp3DomBuilder.build(new StringReader("${TILE_TEST_COORDINATES}")) + // bad GAV + configuration = Xpp3DomBuilder.build(new StringReader("${gav}")) } + } + + + @Test + public void testMerge() throws MavenExecutionException { + Model model = createBasicModel() + addTileAndPlugin(model, TILE_TEST_COORDINATES) Model pureModel = model.clone() @@ -166,4 +178,17 @@ public class TilesMavenLifecycleParticipantTest { assert executions*.id.intersect(["print-antrun1", "print-antrun2"]) } } + + protected Model createBasicModel() { + Model model = new Model() + + model.setGroupId("com.bluetrainsoftware.maven") + model.setArtifactId("maven-tiles-example") + model.setVersion("1.1-SNAPSHOT") + + Properties model1Properties = new Properties() + model1Properties.setProperty("property1", "property1") + model.setProperties(model1Properties) + model + } }