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/11 15:29:57 UTC

[maven-verifier] 01/01: [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 MSHARED-1128
in repository https://gitbox.apache.org/repos/asf/maven-verifier.git

commit 7292a27b8ddfed1d1b05be088c3e5df3047ed1f8
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 | 103 +++++++++++++++------
 .../apache/maven/shared/verifier/VerifierTest.java |  29 +++++-
 2 files changed, 104 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..b5bb870 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;
@@ -1175,18 +1174,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 +1252,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 +1301,25 @@ public class Verifier
             args.add( "-Dmaven.repo.local=" + localRepo );
         }
 
-        args.addAll( allGoals );
+        if ( autoclean )
+        {
+            /*
+             * NOTE: Neither test lifecycle binding nor prefix resolution here but call the goal directly.
+             */
+            args.add( "org.apache.maven.plugins:maven-clean-plugin:clean" );
+        }
+
+        for ( String cliArgument : cliArguments )
+        {
+            args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
+        }
+
+        int ret;
+        File logFile = new File( getBasedir(), getLogFileName() );
 
         try
         {
-
-            MavenLauncher launcher = getMavenLauncher( envVars );
+            MavenLauncher launcher = getMavenLauncher( environmentVariables );
 
             String[] cliArgs = args.toArray( new String[0] );
             ret = launcher.run( cliArgs, systemProperties, getBasedir(), logFile );
@@ -1284,8 +1335,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" ) + " "
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" ) ) );