Re: [maven-and-scala] classpath issue in multi-module project?

418 views
Skip to first unread message

Martin Grigorov

unread,
Apr 12, 2013, 8:03:42 AM4/12/13
to maven-a...@googlegroups.com
Hi,


On Fri, Apr 12, 2013 at 2:48 PM, Adam Retter <adam....@googlemail.com> wrote:
I am trying to build a multi-module Maven project with the net.alchim31.maven:scala-maven-plugin:3.1.3 and am having classpath issues when compared to a single module project.

My first project layout looked like - 

project/pom.xml
project/src/main/scala
project/src/test/scala
project/src/test/resources

In my Specs2 tests I often use something like:

Path.fromString("src/test/resources/my/pkg/file.txt")

To get access to an external file that contains some data relevant to the test.

This all works perfectly.

However I am now trying to move to a multi-module project structured like so - 

project/pom.xml
project/parent/pom.xml
project/module1/pom.xml
project/module1/src/main/scala
project/module1/src/test/scala
project/module1/src/test/resources

The project/pom.xml is the top-level pom and really just references the parent pom, and declares the modules.
The project/module1/pom.xml is specific to the module, it includes the parent pom.
If you wish to see our pom files - our project is on GitHub - https://github.com/digital-preservation/csv-validator

The problem is that is i run 'mvn clean install' from the project/ folder using the top-level pom, my tests in module1/ now fail, because when they try and load their resources, they get an invalid path. i.e. - 

Path.fromString("src/test/resources/my/pkg/file.txt")

now resolves to "project/src/test/resources/my/pkg/file.txt", whereas it should resolve to "project/module1/src/test/resources/my/pkg/file.txt".

If I do change into the module1/ sub-folder and run 'mvn clean install' then all works correctly. During test execution it seems the classpath is resolving relative to the mvn command and not the module under compilation.

I think this may be a problem with the scala-maven-plugin, because we use this multi-module pattern a lot with many Java projects (using the maven-compiler-plugin) and have not experienced any problems.

 
To me this looks like a wrong usage of Specs2 Path.
It resolves by using "current working directory" (CWD), i.e. new File(".", relativePath). So depending on CWD the tests pass or fail.
You should use Class#getResource(String) or Class#getResourceAsStream(String) instead. This way you can load files which are relative to the used Class.
For example: my.pkg.MyClass.class.getResourceAsStream("file.txt")
 

Any ideas?

Thanks Adam.



--
 
---
You received this message because you are subscribed to the Google Groups "Maven and Scala" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maven-and-sca...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

David Bernard

unread,
Apr 12, 2013, 8:04:34 AM4/12/13
to Maven and Scala
The execution of your specs2 test is managed by maven-specs2-plugin, not by the scala-maven-plugin (run goal) nor by surfire (like your JUnit test, it's possible to run specs as JUnit).

I don't know if some committers or user of maven-specs2-plugin is member of this ml. You can try to open a ticket at https://github.com/mmakowski/maven-specs2-plugin/issues .

As a workaround you can try to call
Path.fromString(System.getProperty("basedir") + "/src/test/resources/my/pkg/file.txt"); like with surefire


Reply all
Reply to author
Forward
0 new messages