Configuration using Maven Plugin
Pax Exam 0.4.0 introduces configuration of tests entirely inside your pom.xml using all maven benefits like dependency-management, inheritance and using variables for identifiers.
Beware, goal names and configuration has been slightly changed between 0.4 and 0.5 release.
The Plugin maven-paxexam-plugin has two possible goals:
- generate-depends-file
- generate-config
Manage only versions with maven: generate-depends-file
(Since 0.6)
When using the "mavenBundle" factory to construct maven managed dependencies as so:
mavenBundle().artifactId( "pax-logging-api" ).groupId( "org.ops4j.pax.logging" ).version("1.3.0")
it might be unattractive to define dependency versions hard-coded in you test code.
Instead the api allows setting the version part as like so:
mavenBundle().artifactId( "pax-logging-api" ).groupId( "org.ops4j.pax.logging" ).versionAsInProject()
This "versionAsInProject" refers to a version that you refered already in your project pom.xml:
<dependency> <groupId>org.ops4j.pax.logging</groupId> <artifactId>pax-logging-api</artifactId> <version>1.3.0</version> </dependency>
To let Pax Exam know about this information you need to add a small plugin to your build definition in this pom:
<plugin> <groupId>org.ops4j.pax.exam</groupId> <artifactId>maven-paxexam-plugin</artifactId> <executions> <execution> <id>generate-config</id> <goals> <goal>generate-depends-file</goal> </goals> </execution> </executions> </plugin>
Technical Details
This plugin will generate a small descriptorfile to target/classes/META-INF/maven/dependencies.properties_
before launching the test.
It will contain all - even transient - maven dependencies as a flat, static properties file.
This way, pax exam can load the version information at runtime directly from classpath (see target/classes folder).
Fully configure test inside pom.xml: generate-config
Your test can now look like this:
@RunWith( MavenConfiguredJUnit4TestRunner.class ) public class NewPaxExamFeature { @Inject BundleContext context; @Test public void use() { System.out.println( "Hello World from " + context.getBundle().getSymbolicName() ); } }
Configuration in Pax Exam 0.5.0
With a pom that looks like this:
<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>com.company</groupId> <artifactId>we-try-pax-exam-features</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>OPS4J Pax Exam Feature Test</name> <!-- we can use full maven properties incl. inheritance ! --> <properties> <target-framework>equinox</target-framework> <dependency.pax.web.version>0.5.2</dependency.pax.web.version> </properties> <dependencies> <!-- normal dependencies required at compile time --> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-junit</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-container-default</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <!-- Here's the stuff we want to provision our osgi runtime with --> <dependency> <groupId>org.ops4j.pax.web</groupId> <artifactId>pax-web-service</artifactId> <version>${dependency.pax.web.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- use pax exam maven plugin --> <plugin> <groupId>org.ops4j.pax.exam</groupId> <artifactId>maven-paxexam-plugin</artifactId> <executions> <execution> <id>generate-config</id> <goals> <goal>generate-config</goal> </goals> </execution> </executions> <configuration> <options> <platform>${target-framework}</platform> <profiles>log,url</profiles> </options> <settings> <dependency_options>org.ops4j.pax.web:pax-web-service@7</dependency_options> </settings> </configuration> </plugin> </plugins> </build> </project>
Settings vs. Options
Options
The content of options are raw pax runner arguments (hence the name options).
So, you can specify any pax runner option here even if its unsupported from the pax exam api.
Settings
dependency_options |
a comma separed list of options referencing the GA part of your dependencies. |
configOutput (outputFile in Version < 0.6.0) |
file that this plugin will write. Default is: (project.build.directory)/test-classes/META-INF/maven/paxexam-config.args |
(Which also means that the plugin currently only runs with the default-platform, which is based on pax runner)
Configuration in Pax Exam 0.4.0
With a pom that looks like this:
<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>com.company</groupId> <artifactId>we-try-pax-exam-features</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>OPS4J Pax Exam Feature Test</name> <!-- we can use full maven properties incl. inheritance ! --> <properties> <target-framework>equinox</target-framework> <dependency.pax.web.version>0.5.2</dependency.pax.web.version> </properties> <dependencies> <!-- normal dependencies required at compile time --> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-junit</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-container-default</artifactId> <version>0.4.0-SNAPSHOT</version> </dependency> <!-- Here's the stuff we want to provision our osgi runtime with --> <dependency> <groupId>org.ops4j.pax.web</groupId> <artifactId>pax-web-service</artifactId> <version>${dependency.pax.web.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- use pax exam maven plugin --> <plugin> <groupId>org.ops4j.pax.exam</groupId> <artifactId>maven-paxexam-plugin</artifactId> <executions> <execution> <id>generate-paxexam-config</id> <goals> <goal>generate-paxexam-config</goal> </goals> </execution> </executions> <configuration> <settings> <platform>${target-framework}</platform> <profiles>log,url</profiles> </settings> </configuration> </plugin> </plugins> </build> </project>
The content of settings are raw pax runner arguments.
So, you can specify any pax runner option here even if its unsupported from the pax exam api.
(Which also means that the plugin currently only runs with the default-platform, which is based on pax runner)
Be sure to run maven (mvn compile at least) before running the sample in your IDE because the plugin must be triggered. (Some IDEs support maven runs as IDE "compile prior test")
Mix Maven and API Settings
It is ok to mix maven settings with api settings. Maven settings are being overwritten by API settings.
If you have the plugin set up like above, you must "use" those settings like so:
@RunWith( JUnit4TestRunner.class ) public class NewPaxExamFeature { @Inject BundleContext context; @Configuration public static Option[] config() { return options( mavenConfiguration(), equinox() ); } @Test public void use() { System.out.println( "Hello World from " + context.getBundle().getSymbolicName() ); } }
See, you use the normal RunWith() annotation like you would use without maven support.
But you manually invoke the maven settings via the mavenConfiguration() option.
You can specify all kinds of additional options additionally (here we hard-coded equinox()).
The order of options does not change the api option priority (as it is defined by Pax Runner).