You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/04/18 02:20:40 UTC

[maven-surefire] branch pull/285 created (now fcb67de)

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a change to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git.


      at fcb67de  finished the impl

This branch includes the following new commits:

     new 9a29204  Adding toolchains support like compiler plugin
     new fc1d3db  add docs, change to ReflectionUtils
     new a6bed67  minor corrections
     new fcb67de  finished the impl

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-surefire] 01/04: Adding toolchains support like compiler plugin

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 9a29204dc57b20078f6c75e6f33f1d702aa98e17
Author: Akom <re...@akom.net>
AuthorDate: Fri Apr 17 11:38:23 2020 -0400

    Adding toolchains support like compiler plugin
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 80 +++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index b26e981..ba8b38d 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -97,6 +97,8 @@ import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
 import javax.annotation.Nonnull;
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -765,6 +767,40 @@ public abstract class AbstractSurefireMojo
     private String[] dependenciesToScan;
 
     /**
+     * <p>
+     *     Allow for configuration of the test jvm via maven toolchains.
+     *     This permits a configuration where the project is built with one jvm and tested with another.
+     *     This is similar to {@link #jvm}, but avoids hardcoding paths.
+     *     The two parameters are mutually exclusive (jvm wins)
+     * </p>
+     *
+     * <p>Examples:</p>
+     * (see <a href="https://maven.apache.org/guides/mini/guide-using-toolchains.html">
+     *     Guide to Toolchains</a> for more info)
+     *
+     * <pre>
+     * {@code
+     *    <configuration>
+     *        ...
+     *        <jdkToolchain>
+     *            <version>1.11</version>
+     *        </jdkToolchain>
+     *    </configuration>
+     *
+     *    <configuration>
+     *        ...
+     *        <jdkToolchain>
+     *            <version>1.8</version>
+     *            <vendor>zulu</vendor>
+     *        </jdkToolchain>
+     *    </configuration>
+     *    }
+     * </pre>
+     */
+    @Parameter
+    private Map<String, String> jdkToolchain;
+
+    /**
      *
      */
     @Component
@@ -909,6 +945,48 @@ public abstract class AbstractSurefireMojo
         return consoleLogger;
     }
 
+
+    //TODO remove the part with ToolchainManager lookup once we depend on
+    //3.0.9 (have it as prerequisite). Define as regular component field then.
+    //This code duplicates AbstractCompilerMojo in maven-compiler-plugin
+    protected final Toolchain getToolchain()
+    {
+        Toolchain tc = null;
+
+        if ( jdkToolchain != null )
+        {
+            // Maven 3.3.1 has plugin execution scoped Toolchain Support
+            try
+            {
+                Method getToolchainsMethod =
+                    toolchainManager.getClass().getMethod( "getToolchains", MavenSession.class, String.class,
+                        Map.class );
+
+                @SuppressWarnings( "unchecked" )
+                List<Toolchain> tcs =
+                    (List<Toolchain>) getToolchainsMethod.invoke( toolchainManager, getSession(), "jdk",
+                        jdkToolchain );
+
+                if ( tcs != null && !tcs.isEmpty() )
+                {
+                    tc = tcs.get( 0 );
+                }
+            }
+            catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
+                | InvocationTargetException e )
+            {
+                // ignore
+            }
+        }
+
+        if ( tc == null )
+        {
+            tc = toolchainManager.getToolchainFromBuildContext( "jdk", getSession() );
+        }
+
+        return tc;
+    }
+
     private void setupStuff()
     {
         surefireDependencyResolver = new SurefireDependencyResolver( getRepositorySystem(),
@@ -925,7 +1003,7 @@ public abstract class AbstractSurefireMojo
 
         if ( getToolchainManager() != null )
         {
-            toolchain = getToolchainManager().getToolchainFromBuildContext( "jdk", getSession() );
+            toolchain = getToolchain();
         }
     }
 


[maven-surefire] 02/04: add docs, change to ReflectionUtils

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit fc1d3db1dd2bbe21c219f237bf7fb4a6d973152d
Author: Akom <re...@akom.net>
AuthorDate: Fri Apr 17 14:16:12 2020 -0400

    add docs, change to ReflectionUtils
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 21 ++++----
 .../src/site/apt/examples/toolchains.apt.vm        | 56 ++++++++++++++++++++++
 maven-surefire-plugin/src/site/markdown/java9.md   |  2 +
 maven-surefire-plugin/src/site/site.xml            |  1 +
 4 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index ba8b38d..a67faca 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -85,6 +85,7 @@ import org.apache.maven.surefire.testset.TestListResolver;
 import org.apache.maven.surefire.testset.TestRequest;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 import org.apache.maven.surefire.util.DefaultScanResult;
+import org.apache.maven.surefire.util.ReflectionUtils;
 import org.apache.maven.surefire.util.RunOrder;
 import org.apache.maven.toolchain.DefaultToolchain;
 import org.apache.maven.toolchain.Toolchain;
@@ -97,7 +98,6 @@ import org.codehaus.plexus.languages.java.jpms.ResolvePathsResult;
 import javax.annotation.Nonnull;
 import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.nio.file.Files;
@@ -796,6 +796,8 @@ public abstract class AbstractSurefireMojo
      *    </configuration>
      *    }
      * </pre>
