[SUREFIRE-1385] System properties defined in the Surefire and Failsafe plugin configuration should override user properties

When building the Surefire properties, user properties should be set first so that the more direct configuration of systemPropertyVariables and systemPropertyFile can possibly override them.
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java
index fd2b4fb..4b13898 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireProperties.java
@@ -143,19 +143,22 @@
                                                             Properties userProperties, SurefireProperties props )
     {
         SurefireProperties result = new SurefireProperties();
+
+        // SUREFIRE-1385: Make sure user properties are set first, so that system properties
+        // configured in the POM or properties file take precedence
+        
+        // We used to take all of our system properties and dump them in with the
+        // user specified properties for SUREFIRE-121, causing SUREFIRE-491.
+        // Not gonna do THAT any more... instead, we only propagate those system properties
+        // that have been explicitly specified by the user via -Dkey=value on the CLI.
+        result.copyPropertiesFrom( userProperties );
+
         result.copyPropertiesFrom( systemProperties );
 
         result.copyPropertiesFrom( props );
 
         copyProperties( result, systemPropertyVariables );
-        copyProperties( result, systemPropertyVariables );
 
-        // We used to take all of our system properties and dump them in with the
-        // user specified properties for SUREFIRE-121, causing SUREFIRE-491.
-        // Not gonna do THAT any more... instead, we only propagate those system properties
-        // that have been explicitly specified by the user via -Dkey=value on the CLI
-
-        result.copyPropertiesFrom( userProperties );
         return result;
     }
 
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefirePropertiesTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefirePropertiesTest.java
index af9ee75..6f727b0 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefirePropertiesTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefirePropertiesTest.java
@@ -19,6 +19,8 @@
  */
 
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import junit.framework.TestCase;
@@ -75,4 +77,23 @@
 
     }
 
+    public void testCalculateEffectiveProperties_SystemPropertyVariablesOverrideUserProperties()
+    {
+        Properties userProperties = new Properties();
+        userProperties.setProperty( "prop2", "user-property-2" );
+        userProperties.setProperty( "prop3", "user-property-3" );
+
+        Map<String, String> systemPropertyVariables = new HashMap<String, String>( 2 );
+        systemPropertyVariables.put( "prop1", "system-property-variable-1" );
+        systemPropertyVariables.put( "prop2", "system-property-variable-2" );
+
+        SurefireProperties surefireProperties =
+            SurefireProperties.calculateEffectiveProperties( null, systemPropertyVariables, userProperties, null );
+
+        assertEquals( 3, surefireProperties.size() );
+        assertEquals( "system-property-variable-1", surefireProperties.get( "prop1" ) );
+        assertEquals( "system-property-variable-2", surefireProperties.get( "prop2" ) );
+        assertEquals( "user-property-3", surefireProperties.get( "prop3" ) );
+    }
+
 }
diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1385SystemPropertyVariablesOverrideIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1385SystemPropertyVariablesOverrideIT.java
new file mode 100644
index 0000000..b82e603
--- /dev/null
+++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1385SystemPropertyVariablesOverrideIT.java
@@ -0,0 +1,45 @@
+package org.apache.maven.surefire.its.jiras;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
+import org.apache.maven.surefire.its.fixture.SurefireLauncher;
+import org.junit.Test;
+
+/**
+ * @author gboue
+ * @since 2.20.1
+ */
+public class Surefire1385SystemPropertyVariablesOverrideIT
+        extends SurefireJUnit4IntegrationTestCase
+{
+
+    @Test
+    public void systemPropertyVariablesShouldOverrideUserProperties()
+    {
+        unpack().sysProp( "prop-2", "from-cli-2" ).sysProp( "prop-3", "from-cli-3" ).executeTest().verifyErrorFree( 3 );
+    }
+
+    private SurefireLauncher unpack()
+    {
+        return unpack( "/surefire-1385-system-property-variables-override" );
+    }
+
+}
diff --git a/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/pom.xml b/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/pom.xml
new file mode 100644
index 0000000..4ea06c3
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.surefire</groupId>
+    <artifactId>it-parent</artifactId>
+    <version>1.0</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <groupId>org.apache.maven.plugins.surefire</groupId>
+  <artifactId>surefire-1385</artifactId>
+  <version>1.0</version>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.12</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <prop-1>from-system-property-variable-1</prop-1>
+            <prop-2>from-system-property-variable-2</prop-2>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/src/test/java/ATest.java b/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/src/test/java/ATest.java
new file mode 100644
index 0000000..10ecdb3
--- /dev/null
+++ b/surefire-integration-tests/src/test/resources/surefire-1385-system-property-variables-override/src/test/java/ATest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ATest
+{
+
+    @Test
+    public void testProp1()
+    {
+        assertEquals( "from-system-property-variable-1", System.getProperty( "prop-1" ) );
+    }
+
+    @Test
+    public void testProp2()
+    {
+        assertEquals( "from-system-property-variable-2", System.getProperty( "prop-2" ) );
+    }
+
+    @Test
+    public void testProp3()
+    {
+        assertEquals( "from-cli-3", System.getProperty( "prop-3" ) );
+    }
+}