Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SUREFIRE-790 Added support for execution context propagation #2

Open
wants to merge 1 commit into from

3 participants

Karel Piwko Marcus Holl George Gastaldi
Karel Piwko

No description provided.

Marcus Holl

We also need the possibility to use the settings file with the active profiles from a surrounding maven call inside a maven process forked by surefire via the org.apache.maven.it.Verifier. This is from my point of view a valid use case which is currently apparently not supported.
For build systems it is quite usual to use dedicated local repositories defined via -Dmaven.repo.local on the command line of the outermost maven call. Maybe you should consider to hand over that property also.

asfgit asfgit referenced this pull request from a commit
Hervé Boutemy hboutemy svnpubsub configuration simplification #2: use mvn site site:stage
instead mvn site-deploy

BTW, this has others advantages:
- easier to understand (it's a local site staging, not a site deployment
to remote hosting)
- the distributionManagement entry has the real value, not a temporary
local storage location (which appears in Distribution Management report)
c7831f6
George Gastaldi

I think the default value for executionContextNamespace should be: org.apache.maven

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
37 maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/IntegrationTestMojo.java
View
@@ -607,6 +607,23 @@
private MavenSession session;
/**
+ * Propagate information about current Maven execution into the tests via a set of System properties.
+ * Following properties are available:
+ * "offline", "user-setttings", "global-settings", "active-profiles" and "pom"
+ *
+ * @parameter default-value="false"
+ */
+ private boolean propagateExecutionContext;
+
+ /**
+ * Define a namespace where Maven execution System properties are stored. Does not apply if if
+ * <b>propagateExecutionContext</b> is not set.
+ *
+ * @parameter default-value="maven.execution"
+ */
+ private String executionContextNamespace;
+
+ /**
* (TestNG only) Define the factory class used to create all test instances.
*
* @parameter expression="${objectFactory}"
@@ -1394,4 +1411,24 @@ protected void addPluginSpecificChecksumItems( ChecksumCalculator checksum )
checksum.add( summaryFile );
}
+ public boolean isPropagateExecutionContext()
+ {
+ return propagateExecutionContext;
+ }
+
+ public void setPropagateExecutionContext( boolean propagateExecutionContext )
+ {
+ this.propagateExecutionContext = propagateExecutionContext;
+ }
+
+ public String getExecutionContextNamespace()
+ {
+ return executionContextNamespace;
+ }
+
+ public void setExecuctionContextNamespace( String executionContextNamespace )
+ {
+ this.executionContextNamespace = executionContextNamespace;
+ }
+
}
75 maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
View
@@ -1027,6 +1027,14 @@ void processSystemProperties( boolean setInSystem )
copyPropertiesToInternalSystemProperties( getUserProperties() );
+ // Include execution context properties
+ // This feature allows test to get some of the information from the process
+ // which executed them, see SUREFIRE-790
+ if ( isPropagateExecutionContext() )
+ {
+ copyPropertiesToInternalSystemProperties( getMavenExecutionProperties() );
+ }
+
getInternalSystemProperties().setProperty( "basedir", getBasedir().getAbsolutePath() );
getInternalSystemProperties().setProperty( "user.dir", getWorkingDirectory().getAbsolutePath() );
getInternalSystemProperties().setProperty( "localRepository", getLocalRepository().getBasedir() );
@@ -1089,6 +1097,73 @@ private Properties getUserProperties()
return props;
}
+ private Properties getMavenExecutionProperties()
+ {
+ Properties props = new Properties();
+ try
+ {
+ // try calling MavenSession.getRequest() from Maven 3.0.0+
+ Method getRequest = getSession().getClass().getMethod( "getRequest", null );
+ Object request = getRequest.invoke( getSession(), null );
+
+ // try getting pom file, settings.xml file, offline flag and active profiles
+ Method isOffline = request.getClass().getMethod("isOffline", null);
+ Method getUserSettingsFile = request.getClass().getMethod("getUserSettingsFile", null);
+ Method getGlobalSettingsFile = request.getClass().getMethod("getGlobalSettingsFile", null);
+
+ File pomFile = getSession().getCurrentProject().getFile();
+ File userSettingsFile = (File) getUserSettingsFile.invoke(request, null);
+ File globalSettingsFile = (File) getGlobalSettingsFile.invoke(request, null);
+ Boolean offline = (Boolean) isOffline.invoke(request, null);
+ List activeProfiles = getSession().getCurrentProject().getActiveProfiles();
+
+ getLog().debug("Adding Maven Execution properties to Surefire execution.");
+
+ // FIXME allow prefix here via configuration
+ String namespace = getExecutionContextNamespace();
+ if ( offline != null)
+ {
+ storeExecutionContextProperty( props, namespace, "offline", offline.toString() );
+ }
+ if ( pomFile != null)
+ {
+ storeExecutionContextProperty( props, namespace, "pom", pomFile.getAbsolutePath() );
+ }
+ if ( globalSettingsFile != null)
+ {
+ storeExecutionContextProperty( props, namespace, "global-settings", globalSettingsFile.getAbsolutePath() );
+ }
+ if ( userSettingsFile != null)
+ {
+ storeExecutionContextProperty( props, namespace, "user-settings", userSettingsFile.getAbsolutePath() );
+ }
+ if ( activeProfiles != null && activeProfiles.size() > 0)
+ {
+ storeExecutionContextProperty( props, namespace, "active-profiles", StringUtils.join(activeProfiles.iterator(), ",") );
+ }
+ }
+ catch ( Exception e)
+ {
+ String msg = "Build does uses Maven prior 3.0.0, cannot propagate Maven Execution properties" +
+ " from parent process (cf. SUREFIRE-790)";
+ if ( getLog().isDebugEnabled() )
+ {
+ getLog().warn( msg, e );
+ }
+ else
+ {
+ getLog().warn( msg );
+ }
+ }
+ return props;
+ }
+
+ private void storeExecutionContextProperty(Properties properties, String namespace, String key, String value)
+ {
+ properties.put(namespace + "." + key, value);
+ getLog().debug("Added execution context property: " + key + "=" + value);
+ }
+
void ensureWorkingDirectoryExists()
throws MojoFailureException
8 maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireExecutionParameters.java
View
@@ -283,5 +283,13 @@
void setRunOrder( String runOrder );
String getRunOrder();
+
+ boolean isPropagateExecutionContext();
+
+ void setPropagateExecutionContext( boolean propagateExecutionContext );
+
+ String getExecutionContextNamespace();
+
+ void setExecuctionContextNamespace( String executionContextNamespace );
}
37 maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java
View
@@ -575,6 +575,23 @@
private MavenSession session;
/**
+ * Propagate information about current Maven execution into the tests via a set of System properties.
+ * Following properties are available:
+ * "offline", "user-setttings", "global-settings", "active-profiles" and "pom"
+ *
+ * @parameter default-value="false"
+ */
+ private boolean propagateExecutionContext;
+
+ /**
+ * Define a namespace where Maven execution System properties are stored. Does not apply if if
+ * <b>propagateExecutionContext</b> is not set.
+ *
+ * @parameter default-value="maven.execution"
+ */
+ private String executionContextNamespace;
+
+ /**
* (TestNG only) Define the factory class used to create all test instances.
*
* @parameter expression="${objectFactory}"
@@ -1315,4 +1332,24 @@ protected void addPluginSpecificChecksumItems( ChecksumCalculator checksum )
{
}
+ public boolean isPropagateExecutionContext()
+ {
+ return propagateExecutionContext;
+ }
+
+ public void setPropagateExecutionContext( boolean propagateExecutionContext )
+ {
+ this.propagateExecutionContext = propagateExecutionContext;
+ }
+
+ public String getExecutionContextNamespace()
+ {
+ return executionContextNamespace;
+ }
+
+ public void setExecuctionContextNamespace( String executionContextNamespace )
+ {
+ this.executionContextNamespace = executionContextNamespace;
+ }
+
}
43 surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/ExecutionPropagationTestIT.java
View
@@ -0,0 +1,43 @@
+package org.apache.maven.surefire.its;
+/*
+ * 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.
+ */
+
+/**
+ * Test maven execution propagation
+ *
+ * @author <a href="http://community.jboss.org/people/kpiwko">Karel Piwko</a>
+ */
+public class ExecutionPropagationTestIT
+ extends SurefireVerifierTestClass
+{
+
+ public ExecutionPropagationTestIT()
+ {
+ super( "/surefire-790-propagate-execution-context" );
+ }
+
+ public void testExecutionPropagation()
+ throws Exception
+ {
+ executeTest();
+ verifyErrorFreeLog();
+ assertTestSuiteResults( 1, 0, 0, 0 );
+ }
+
+}
37 surefire-integration-tests/src/test/resources/surefire-790-propagate-execution-context/pom.xml
View
@@ -0,0 +1,37 @@
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.plugins.surefire</groupId>
+ <artifactId>junit4-test</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>surefire-747-parallelo-method-skips-test</name>
+ <url>http://maven.apache.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.version}</version>
+ <configuration>
+ <propagateExecutionContext>true</propagateExecutionContext>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
13 ...c/test/resources/surefire-790-propagate-execution-context/src/test/java/surefire790/PropagationTest.java
View
@@ -0,0 +1,13 @@
+package surefire790;
+
+import junit.framework.TestCase;
+
+public class PropagationTest
+ extends TestCase
+{
+
+ public void testPropertyPropagation()
+ {
+ assertNotNull( "property pom not set", System.getProperty( "maven.execution.pom" ) );
+ }
+}
Something went wrong with that request. Please try again.