+     *
+     * @since 3.0.0-M5 and Maven 3.3.x
      */
     @Parameter
     private Map<String, String> jdkToolchain;
@@ -955,27 +957,20 @@ public abstract class AbstractSurefireMojo
 
         if ( jdkToolchain != null )
         {
-            // Maven 3.3.1 has plugin execution scoped Toolchain Support
             try
             {
-                Method getToolchainsMethod =
-                    toolchainManager.getClass().getMethod( "getToolchains", MavenSession.class, String.class,
-                        Map.class );
-
-                @SuppressWarnings( "unchecked" )
+                Method getToolchainsMethod = ReflectionUtils.getMethod( getToolchainManager(), "getToolchains" );
                 List<Toolchain> tcs =
-                    (List<Toolchain>) getToolchainsMethod.invoke( toolchainManager, getSession(), "jdk",
-                        jdkToolchain );
-
+                    (List<Toolchain>) ReflectionUtils.invokeMethodWithArray( getToolchainManager(),
+                        getToolchainsMethod, getSession(), "jdk" );
                 if ( tcs != null && !tcs.isEmpty() )
                 {
                     tc = tcs.get( 0 );
                 }
             }
-            catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
-                | InvocationTargetException e )
+            catch ( Exception x )
             {
-                // ignore
+                //ignore
             }
         }
 
