diff --git a/pom.xml b/pom.xml index f13a2256..f4644a09 100644 --- a/pom.xml +++ b/pom.xml @@ -166,6 +166,18 @@ 2.0.0 test + + org.apache.maven.plugin-testing + maven-plugin-testing-harness + test + 3.3.0 + + + org.apache.maven + maven-compat + ${mavenVersion} + test + org.apache.maven.plugin-tools diff --git a/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/invoker.properties b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/invoker.properties new file mode 100644 index 00000000..1313ddc0 --- /dev/null +++ b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/invoker.properties @@ -0,0 +1 @@ +invoker.mavenOpts = -Drevision=1.2.3.4 -Dany.property diff --git a/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml new file mode 100644 index 00000000..0eabb7c0 --- /dev/null +++ b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + resolve-properties-ci-do-not-interpolate-profile-activation-file + ${revision} + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + + resolveCiFriendliesOnly + + + + + + + + 0 + + + file.txt + + + + + 1 + + + ${basedir}/file.txt + + + + + 2 + + + ${any.property} + + + + + 3 + + + ${revision} + + + + + diff --git a/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/verify.groovy b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/verify.groovy new file mode 100644 index 00000000..bb1a1924 --- /dev/null +++ b/src/it/projects/resolve-properties-ci-do-not-interpolate-profile-activation-file/verify.groovy @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +File originalPom = new File( basedir, 'pom.xml' ) +assert originalPom.exists() + +def originalProject = new XmlSlurper().parse( originalPom ) +assert 'file.txt' == originalProject.profiles.profile[0].activation.file.exists.text() +assert '${basedir}/file.txt' == originalProject.profiles.profile[1].activation.file.exists.text() +assert '${any.property}' == originalProject.profiles.profile[2].activation.file.exists.text() +assert '${revision}' == originalProject.profiles.profile[3].activation.file.exists.text() + +File flattenedPom = new File( basedir, '.flattened-pom.xml' ) +assert flattenedPom.exists() +def flattenedProject = new XmlSlurper().parse( flattenedPom ) + +assert 'file.txt' == flattenedProject.profiles.profile[0].activation.file.exists.text() +assert '${basedir}/file.txt' == flattenedProject.profiles.profile[1].activation.file.exists.text() +assert '${any.property}' == flattenedProject.profiles.profile[2].activation.file.exists.text() +assert '1.2.3.4' == flattenedProject.profiles.profile[3].activation.file.exists.text() diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index f98a9240..89731cc1 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -41,6 +41,7 @@ import org.apache.maven.model.building.ModelBuildingResult; import org.apache.maven.model.building.ModelProblemCollector; import org.apache.maven.model.interpolation.ModelInterpolator; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.model.profile.ProfileActivationContext; import org.apache.maven.model.profile.ProfileInjector; @@ -60,9 +61,9 @@ import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor; import org.codehaus.mojo.flatten.cifriendly.CiInterpolator; import org.codehaus.mojo.flatten.model.resolution.FlattenModelResolver; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.impl.ArtifactDescriptorReader; import org.eclipse.aether.resolution.ArtifactDescriptorException; @@ -569,7 +570,7 @@ protected Model createFlattenedPom( File pomFile ) throws MojoExecutionException } FlattenDescriptor descriptor = getFlattenDescriptor(); - Model originalPom = this.project.getOriginalModel(); + Model originalPom = getOriginalModel(); Model resolvedPom = this.project.getModel(); Model interpolatedPom = createResolvedPom( buildingRequest ); @@ -599,10 +600,9 @@ protected Model createFlattenedPom( File pomFile ) throws MojoExecutionException return flattenedPom; } - private Model createResolvedPom( ModelBuildingRequest buildingRequest ) - { + private Model createResolvedPom( ModelBuildingRequest buildingRequest ) throws MojoExecutionException { LoggingModelProblemCollector problems = new LoggingModelProblemCollector( getLog() ); - Model originalModel = this.project.getOriginalModel().clone(); + Model originalModel = getOriginalModel(); if (this.flattenMode == FlattenMode.resolveCiFriendliesOnly) { return this.modelCiFriendlyInterpolator.interpolateModel( originalModel, this.project.getModel().getProjectDirectory(), buildingRequest, problems ); @@ -611,6 +611,16 @@ private Model createResolvedPom( ModelBuildingRequest buildingRequest ) buildingRequest, problems ); } + private Model getOriginalModel() throws MojoExecutionException { + MavenXpp3Reader reader = new MavenXpp3Reader(); + try { + return reader.read( new FileInputStream( this.project.getFile() ) ); + } + catch ( IOException | XmlPullParserException e ) { + throw new MojoExecutionException( "Error reading raw model.", e ); + } + } + /** * This method creates the clean POM as a {@link Model} where to copy elements from that shall be * {@link ElementHandling#flatten flattened}. Will be mainly empty but contains some the minimum elements that have diff --git a/src/test/java/org/codehaus/mojo/flatten/FlattenMojoTest.java b/src/test/java/org/codehaus/mojo/flatten/FlattenMojoTest.java new file mode 100644 index 00000000..562699cc --- /dev/null +++ b/src/test/java/org/codehaus/mojo/flatten/FlattenMojoTest.java @@ -0,0 +1,90 @@ +package org.codehaus.mojo.flatten; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test-Case for {@link FlattenMojo}. + * + * @author dehasi + */ +public class FlattenMojoTest { + + private static final String PATH = "src/test/resources/resolve-properties-ci-do-not-interpolate-profile-activation-file/"; + private static final String POM = PATH + "pom.xml"; + private static final String FLATTENED_POM = PATH + ".flattened-pom.xml"; + + @Rule + public MojoRule rule = new MojoRule(); + + /** + * Test method to check that profile activation file is not interpolated. + * + * @throws Exception if something goes wrong. + */ + @Test + public void keepsProfileActivationFile() throws Exception { + MavenProject project = rule.readMavenProject( new File( PATH ) ); + FlattenMojo flattenMojo = (FlattenMojo) rule.lookupConfiguredMojo( project, "flatten" ); + + flattenMojo.execute(); + + assertThat( profileActivationFile( FLATTENED_POM ) ) + .isEqualTo( profileActivationFile( POM ) ); + } + + private static String profileActivationFile( String pom ) throws Exception { + return readPom( pom ).getProfiles().get( 0 ).getActivation().getFile().getExists(); + } + + private static Model readPom(String pomFilePath) throws IOException, XmlPullParserException { + MavenXpp3Reader reader = new MavenXpp3Reader(); + + return reader.read( new FileInputStream( new File( pomFilePath ) ) ); + } + + /** + * After test method. Removes flattened-pom.xml file which is created during test. + * + * @throws RuntimeException if can't remove file. + */ + @After + public void removeFlattenedPom() throws IOException { + File flattenedPom = new File( FLATTENED_POM ); + if ( flattenedPom.exists() ) { + if ( !flattenedPom.delete() ) { + throw new IOException( "Can't delete %s" + flattenedPom ); + } + } + } +} diff --git a/src/test/resources/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml b/src/test/resources/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml new file mode 100644 index 00000000..01f9f7e8 --- /dev/null +++ b/src/test/resources/resolve-properties-ci-do-not-interpolate-profile-activation-file/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + resolve-properties-ci-do-not-interpolate-profile-activation-file + ${revision} + + + 1.2.3.4 + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + + resolveCiFriendliesOnly + + + + + + + + + + file.txt + + + + +