Skip to content

Commit

Permalink
[MSHARED-1382] support a possibility to disable snapshots update.
Browse files Browse the repository at this point in the history
  • Loading branch information
lrozenblyum authored and slawekjaranowski committed May 3, 2024
1 parent d0a2fa6 commit f7c295b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class DefaultInvocationRequest implements InvocationRequest {

private boolean showErrors;

private boolean updateSnapshots;
private UpdateSnapshotsPolicy updateSnapshotsPolicy = UpdateSnapshotsPolicy.DEFAULT;

private boolean shellEnvironmentInherited = true;

Expand Down Expand Up @@ -227,7 +227,11 @@ public boolean isShowErrors() {
* @return a boolean.
*/
public boolean isUpdateSnapshots() {
return updateSnapshots;
return updateSnapshotsPolicy == UpdateSnapshotsPolicy.ALWAYS;
}

public UpdateSnapshotsPolicy getUpdateSnapshotsPolicy() {
return updateSnapshotsPolicy;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>true</code> if plugins and snapshots should be updated, <code>false</code> otherwise.
* @return <code>true</code> if plugins and snapshots must be updated, <code>false</code> 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.
*
Expand Down Expand Up @@ -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 <code>true</code> if plugins and snapshots should be updated, <code>false</code>
* @param updateSnapshots <code>true</code> if plugins and snapshots must be updated, <code>false</code>
* 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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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() {

Expand Down

0 comments on commit f7c295b

Please sign in to comment.