You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/09/12 21:52:30 UTC
[maven-verifier] branch master updated: [MSHARED-1128] Introduce execute method and deprecate executeGoal(s)
This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-verifier.git
The following commit(s) were added to refs/heads/master by this push:
new 6b9dbb5 [MSHARED-1128] Introduce execute method and deprecate executeGoal(s)
6b9dbb5 is described below
commit 6b9dbb527158002e13b79d2c6c30b06026213ad9
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sun Sep 11 17:29:14 2022 +0200
[MSHARED-1128] Introduce execute method and deprecate executeGoal(s)
---
.../org/apache/maven/shared/verifier/Verifier.java | 114 ++++++++++++++++-----
.../apache/maven/shared/verifier/VerifierTest.java | 29 +++++-
2 files changed, 115 insertions(+), 28 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/verifier/Verifier.java b/src/main/java/org/apache/maven/shared/verifier/Verifier.java
index dfca520..fc1a78b 100644
--- a/src/main/java/org/apache/maven/shared/verifier/Verifier.java
+++ b/src/main/java/org/apache/maven/shared/verifier/Verifier.java
@@ -35,7 +35,6 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -66,6 +65,13 @@ public class Verifier
private static final String[] DEFAULT_CLI_ARGUMENTS = {"-e", "--batch-mode"};
+ /**
+ * Command used to clean project before execution.
+ * <p
+ * NOTE: Neither test lifecycle binding nor prefix resolution here but call the goal directly.
+ */
+ private static final String CLEAN_CLI_ARGUMENT = "org.apache.maven.plugins:maven-clean-plugin:clean";
+
private String localRepo;
private final String basedir;
@@ -1175,18 +1181,54 @@ public class Verifier
//
// ----------------------------------------------------------------------
+ /**
+ * Execute Maven.
+ * <p>
+ * For replacement please use:
+ * <pre>
+ * verifier.addCliArgument( "goal" );
+ * verifier.execute();
+ * </pre>
+ *
+ * @deprecated will be removed without replacement.
+ */
+ @Deprecated
public void executeGoal( String goal )
throws VerificationException
{
executeGoal( goal, environmentVariables );
}
+ /**
+ * Execute Maven.
+ * <p>
+ * For replacement please use:
+ * <pre>
+ * verifier.addCliArgument( "goal" );
+ * verifier.setEnvironmentVariable( "key1", "value1" );
+ * verifier.setEnvironmentVariable( "key2", "value2" );
+ * verifier.execute();
+ * </pre>
+ *
+ * @deprecated will be removed without replacement.
+ */
public void executeGoal( String goal, Map<String, String> envVars )
throws VerificationException
{
- executeGoals( Arrays.asList( goal ), envVars );
+ executeGoals( Collections.singletonList( goal ), envVars );
}
+ /**
+ * Execute Maven.
+ * <p>
+ * For replacement please use:
+ * <pre>
+ * verifier.addCliArguments( "goal1", "goal2" );
+ * verifier.execute();
+ * </pre>
+ *
+ * @deprecated will be removed without replacement.
+ */
public void executeGoals( List<String> goals )
throws VerificationException
{
@@ -1217,31 +1259,34 @@ public class Verifier
}
}
+ /**
+ * Execute Maven.
+ * <p>
+ * For replacement please use:
+ * <pre>
+ * verifier.addCliArguments( "goal1", "goal2" );
+ * verifier.setEnvironmentVariable( "key1", "value1" );
+ * verifier.setEnvironmentVariable( "key2", "value2" );
+ * verifier.execute();
+ * </pre>
+ *
+ * @deprecated will be removed without replacement.
+ */
public void executeGoals( List<String> goals, Map<String, String> envVars )
throws VerificationException
{
- List<String> allGoals = new ArrayList<String>();
-
- if ( autoclean )
- {
- /*
- * NOTE: Neither test lifecycle binding nor prefix resolution here but call the goal directly.
- */
- allGoals.add( "org.apache.maven.plugins:maven-clean-plugin:clean" );
- }
-
- allGoals.addAll( goals );
-
- List<String> args = new ArrayList<String>();
-
- int ret;
+ cliArguments.addAll( goals );
+ environmentVariables.putAll( envVars );
+ execute();
+ }
- File logFile = new File( getBasedir(), getLogFileName() );
+ /**
+ * Execute Maven.
+ */
+ public void execute() throws VerificationException
+ {
- for ( String cliArgument : cliArguments )
- {
- args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
- }
+ List<String> args = new ArrayList<>();
Collections.addAll( args, defaultCliArguments );
@@ -1263,12 +1308,22 @@ public class Verifier
args.add( "-Dmaven.repo.local=" + localRepo );
}
- args.addAll( allGoals );
+ if ( autoclean )
+ {
+ args.add( CLEAN_CLI_ARGUMENT );
+ }
- try
+ for ( String cliArgument : cliArguments )
{
+ args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
+ }
- MavenLauncher launcher = getMavenLauncher( envVars );
+ int ret;
+ File logFile = new File( getBasedir(), getLogFileName() );
+
+ try
+ {
+ MavenLauncher launcher = getMavenLauncher( environmentVariables );
String[] cliArgs = args.toArray( new String[0] );
ret = launcher.run( cliArgs, systemProperties, getBasedir(), logFile );
@@ -1284,8 +1339,6 @@ public class Verifier
if ( ret > 0 )
{
- System.err.println( "Exit code: " + ret );
-
throw new VerificationException(
"Exit code was non-zero: " + ret + "; command line and log = \n" + new File( mavenHome,
"bin/mvn" ) + " "
@@ -1697,6 +1750,13 @@ public class Verifier
return autoclean;
}
+ /**
+ * Clean project before execution by adding {@link #CLEAN_CLI_ARGUMENT} to command line.
+ * <p>
+ * By default, options is enabled.
+ *
+ * @param autoclean indicate if option is enabled
+ */
public void setAutoclean( boolean autoclean )
{
this.autoclean = autoclean;
diff --git a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
index 71b1df9..41d805b 100644
--- a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
+++ b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
@@ -100,6 +100,7 @@ public class VerifierTest
Verifier verifier = new Verifier( "src/test/resources" );
verifier.verifyFilePresent( "mshared104.jar!/pom.xml" );
verifier.verifyFileNotPresent( "mshared104.jar!/fud.xml" );
+ verifier.resetStreams();
}
@Test
@@ -115,7 +116,14 @@ public class VerifierTest
{
VerificationException exception = assertThrows( VerificationException.class, () -> {
Verifier verifier = new Verifier( "src/test/resources" );
- verifier.loadProperties( "unknown.properties" );
+ try
+ {
+ verifier.loadProperties( "unknown.properties" );
+ }
+ finally
+ {
+ verifier.resetStreams();
+ }
} );
assertInstanceOf( FileNotFoundException.class, exception.getCause() );
}
@@ -126,6 +134,7 @@ public class VerifierTest
String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();
Verifier verifier = new Verifier( temporaryDir.toString(), null, false, mavenHome );
verifier.executeGoal( "some-goal" );
+ verifier.resetStreams();
Path logFile = Paths.get( verifier.getBasedir(), verifier.getLogFileName() );
ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" );
}
@@ -135,6 +144,7 @@ public class VerifierTest
{
Verifier verifier = new Verifier( "src/test/resources" );
Map<String, String> filterMap = verifier.newDefaultFilterMap();
+ verifier.resetStreams();
assertEquals( 2, filterMap.size() );
assertTrue( filterMap.containsKey( "@basedir@" ) );
@@ -147,6 +157,21 @@ public class VerifierTest
TestVerifier verifier = new TestVerifier( "src/test/resources" );
verifier.executeGoal( "test" );
+ verifier.resetStreams();
+
+ assertThat( verifier.launcher.cliArgs, arrayContaining(
+ "-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
+ "org.apache.maven.plugins:maven-clean-plugin:clean", "test" ) );
+ }
+
+ @Test
+ void testDefaultMavenArgumentWithExecuteMethod() throws VerificationException
+ {
+ TestVerifier verifier = new TestVerifier( "src/test/resources" );
+
+ verifier.addCliArgument( "test" );
+ verifier.execute();
+ verifier.resetStreams();
assertThat( verifier.launcher.cliArgs, arrayContaining(
"-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
@@ -170,6 +195,7 @@ public class VerifierTest
verifier.addCliArgument( inputArgument );
verifier.executeGoal( "test" );
+ verifier.resetStreams();
assertThat( verifier.launcher.cliArgs, hasItemInArray( expectedArgument ) );
}
@@ -181,6 +207,7 @@ public class VerifierTest
verifier.addCliArguments( "cliArg1", "cliArg2" );
verifier.executeGoal( "test" );
+ verifier.resetStreams();
assertThat( verifier.launcher.cliArgs, allOf(
hasItemInArray( "cliArg1" ), hasItemInArray( "cliArg2" ) ) );