Some unit tests in my application are related to finding and managing some file resources that are part of the application itself. I need these tests to run in the actual production settings of the application, where it was deployed as a JAR file, and not as an exploded directory.
How can I instruct Maven to run my unit tests, considering the jar file (and any other declared library dependencies) created by the project as the class path instead of the compiled classes on the file system, as it happens by default?
In other words, right now the class path for my unit tests: /$PROJECTPATH/target/classes/ .
Instead, I would like this classpath to be set: /$PROJECTPATH/target/myjarfile.jar .
I tried to manually add and remove the dependency classes, as described here: http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html , but it still does not work.
My current POM project is as follows:
<?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>org.mygroupid</groupId> <artifactId>myartifact</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> ... </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <configuration> <includeScope>runtime</includeScope> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> <executions> <execution> <id>copy-dependencies</id> <phase>process-resources</phase> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.3</version> <configuration> <classpathDependencyExcludes> <classpathDependencyExclude> ${project.build.outputDirectory} </classpathDependencyExclude> </classpathDependencyExcludes> <additionalClasspathElements> <additionalClasspathElement> ${project.build.directory}/${project.build.finalName}.${project.packaging} </additionalClasspathElement> </additionalClasspathElements> </configuration> </plugin> </plugins> </build> </project>
Thanks in advance for your help!
source share