From 8bb80b387d4f6df439c223acee98d841c25e7198 Mon Sep 17 00:00:00 2001 From: Nico Korthout Date: Wed, 29 Nov 2023 21:39:10 +0100 Subject: [PATCH] Add skip flags (#385) * Add skip flags This introduces 3 flags: - `flatten.skip`: skips the entire plugin regardless of goal - `flatten.clean.skip`: skips the clean goal - `flatten.flatten.skip`: skips the flatten goal This means the user has control over what parts are skipped, and the flags are descriptive. The `flatten.` prefix is in line with other parameters like `flatten.mode` and `flatten.dependency.mode`. * Test skip flags This adds 3 ITs for the skip flags: - to verify `skip` -> should not create/remove flattened pom - to verify `skipFlatten` -> should not create flattened pom - to verify `skipClean` -> should not remove flattened pom * Prepare for 1.6.0 release * Annotate skip flags as since 1.6.0 This is the release in which they'll be first available. --- pom.xml | 2 +- src/it/projects/clean-skip/.flattened-pom.xml | 16 ++++++++++ src/it/projects/clean-skip/pom.xml | 29 +++++++++++++++++++ src/it/projects/clean-skip/verify.groovy | 24 +++++++++++++++ src/it/projects/flatten-skip/pom.xml | 29 +++++++++++++++++++ src/it/projects/flatten-skip/verify.groovy | 24 +++++++++++++++ src/it/projects/skip/.flattened-pom.xml | 16 ++++++++++ src/it/projects/skip/pom.xml | 29 +++++++++++++++++++ src/it/projects/skip/verify.groovy | 28 ++++++++++++++++++ .../mojo/flatten/AbstractFlattenMojo.java | 17 +++++++++++ .../org/codehaus/mojo/flatten/CleanMojo.java | 18 ++++++++++++ .../codehaus/mojo/flatten/FlattenMojo.java | 17 +++++++++++ 12 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 src/it/projects/clean-skip/.flattened-pom.xml create mode 100644 src/it/projects/clean-skip/pom.xml create mode 100644 src/it/projects/clean-skip/verify.groovy create mode 100644 src/it/projects/flatten-skip/pom.xml create mode 100644 src/it/projects/flatten-skip/verify.groovy create mode 100644 src/it/projects/skip/.flattened-pom.xml create mode 100644 src/it/projects/skip/pom.xml create mode 100644 src/it/projects/skip/verify.groovy diff --git a/pom.xml b/pom.xml index 37894a1f..f56e54b8 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ flatten-maven-plugin - 1.5.1-SNAPSHOT + 1.6.0-SNAPSHOT maven-plugin Maven Flatten Plugin diff --git a/src/it/projects/clean-skip/.flattened-pom.xml b/src/it/projects/clean-skip/.flattened-pom.xml new file mode 100644 index 00000000..c7c5b961 --- /dev/null +++ b/src/it/projects/clean-skip/.flattened-pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + org.codehaus.mojo.flatten.its + resolve-properties + 0.0.1-SNAPSHOT + + + org.codehaus.mojo.flatten.its + dep + 1.1 + compile + + + diff --git a/src/it/projects/clean-skip/pom.xml b/src/it/projects/clean-skip/pom.xml new file mode 100644 index 00000000..d7c0aa94 --- /dev/null +++ b/src/it/projects/clean-skip/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-skip + 0.0.1-SNAPSHOT + + + + org.codehaus.mojo.flatten.its + dep + 1.1 + + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + @project.version@ + + true + + + + + diff --git a/src/it/projects/clean-skip/verify.groovy b/src/it/projects/clean-skip/verify.groovy new file mode 100644 index 00000000..9e828c4e --- /dev/null +++ b/src/it/projects/clean-skip/verify.groovy @@ -0,0 +1,24 @@ +/* + * 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. + */ + +// Clean mojo is skipped so we should expect flattened pom to still exist +File flattendPom = new File( basedir, '.flattened-pom.xml' ) +assert flattendPom.exists() + + diff --git a/src/it/projects/flatten-skip/pom.xml b/src/it/projects/flatten-skip/pom.xml new file mode 100644 index 00000000..50f6313e --- /dev/null +++ b/src/it/projects/flatten-skip/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-skip + 0.0.1-SNAPSHOT + + + + org.codehaus.mojo.flatten.its + dep + 1.1 + + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + @project.version@ + + true + + + + + diff --git a/src/it/projects/flatten-skip/verify.groovy b/src/it/projects/flatten-skip/verify.groovy new file mode 100644 index 00000000..9f5e8190 --- /dev/null +++ b/src/it/projects/flatten-skip/verify.groovy @@ -0,0 +1,24 @@ +/* + * 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. + */ + +// Flatten mojo is skipped so we should expect no flattened pom +File flattendPom = new File( basedir, '.flattened-pom.xml' ) +assert !flattendPom.exists() + + diff --git a/src/it/projects/skip/.flattened-pom.xml b/src/it/projects/skip/.flattened-pom.xml new file mode 100644 index 00000000..c7c5b961 --- /dev/null +++ b/src/it/projects/skip/.flattened-pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + org.codehaus.mojo.flatten.its + resolve-properties + 0.0.1-SNAPSHOT + + + org.codehaus.mojo.flatten.its + dep + 1.1 + compile + + + diff --git a/src/it/projects/skip/pom.xml b/src/it/projects/skip/pom.xml new file mode 100644 index 00000000..50f6313e --- /dev/null +++ b/src/it/projects/skip/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + flatten-skip + 0.0.1-SNAPSHOT + + + + org.codehaus.mojo.flatten.its + dep + 1.1 + + + + + verify + + + org.codehaus.mojo + flatten-maven-plugin + @project.version@ + + true + + + + + diff --git a/src/it/projects/skip/verify.groovy b/src/it/projects/skip/verify.groovy new file mode 100644 index 00000000..2d445d72 --- /dev/null +++ b/src/it/projects/skip/verify.groovy @@ -0,0 +1,28 @@ +/* + * 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. + */ + +// Flatten plugin is skipped so we should expect that the flattened +// pom is not removed by the clean mojo, and not newly created by the +// flatten mojo, i.e. it is left unchanged. We can check this by +// asserting it was last modified before starting to run this test +File flattendPom = new File( basedir, '.flattened-pom.xml' ) +assert flattendPom.exists() +long now = System.currentTimeMillis() +assert now - flattendPom.lastModified() > 20*1000 + diff --git a/src/main/java/org/codehaus/mojo/flatten/AbstractFlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/AbstractFlattenMojo.java index 9c48435d..63c30b6b 100644 --- a/src/main/java/org/codehaus/mojo/flatten/AbstractFlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/AbstractFlattenMojo.java @@ -43,6 +43,14 @@ public abstract class AbstractFlattenMojo extends AbstractMojo { @Parameter(property = "flattenedPomFilename", defaultValue = ".flattened-pom.xml") private String flattenedPomFilename; + /** + * If {@code true} the plugin will be skipped. + * + * @since 1.6.0 + */ + @Parameter(property = "flatten.skip", defaultValue = "false") + private boolean skip; + /** * The constructor. */ @@ -71,4 +79,13 @@ public File getOutputDirectory() { protected File getFlattenedPomFile() { return new File(getOutputDirectory(), getFlattenedPomFilename()); } + + protected boolean shouldSkip() { + if (skip) { + return true; + } + return shouldSkipGoal(); + } + + protected abstract boolean shouldSkipGoal(); } diff --git a/src/main/java/org/codehaus/mojo/flatten/CleanMojo.java b/src/main/java/org/codehaus/mojo/flatten/CleanMojo.java index 9ff42c36..3d98d577 100644 --- a/src/main/java/org/codehaus/mojo/flatten/CleanMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/CleanMojo.java @@ -24,6 +24,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; /** * This MOJO realizes the goal flatten:clean that deletes any files created by @@ -42,6 +43,14 @@ threadSafe = true) public class CleanMojo extends AbstractFlattenMojo { + /** + * If {@code true} the clean goal will be skipped. + * + * @since 1.6.0 + */ + @Parameter(property = "flatten.clean.skip", defaultValue = "false") + private boolean skipClean; + /** * The constructor. */ @@ -53,6 +62,10 @@ public CleanMojo() { * {@inheritDoc} */ public void execute() throws MojoExecutionException, MojoFailureException { + if (shouldSkip()) { + getLog().info("Clean skipped."); + return; + } File flattenedPomFile = getFlattenedPomFile(); if (flattenedPomFile.isFile()) { @@ -63,4 +76,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } } + + @Override + protected boolean shouldSkipGoal() { + return skipClean; + } } diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index e7f58478..55fbe797 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -364,6 +364,14 @@ public class FlattenMojo extends AbstractFlattenMojo { @Parameter(property = "flatten.dependency.keepComments", required = false, defaultValue = "false") private boolean keepCommentsInPom; + /** + * If {@code true} the flatten goal will be skipped. + * + * @since 1.6.0 + */ + @Parameter(property = "flatten.flatten.skip", defaultValue = "false") + private boolean skipFlatten; + @Inject private DirectDependenciesInheritanceAssembler inheritanceAssembler; @@ -400,6 +408,10 @@ public FlattenMojo() { */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + if (shouldSkip()) { + getLog().info("Flatten skipped."); + return; + } getLog().info("Generating flattened POM of project " + this.project.getId() + "..."); @@ -422,6 +434,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } + @Override + protected boolean shouldSkipGoal() { + return skipFlatten; + } + /** * This method extracts the XML header comment if available. *