Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslakknutsen/1155271 to your computer and use it in GitHub Desktop.
Save aslakknutsen/1155271 to your computer and use it in GitHub Desktop.
Arquillian / Surefire - Using Profiles as separation between TestCompile and TestRuntime. Binding Maven Surefire to operate on different TestRuntimes within the same Maven run
[INFO] --- maven-surefire-plugin:2.10-SNAPSHOT:test (jetty) @ arquillian-surefire-test ---
[INFO] Surefire report directory: /home/aslak/dev/source/testing/arquillian-surefire-test/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.jboss.arquillian.test.SimpleTestCase
Aug 18, 2011 11:27:03 PM org.jboss.arquillian.container.impl.client.container.ContainerRegistryCreator getActivatedConfiguration
INFO: Could not read active container configuration: null
Aug 18, 2011 11:27:03 PM org.jboss.arquillian.container.jetty.embedded_7.JettyEmbeddedContainer start
INFO: Starting Jetty Embedded Server 7.1.4.v20100610 [id:344078580]
2011-08-18 23:27:03.762:INFO::jetty-7.1.4.v20100610
2011-08-18 23:27:03.872:INFO::Started SelectChannelConnector@localhost:9090
Aug 18, 2011 11:27:04 PM org.jboss.shrinkwrap.jetty_7.api.ShrinkWrapWebAppContext <init>
INFO: Webapp archive location: file:/tmp/export7665644895419236225e4e77c75-0660-481f-80b8-c3d59d8bdebd.war
2011-08-18 23:27:04.906:INFO::Extract jar:file:/tmp/export7665644895419236225e4e77c75-0660-481f-80b8-c3d59d8bdebd.war!/ to /tmp/Jetty_localhost_9090_export7665644895419236225e4e77c75.0660.481f.80b8.c3d59d8bdebd.war__e4e77c75.0660.481f.80b8.c3d59d8bdebd__gasvsv/webapp
2011-08-18 23:27:04.991:INFO::NO JSP Support for /e4e77c75-0660-481f-80b8-c3d59d8bdebd, did not find org.apache.jasper.servlet.JspServlet
2011-08-18 23:27:05.102:INFO::No Transaction manager found - if your webapp requires one, please configure one.
2011-08-18 23:27:05.123:INFO::WebApp@2030014547 at http://localhost:9090/e4e77c75-0660-481f-80b8-c3d59d8bdebd
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.081 sec
Aug 18, 2011 11:27:05 PM org.jboss.arquillian.container.jetty.embedded_7.JettyEmbeddedContainer stop
INFO: Stopping Jetty Embedded Server [id:344078580]
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-surefire-plugin:2.10-SNAPSHOT:test (tomcat) @ arquillian-surefire-test ---
[INFO] Surefire report directory: /home/aslak/dev/source/testing/arquillian-surefire-test/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.jboss.arquillian.test.SimpleTestCase
Aug 18, 2011 11:27:06 PM org.jboss.arquillian.container.impl.client.container.ContainerRegistryCreator getActivatedConfiguration
INFO: Could not read active container configuration: null
Aug 18, 2011 11:27:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 18, 2011 11:27:07 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service arquillian-tomcat-embedded-7
Aug 18, 2011 11:27:07 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.19
Aug 18, 2011 11:27:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 18, 2011 11:27:08 PM org.apache.catalina.startup.ContextConfig webConfig
INFO: No global web.xml found
Aug 18, 2011 11:27:08 PM org.apache.catalina.realm.JAASRealm setContainer
INFO: Set JAAS app name arquillian-tomcat-embedded-7
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.493 sec
Aug 18, 2011 11:27:09 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8080"]
Aug 18, 2011 11:27:10 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service arquillian-tomcat-embedded-7
Aug 18, 2011 11:27:10 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-8080"]
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
<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>
<groupId>org.jboss.arquillian.surefire</groupId>
<artifactId>arquillian-surefire-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<version.arquillian_core>1.0.0.CR1</version.arquillian_core>
<version.arquillian_jetty>1.0.0.CR1</version.arquillian_jetty>
<version.arquillian_tomcat>1.0.0.Final-SNAPSHOT</version.arquillian_tomcat>
<version.jetty>7.0.2.v20100331</version.jetty>
<version.tomcat>7.0.19</version.tomcat>
<version.jboss_spec>2.0.0.Final</version.jboss_spec>
<version.surefire>2.10-SNAPSHOT</version.surefire>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>${version.arquillian_core}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-build</artifactId>
<version>${version.arquillian_core}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>${version.jboss_spec}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
</dependency>
<!-- Test Compile ClassPath -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- Test Runtime ClassPath -->
<profile>
<id>jetty-runtime</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-jetty-embedded-7</artifactId>
<version>${version.arquillian_jetty}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${version.jetty}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>${version.jetty}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>tomcat-runtime</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-embedded-7</artifactId>
<version>${version.arquillian_tomcat}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${version.tomcat}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${version.tomcat}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${version.tomcat}</version>
</dependency>
</dependencies>
</profile>
<!--
Test Runtime configurations
We create individual Runtime Test Configuration where we configure surefire to use our runtime profiles.
A simple "mvn test" will run all the activateByDefault profiles in one go, that means both jetty and tomcat will be
executed.
Specifically activating one Profile will deactivate the default Profiles. So We can explicitly activate on command line
the profile we want to test if we don't want to run them all:
"mvn test -Pjetty-test"
-->
<profile>
<id>jetty-test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>jetty</id>
<phase>test</phase>
<goals><goal>test</goal></goals>
<configuration>
<additionalProfiles>
<p>jetty-runtime</p>
</additionalProfiles>
<skipTests>false</skipTests>
<reportsDirectory>${project.build.directory}/surefire-reports-jetty</reportsDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>tomcat-test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>tomcat</id>
<phase>test</phase>
<goals><goal>test</goal></goals>
<configuration>
<additionalProfiles>
<p>tomcat-runtime</p>
</additionalProfiles>
<skipTests>false</skipTests>
<reportsDirectory>${project.build.directory}/surefire-reports-tomcat</reportsDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- SkipTest by default, we require some form of runtime to be activated -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
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 f9a8db4..ea6f97c 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
@@ -34,7 +34,10 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.DefaultArtifactRepository;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
@@ -44,12 +47,22 @@ import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Profile;
+import org.apache.maven.model.Repository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.surefire.booterclient.ChecksumCalculator;
import org.apache.maven.plugin.surefire.booterclient.ForkConfiguration;
import org.apache.maven.plugin.surefire.booterclient.ForkStarter;
+import org.apache.maven.profiles.DefaultProfileManager;
+import org.apache.maven.profiles.ProfileManager;
+import org.apache.maven.project.DefaultProjectBuilderConfiguration;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.MavenSettingsBuilder;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.SettingsUtils;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
import org.apache.maven.surefire.booter.Classpath;
@@ -724,6 +737,7 @@ public abstract class AbstractSurefireMojo
checksum.add( getClasspathDependencyExcludes() );
checksum.add( getClasspathDependencyScopeExclude() );
checksum.add( getAdditionalClasspathElements() );
+ checksum.add( getAdditionalProfiles() );
checksum.add( getReportsDirectory() );
checksum.add( getTestSourceDirectory() );
checksum.add( getTest() );
@@ -815,7 +829,65 @@ public abstract class AbstractSurefireMojo
classpath.add( getClassesDirectory().getAbsolutePath() );
- Set classpathArtifacts = getProject().getArtifacts();
+ Set classpathArtifacts;
+ if (getAdditionalProfiles() != null)
+ {
+ try
+ {
+ MavenSession session = getSession();
+ // Reload the Project with the new Profiles applied
+ ProfileManager profileManager = new DefaultProfileManager(getContainer(), session.getSystemProperties());
+ List settingsProfiles = session.getRequest().getProfiles();
+ if ( settingsProfiles != null && !settingsProfiles.isEmpty() )
+ {
+ for ( Iterator it = settingsProfiles.iterator(); it.hasNext(); )
+ {
+ Profile profile = (Profile) it.next();
+ profileManager.addProfile( profile );
+ if(profile.getActivation().isActiveByDefault())
+ {
+ profileManager.explicitlyActivate(profile.getId());
+ for(Iterator repoItr = profile.getRepositories().iterator(); repoItr.hasNext(); )
+ {
+ Repository repo = (Repository)repoItr.next();
+ session.getRequest().addRemoteRepository(
+ new DefaultArtifactRepository(repo.getId(), repo.getUrl(), new DefaultRepositoryLayout()));
+ }
+ }
+ }
+ }
+
+ for( Iterator activeProfileIter = getProject().getModel().getProfiles().iterator(); activeProfileIter.hasNext();)
+ {
+ Profile profile = (Profile) activeProfileIter.next();
+ profileManager.addProfile(profile);
+ }
+
+ for ( Iterator iter = getAdditionalProfiles().iterator(); iter.hasNext(); )
+ {
+ String classpathProfile = (String) iter.next();
+ if ( classpathProfile != null )
+ {
+ profileManager.explicitlyActivate(classpathProfile);
+ }
+ }
+
+ MavenProject newProject = getProjectBuilder().buildWithDependencies(
+ getProject().getFile(),
+ getLocalRepository(),
+ profileManager);
+
+ classpathArtifacts = newProject.getArtifacts();
+ } catch (Exception e) {
+ throw new MojoFailureException("Could not append additional Profiles: " + getAdditionalProfiles(), e);
+ }
+
+ }
+ else
+ {
+ classpathArtifacts = getProject().getArtifacts();
+ }
+
if ( getClasspathDependencyScopeExclude() != null && !getClasspathDependencyScopeExclude().equals( "" ) )
{
@@ -854,7 +926,7 @@ public abstract class AbstractSurefireMojo
}
}
}
-
+
// adding TestNG MethodSelector to the classpath
// Todo: move
if ( getTestNgArtifact() != null )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment