fixed rewriting execution ids and rewrite them in profiles as well

This commit is contained in:
brian.vella
2019-01-16 10:38:51 +01:00
committed by Mark Derricutt
parent e06f8fbfce
commit 8c3f312a75
4 changed files with 197 additions and 18 deletions

View File

@@ -123,6 +123,70 @@ A tile can define the tiles plugin if it wishes to cascade tile inclusion, or it
Although this will appear to not validate when editing in an IDE, the tile plugin will strip off the tiles
section when processing and use it directly.
== Execution Ids
Execution ids within tiles are prepended with the gav parameters of the tile that included the execution, for easier
debugging / tracing. If this is not desired, adding a configuration entry `<tiles-keep-id>true<tiles-keep-id>` will
keep the original id.
[source,xml,indent=0]
.tile.xml
----
<?xml version="1.0" encoding="UTF-8"?>
<project>
<build>
<plugins>
<plugin>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>1</id>
</execution>
<execution>
<id>2</id>
<configuration>
<tiles-keep-id>true</tiles-keep-id>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>3</id>
</execution>
<execution>
<id>4</id>
<configuration>
<tiles-keep-id>true</tiles-keep-id>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
----
In the above tile, executions with ids 1 and 3 (in profile) will have their ids changed to
`io.repaint.tiles:execution-id-replacing-tile:1.1-SNAPSHOT::1` and
`io.repaint.tiles:execution-id-replacing-tile:1.1-SNAPSHOT::3` respectively, while executions with ids 2 and 4 (in
profile) will retain their original execution id.
== Build Smells
When migrating from a parent structure, it is worthwhile to take the opportunity to reduce your build smells. You

View File

@@ -1,11 +1,13 @@
package io.repaint.maven.tiles
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import groovy.xml.XmlUtil
import org.apache.maven.artifact.Artifact
import org.apache.maven.model.Model
import org.apache.maven.model.Plugin
import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.codehaus.plexus.util.xml.Xpp3Dom
/**
* This will parse a tile.xml file with the intent of removing extra syntax, holding onto it and then
* pushing the rest into a standard model. We could have used a Delegate or a Mixin here potentially, but
@@ -13,7 +15,6 @@ import org.codehaus.plexus.util.xml.Xpp3Dom
*
* @author: Richard Vowles - https://plus.google.com/+RichardVowles
*/
@CompileStatic
class TileModel {
Model model
List<String> tiles = []
@@ -44,7 +45,7 @@ class TileModel {
}
}
public void loadTile(File tilePom) {
void loadTile(File tilePom) {
this.tilePom = tilePom
MavenXpp3Reader pomReader = new MavenXpp3Reader()
@@ -52,8 +53,9 @@ class TileModel {
model = pomReader.read(strippedPom())
}
public TileModel() {}
public TileModel(File tilePom, Artifact artifact) {
TileModel() {}
TileModel(File tilePom, Artifact artifact) {
loadTile(tilePom)
// this is in the artifact but isn't actually in the file, we need it
@@ -65,22 +67,31 @@ class TileModel {
model.packaging = "pom"
// Update each tile'd plugin's execution id with the tile GAV for easier debugging/tracing
if (model.build) {
if (model.build.plugins) {
model.build.plugins.each { plugin ->
if (plugin.executions) {
plugin.executions.each { execution ->
Xpp3Dom configuration = execution.configuration as Xpp3Dom
if (configuration?.getAttribute("tiles-keep-id") == "true") {
// do not rewrite the current execution id
return
}
execution.id = GavUtil.artifactGav(artifact) + "::" + execution.id
}
}
if (model.build?.plugins) {
rewritePluginExecutionIds(model.build.plugins, artifact)
}
if (model.profiles) {
model.profiles.each { profile ->
if (profile.build?.plugins) {
rewritePluginExecutionIds(profile.build.plugins, artifact)
}
}
}
}
private static List<Plugin> rewritePluginExecutionIds(List<Plugin> plugins, Artifact artifact) {
plugins.each { plugin ->
if (plugin.executions) {
plugin.executions.each { execution ->
if (execution.configuration?.getChild("tiles-keep-id")?.getValue() == "true") {
// do not rewrite the current execution id
return
}
execution.id = GavUtil.artifactGav(artifact) + "::" + execution.id
}
}
}
}
}

View File

@@ -0,0 +1,40 @@
package io.repaint.maven.tiles
import io.repaint.maven.tiles.TileModel
import org.apache.maven.artifact.Artifact
import org.apache.maven.artifact.DefaultArtifact
import org.apache.maven.artifact.handler.DefaultArtifactHandler
import org.apache.maven.artifact.versioning.VersionRange
import org.junit.Test
/**
*
* @author: Richard Vowles - https://plus.google.com/+RichardVowles
*/
class TileModelTest {
@Test
public void testLoad() {
def loader = new TileModel()
loader.loadTile(new File("src/test/resources/extended-syntax-tile.xml"))
assert loader.tiles.size() == 2
}
@Test
public void testReplaceExecutionId() {
Artifact artifact = new DefaultArtifact("io.repaint.tiles",
"execution-id-replacing-tile",
VersionRange.createFromVersion("1.1-SNAPSHOT"),
"compile",
"xml",
"",
new DefaultArtifactHandler("xml"))
TileModel tileModel = new TileModel(new File("src/test/resources/execution-id-tile.xml"), artifact)
assert tileModel.model.build.plugins[0].executions[0].id == "io.repaint.tiles:execution-id-replacing-tile:1.1-SNAPSHOT::1"
assert tileModel.model.build.plugins[0].executions[1].id == "2"
assert tileModel.model.profiles[0].build.plugins[0].executions[0].id == "io.repaint.tiles:execution-id-replacing-tile:1.1-SNAPSHOT::3"
assert tileModel.model.profiles[0].build.plugins[0].executions[1].id == "4"
}
}

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<build>
<plugins>
<plugin>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>1</id>
</execution>
<execution>
<id>2</id>
<configuration>
<tiles-keep-id>true</tiles-keep-id>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>3</id>
</execution>
<execution>
<id>4</id>
<configuration>
<tiles-keep-id>true</tiles-keep-id>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>