diff --git a/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm b/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm
new file mode 100644
index 0000000..3310209
--- /dev/null
+++ b/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm
@@ -0,0 +1,56 @@
+ ------
+ Using Maven Toolchains
+ ------
+ Akom <akom>
+ ------
+ 2020-04-17
+ ------
+
+~~ 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.
+
+~~ NOTE: For help with the syntax of this file, see:
+~~ http://maven.apache.org/doxia/references/apt-format.html
+
+    For general information about Maven Toolchains, see
+    {{{https://maven.apache.org/guides/mini/guide-using-toolchains.html}Guide to Using Toolchains}}
+
+
+Using Maven Toolchains with Surefire/Failsafe.
+
+    By default, if the pom configures the toolchains plugin as specified in the aforementioned
+    guide, surefire and failsafe will launch the test jvm using the main toolchain
+    configured in maven.
+
+    In some cases, it may be desirable to compile and test using different jvms.
+    While the <<<jvm>>> option can achieve this, it requires hardcoding system-specific paths.
+    Configuration option <<<jdkToolchain>>> can be used to specify an alternate toolchain specification.
+
+* Configuring a different jvm for running tests using toolchains
+
++---+
+<configuration>
+    [...]
+    <jdkToolchain>
+        <version>1.11</version>
+        <vendor>zulu</vendor>
+    </jdkToolchain>
+    [...]
+</configuration>
++---+
+
+         The above example assumes that your toolchains.xml contains a valid entry with these values
diff --git a/maven-surefire-plugin/src/site/markdown/java9.md b/maven-surefire-plugin/src/site/markdown/java9.md
index 4ba2567..17157ab 100644
--- a/maven-surefire-plugin/src/site/markdown/java9.md
+++ b/maven-surefire-plugin/src/site/markdown/java9.md
@@ -125,3 +125,5 @@ Your POM should specify the plugin which activates only particular JDK in *toolc
     </plugin>
 
 Now you can run the build with tests on the top of Java 9.
+
+Also see the [full documentation for surefire toolchains](examples/toolchains.html) configuration options.
diff --git a/maven-surefire-plugin/src/site/site.xml b/maven-surefire-plugin/src/site/site.xml
index 2cec467..cbee436 100644
--- a/maven-surefire-plugin/src/site/site.xml
+++ b/maven-surefire-plugin/src/site/site.xml
@@ -62,6 +62,7 @@
       <item name="Shutdown of Forked JVM" href="examples/shutdown.html"/>
       <item name="Run tests with Java 9" href="java9.html"/>
       <item name="Run tests in Docker" href="docker.html"/>
+      <item name="Run tests in a different JVM using toolchains" href="examples/toolchains.html"/>
     </menu>
   </body>
 </project>


[maven-surefire] 04/04: finished the impl

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit fcb67de7c6ccb8b1075478b082e099bc70eb2195
Author: tibordigana <ti...@apache.org>
AuthorDate: Sat Apr 18 04:20:33 2020 +0200

    finished the impl
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 36 ++++++++++++++--------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index ec79ed1..55127d9 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -85,7 +85,6 @@ import org.apache.maven.surefire.testset.TestListResolver;
 import org.apache.maven.surefire.testset.TestRequest;
 import org.apache.maven.surefire.testset.TestSetFailedException;
 import org.apache.maven.surefire.util.DefaultScanResult;
-import org.apache.maven.surefire.util.ReflectionUtils;
 import org.apache.maven.surefire.util.RunOrder;
 import org.apache.maven.toolchain.DefaultToolchain;
 import org.apache.maven.toolchain.Toolchain;
@@ -143,6 +142,8 @@ import static org.apache.maven.surefire.booter.SystemUtils.toJdkHomeFromJvmExec;
 import static org.apache.maven.surefire.booter.SystemUtils.toJdkVersionFromReleaseFile;
 import static org.apache.maven.surefire.suite.RunResult.failure;
 import static org.apache.maven.surefire.suite.RunResult.noTestsRun;
+import static org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray;
+import static org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod;
 
 /**
  * Abstract base class for running tests using Surefire.
@@ -947,38 +948,37 @@ public abstract class AbstractSurefireMojo
         return consoleLogger;
     }
 
-
     //TODO remove the part with ToolchainManager lookup once we depend on
     //3.0.9 (have it as prerequisite). Define as regular component field then.
-    //This code duplicates AbstractCompilerMojo in maven-compiler-plugin
-    protected final Toolchain getToolchain() throws MojoFailureException
+    private Toolchain getToolchain() throws MojoFailureException
     {
         Toolchain tc = null;
 
-        if ( jdkToolchain != null )
+        if ( getJdkToolchain() != null )
         {
-            Method getToolchainsMethod = ReflectionUtils.tryGetMethod(
-                getToolchainManager().getClass(), "getToolchains" );
+            Method getToolchainsMethod = tryGetMethod( ToolchainManager.class, "getToolchains",
+                MavenSession.class, String.class, Map.class );
             if ( getToolchainsMethod != null )
             {
-                List<Toolchain> tcs =
-                    (List<Toolchain>) ReflectionUtils.invokeMethodWithArray( getToolchainManager(),
-                        getToolchainsMethod, getSession(), "jdk" );
-                if ( tcs != null && !tcs.isEmpty() )
+                //noinspection unchecked
+                List<Toolchain> tcs = (List<Toolchain>) invokeMethodWithArray( getToolchainManager(),
+                    getToolchainsMethod, getSession(), "jdk", getJdkToolchain() );
+                if ( !tcs.isEmpty() )
                 {
                     tc = tcs.get( 0 );
                 }
                 else
                 {
                     throw new MojoFailureException(
-                        "Requested toolchain specification did not match any configured toolchain: " + jdkToolchain );
+                        "Requested toolchain specification did not match any configured toolchain: "
+                            + getJdkToolchain() );
                 }
             }
         }
 
         if ( tc == null )
         {
-            tc = toolchainManager.getToolchainFromBuildContext( "jdk", getSession() );
+            tc = getToolchainManager().getToolchainFromBuildContext( "jdk", getSession() );
         }
 
         return tc;
@@ -3940,6 +3940,16 @@ public abstract class AbstractSurefireMojo
         SurefireHelper.logDebugOrCliShowErrors( s, getConsoleLogger(), cli );
     }
 
+    public Map<String, String> getJdkToolchain()
+    {
+        return jdkToolchain;
+    }
+
+    public void setJdkToolchain( Map<String, String> jdkToolchain )
+    {
+        this.jdkToolchain = jdkToolchain;
+    }
+
     public String getTempDir()
     {
         return tempDir;


[maven-surefire] 03/04: minor corrections

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch pull/285
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit a6bed67a78d950e078210840d16b532ffa2d2209
Author: Akom <re...@akom.net>
AuthorDate: Fri Apr 17 19:53:23 2020 -0400

    minor corrections
---
 .../maven/plugin/surefire/AbstractSurefireMojo.java    | 18 ++++++++++--------
 .../src/site/apt/examples/toolchains.apt.vm            | 10 +++++-----
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index a67faca..ec79ed1 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -951,15 +951,16 @@ public abstract class AbstractSurefireMojo
     //TODO remove the part with ToolchainManager lookup once we depend on
     //3.0.9 (have it as prerequisite). Define as regular component field then.
     //This code duplicates AbstractCompilerMojo in maven-compiler-plugin
-    protected final Toolchain getToolchain()
+    protected final Toolchain getToolchain() throws MojoFailureException
     {
         Toolchain tc = null;
 
         if ( jdkToolchain != null )
         {
-            try
+            Method getToolchainsMethod = ReflectionUtils.tryGetMethod(
+                getToolchainManager().getClass(), "getToolchains" );
+            if ( getToolchainsMethod != null )
             {
-                Method getToolchainsMethod = ReflectionUtils.getMethod( getToolchainManager(), "getToolchains" );
                 List<Toolchain> tcs =
                     (List<Toolchain>) ReflectionUtils.invokeMethodWithArray( getToolchainManager(),
                         getToolchainsMethod, getSession(), "jdk" );
@@ -967,10 +968,11 @@ public abstract class AbstractSurefireMojo
                 {
                     tc = tcs.get( 0 );
                 }
-            }
-            catch ( Exception x )
-            {
-                //ignore
+                else
+                {
+                    throw new MojoFailureException(
+                        "Requested toolchain specification did not match any configured toolchain: " + jdkToolchain );
+                }
             }
         }
 
@@ -982,7 +984,7 @@ public abstract class AbstractSurefireMojo
         return tc;
     }
 
-    private void setupStuff()
+    private void setupStuff() throws MojoFailureException
     {
         surefireDependencyResolver = new SurefireDependencyResolver( getRepositorySystem(),
                 getConsoleLogger(), getLocalRepository(),
diff --git a/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm b/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm
index 3310209..891f8b1 100644
--- a/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm
+++ b/maven-surefire-plugin/src/site/apt/examples/toolchains.apt.vm
@@ -30,15 +30,15 @@
     {{{https://maven.apache.org/guides/mini/guide-using-toolchains.html}Guide to Using Toolchains}}
 
 
-Using Maven Toolchains with Surefire/Failsafe.
+Using Maven Toolchains with ${thisPlugin}.
 
     By default, if the pom configures the toolchains plugin as specified in the aforementioned
-    guide, surefire and failsafe will launch the test jvm using the main toolchain
-    configured in maven.
+    guide, ${thisPlugin} will launch the test jvm using the main toolchain
+    configured in Maven.
 
     In some cases, it may be desirable to compile and test using different jvms.
     While the <<<jvm>>> option can achieve this, it requires hardcoding system-specific paths.
-    Configuration option <<<jdkToolchain>>> can be used to specify an alternate toolchain specification.
+    Configuration option <<<jdkToolchain>>> can be used to supply an alternate toolchain specification.
 
 * Configuring a different jvm for running tests using toolchains
 
@@ -53,4 +53,4 @@ Using Maven Toolchains with Surefire/Failsafe.
 </configuration>
 +---+
 
-         The above example assumes that your toolchains.xml contains a valid entry with these values
+         The above example assumes that your toolchains.xml contains a valid entry with these values.