From 4d4579cf86aea6f5d1a729504b26776f5e098822 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 7 Feb 2022 14:23:06 +0100 Subject: [PATCH 1/2] [MPLUGIN-390] Do not overwrite generate files with no content change --- .../plugin/generator/PluginDescriptorGenerator.java | 10 +++------- .../tools/plugin/generator/PluginHelpGenerator.java | 9 +++++++-- .../tools/plugin/generator/PluginXdocGenerator.java | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java index 70581131f..cfa41389f 100644 --- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java +++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java @@ -20,7 +20,6 @@ */ import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; @@ -39,6 +38,7 @@ import org.apache.maven.tools.plugin.PluginToolsRequest; import org.apache.maven.tools.plugin.util.PluginUtils; import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.io.CachingOutputStream; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; @@ -89,16 +89,12 @@ public void writeDescriptor( File destinationFile, PluginToolsRequest request, b { PluginDescriptor pluginDescriptor = request.getPluginDescriptor(); - if ( destinationFile.exists() ) - { - destinationFile.delete(); - } - else if ( !destinationFile.getParentFile().exists() ) + if ( !destinationFile.getParentFile().exists() ) { destinationFile.getParentFile().mkdirs(); } - try ( Writer writer = new OutputStreamWriter( new FileOutputStream( destinationFile ), UTF_8 ) ) + try ( Writer writer = new OutputStreamWriter( new CachingOutputStream( destinationFile ), UTF_8 ) ) { XMLWriter w = new PrettyPrintXMLWriter( writer, UTF_8.name(), null ); diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java index 9149712c8..79ef6cba9 100644 --- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java +++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginHelpGenerator.java @@ -23,15 +23,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.StringWriter; +import java.io.Writer; import org.apache.maven.project.MavenProject; import org.apache.velocity.VelocityContext; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.console.ConsoleLogger; -import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.io.CachingOutputStream; import org.codehaus.plexus.velocity.VelocityComponent; import static java.nio.charset.StandardCharsets.UTF_8; @@ -84,7 +86,10 @@ public void execute( File destinationDirectory ) String helpClassSources = getHelpClassSources( getPluginHelpPath( mavenProject ) ); - FileUtils.fileWrite( helpClass, UTF_8.name(), helpClassSources ); + try ( Writer w = new OutputStreamWriter( new CachingOutputStream( helpClass ), UTF_8 ) ) + { + w.write( helpClassSources ); + } } catch ( IOException e ) { diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java index c17ec5954..481dc5a05 100644 --- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java +++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginXdocGenerator.java @@ -27,11 +27,11 @@ import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; import org.apache.maven.tools.plugin.PluginToolsRequest; import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.io.CachingOutputStream; import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; import org.codehaus.plexus.util.xml.XMLWriter; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -133,7 +133,7 @@ protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, File destin throws IOException { File outputFile = new File( destinationDirectory, getMojoFilename( mojoDescriptor, "xml" ) ); - try ( Writer writer = new OutputStreamWriter( new FileOutputStream( outputFile ), UTF_8 ) ) + try ( Writer writer = new OutputStreamWriter( new CachingOutputStream( outputFile ), UTF_8 ) ) { XMLWriter w = new PrettyPrintXMLWriter( new PrintWriter( writer ), UTF_8.name(), null ); writeBody( mojoDescriptor, w ); From 93791af5d6b0499a61226f21cb66e6e265a3f8c0 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 14 Feb 2022 14:26:11 +0100 Subject: [PATCH 2/2] Add an integration test --- .../src/it/mplugin-390/invoker.properties | 19 +++ .../src/it/mplugin-390/pom.xml | 114 ++++++++++++++++++ .../apache/maven/plugin/coreit/FirstMojo.java | 53 ++++++++ .../src/it/mplugin-390/verify.groovy | 29 +++++ 4 files changed, 215 insertions(+) create mode 100644 maven-plugin-plugin/src/it/mplugin-390/invoker.properties create mode 100644 maven-plugin-plugin/src/it/mplugin-390/pom.xml create mode 100644 maven-plugin-plugin/src/it/mplugin-390/src/main/java/org/apache/maven/plugin/coreit/FirstMojo.java create mode 100644 maven-plugin-plugin/src/it/mplugin-390/verify.groovy diff --git a/maven-plugin-plugin/src/it/mplugin-390/invoker.properties b/maven-plugin-plugin/src/it/mplugin-390/invoker.properties new file mode 100644 index 000000000..f8ed77e2d --- /dev/null +++ b/maven-plugin-plugin/src/it/mplugin-390/invoker.properties @@ -0,0 +1,19 @@ +# 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. + +invoker.goals.1 = process-classes +invoker.goals.2 = process-classes diff --git a/maven-plugin-plugin/src/it/mplugin-390/pom.xml b/maven-plugin-plugin/src/it/mplugin-390/pom.xml new file mode 100644 index 000000000..75ed4247f --- /dev/null +++ b/maven-plugin-plugin/src/it/mplugin-390/pom.xml @@ -0,0 +1,114 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mplugin-390 + mplugin-390 + 1.0-SNAPSHOT + maven-plugin + + Maven Integration Test :: mplugin-390 + + Test plugin-plugin, plugin.xml descriptor - shouldn't contain dependencies in provided scope + + + + UTF-8 + + + + + org.apache.maven + maven-plugin-api + @mavenVersion@ + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + @project.version@ + provided + + + + + org.apache.commons + commons-math3 + 3.6.1 + + + + + org.apache.commons + commons-rng-client-api + 1.4 + runtime + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 3.0.0 + + + touch + generate-resources + + run + + + + + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + @project.version@ + + + + + diff --git a/maven-plugin-plugin/src/it/mplugin-390/src/main/java/org/apache/maven/plugin/coreit/FirstMojo.java b/maven-plugin-plugin/src/it/mplugin-390/src/main/java/org/apache/maven/plugin/coreit/FirstMojo.java new file mode 100644 index 000000000..b8f7f62cd --- /dev/null +++ b/maven-plugin-plugin/src/it/mplugin-390/src/main/java/org/apache/maven/plugin/coreit/FirstMojo.java @@ -0,0 +1,53 @@ +package org.apache.maven.plugin.coreit; + +/* + * 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 org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +/** + * Touches a test file. + * + * @since 1.2 + */ +@Mojo( name = "first", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.INTEGRATION_TEST ) +public class FirstMojo + extends AbstractMojo +{ + + /** + * @since 0.1 + * @deprecated As of 0.2 + */ + @Parameter( alias = "alias" ) + private String aliasedParam; + + public void execute() + throws MojoExecutionException + { + // nothing + } +} diff --git a/maven-plugin-plugin/src/it/mplugin-390/verify.groovy b/maven-plugin-plugin/src/it/mplugin-390/verify.groovy new file mode 100644 index 000000000..000ea824d --- /dev/null +++ b/maven-plugin-plugin/src/it/mplugin-390/verify.groovy @@ -0,0 +1,29 @@ +/* + * 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 descriptorFile = new File( basedir, "target/classes/META-INF/maven/plugin.xml" ); +assert descriptorFile.isFile() + +File touchFile = new File( basedir, "target/touch.txt" ); +assert touchFile.isFile() + +// the touch file should be newer than the descriptor +assert touchFile.lastModified() > descriptorFile.lastModified() + +return true;