From f7c295b153e5424ef7fa79b85f5a009dda26e490 Mon Sep 17 00:00:00 2001 From: Leonid Rozenblyum Date: Sun, 21 Apr 2024 17:15:55 +0300 Subject: [PATCH] [MSHARED-1382] support a possibility to disable snapshots update. --- .../invoker/DefaultInvocationRequest.java | 15 +++++-- .../shared/invoker/InvocationRequest.java | 24 +++++++++++- .../invoker/MavenCommandLineBuilder.java | 6 ++- .../shared/invoker/UpdateSnapshotsPolicy.java | 39 +++++++++++++++++++ .../invoker/MavenCommandLineBuilderTest.java | 27 +++++++++++++ 5 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/apache/maven/shared/invoker/UpdateSnapshotsPolicy.java diff --git a/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java b/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java index d4fd9e4..7b1c6fb 100644 --- a/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java +++ b/src/main/java/org/apache/maven/shared/invoker/DefaultInvocationRequest.java @@ -63,7 +63,7 @@ public class DefaultInvocationRequest implements InvocationRequest { private boolean showErrors; - private boolean updateSnapshots; + private UpdateSnapshotsPolicy updateSnapshotsPolicy = UpdateSnapshotsPolicy.DEFAULT; private boolean shellEnvironmentInherited = true; @@ -227,7 +227,11 @@ public boolean isShowErrors() { * @return a boolean. */ public boolean isUpdateSnapshots() { - return updateSnapshots; + return updateSnapshotsPolicy == UpdateSnapshotsPolicy.ALWAYS; + } + + public UpdateSnapshotsPolicy getUpdateSnapshotsPolicy() { + return updateSnapshotsPolicy; } /** @@ -330,7 +334,12 @@ public InvocationRequest setShowErrors(boolean showErrors) { /** {@inheritDoc} */ public InvocationRequest setUpdateSnapshots(boolean updateSnapshots) { - this.updateSnapshots = updateSnapshots; + return setUpdateSnapshotsPolicy(updateSnapshots ? UpdateSnapshotsPolicy.ALWAYS : UpdateSnapshotsPolicy.DEFAULT); + } + + @Override + public InvocationRequest setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy policy) { + this.updateSnapshotsPolicy = policy; return this; } diff --git a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java index 77dc0bf..f4a499c 100644 --- a/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java +++ b/src/main/java/org/apache/maven/shared/invoker/InvocationRequest.java @@ -51,10 +51,19 @@ public interface InvocationRequest { * Indicates whether Maven should enforce an update check for plugins and snapshots. By default, no update check is * performed. * - * @return true if plugins and snapshots should be updated, false otherwise. + * @return true if plugins and snapshots must be updated, false otherwise. + * + * @see #getUpdateSnapshotsPolicy() which provides a richer variety of the update snapshots policy values. */ boolean isUpdateSnapshots(); + /** + * Indicates the update snapshots policy. + * @return the update snapshots policy. + * @see UpdateSnapshotsPolicy + */ + UpdateSnapshotsPolicy getUpdateSnapshotsPolicy(); + /** * Gets the recursion behavior of a reactor invocation. By default, Maven will recursive the build into sub modules. * @@ -451,12 +460,23 @@ enum CheckSumPolicy { * Specifies whether Maven should enforce an update check for plugins and snapshots. Equivalent of {@code -U} and * {@code --update-snapshots} * - * @param updateSnapshots true if plugins and snapshots should be updated, false + * @param updateSnapshots true if plugins and snapshots must be updated, false * otherwise. * @return This invocation request. + * + * @see #setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy) which provides a richer variety of the update snapshots policy values. */ InvocationRequest setUpdateSnapshots(boolean updateSnapshots); + /** + * Specify the Maven update snapshots policy + * @param policy the policy to be set + * @return This invocation request. + * + * @see UpdateSnapshotsPolicy + */ + InvocationRequest setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy policy); + /** * Sets the failure mode of the Maven invocation. Equivalent of {@code -ff} and {@code --fail-fast}, {@code -fae} * and {@code --fail-at-end}, {@code -fn} and {@code --fail-never} diff --git a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java index a6863c2..22e67dc 100644 --- a/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java +++ b/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java @@ -408,10 +408,14 @@ protected void setFlags(InvocationRequest request, Commandline cli) { cli.createArg().setValue("-o"); } - if (request.isUpdateSnapshots()) { + if (request.getUpdateSnapshotsPolicy() == UpdateSnapshotsPolicy.ALWAYS) { cli.createArg().setValue("-U"); } + if (request.getUpdateSnapshotsPolicy() == UpdateSnapshotsPolicy.NEVER) { + cli.createArg().setValue("-nsu"); + } + if (!request.isRecursive()) { cli.createArg().setValue("-N"); } diff --git a/src/main/java/org/apache/maven/shared/invoker/UpdateSnapshotsPolicy.java b/src/main/java/org/apache/maven/shared/invoker/UpdateSnapshotsPolicy.java new file mode 100644 index 0000000..f5b5b09 --- /dev/null +++ b/src/main/java/org/apache/maven/shared/invoker/UpdateSnapshotsPolicy.java @@ -0,0 +1,39 @@ +/* + * 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. + */ +package org.apache.maven.shared.invoker; + +/** + * Define how Maven should update snapshots. + */ +public enum UpdateSnapshotsPolicy { + /** + * Request Maven to always update snapshots. + */ + ALWAYS, + + /** + * Don't control Maven policy on snapshots updates. + */ + DEFAULT, + + /** + * Prevent Maven updating snapshots. + */ + NEVER +} diff --git a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java index 5141b95..13b1b2e 100644 --- a/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java +++ b/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java @@ -299,6 +299,33 @@ public void testShouldSetUpdateSnapshotsFlagFromRequest() { assertArgumentsPresent(cli, Collections.singleton("-U")); } + // JUnit5: test methods don't need to be public + @Test + void testShouldSetUpdateSnapshotsPolicyAlwaysFromRequest() { + mclb.setFlags(newRequest().setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy.ALWAYS), cli); + + assertArgumentsPresent(cli, Collections.singleton("-U")); + assertArgumentsNotPresent(cli, Collections.singleton("-nsu")); + } + + @Test + void testShouldSetUpdateSnapshotsPolicyDefaultFromRequest() { + mclb.setFlags(newRequest().setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy.DEFAULT), cli); + + Set args = new HashSet<>(); + args.add("-U"); + args.add("-nsu"); + assertArgumentsNotPresent(cli, args); + } + + @Test + void testShouldSetUpdateSnapshotsPolicyNeverFromRequest() { + mclb.setFlags(newRequest().setUpdateSnapshotsPolicy(UpdateSnapshotsPolicy.NEVER), cli); + + assertArgumentsPresent(cli, Collections.singleton("-nsu")); + assertArgumentsNotPresent(cli, Collections.singleton("-U")); + } + @Test public void testShouldSetDebugFlagFromRequest() {