You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Wayne Fay <wa...@gmail.com> on 2006/02/22 19:00:11 UTC

[m2] Trouble accessing test resources after moving to Maven2 from Ant

I am trying to convert a multi-module project that we have
traditionally used Ant to build and run tests into Maven2.

I've already done this successfully for some other internal projects
and been very happy with it.

However, I'm running into a new problem with this particular project.
Several of our unit tests read from external resources, and it seems
like I'm not accessing the resources in a Maven-compatible way. And
YES I already moved my resources to module/src/main/resources/...
rather than keeping them in /src/main/java, and when I look in
target/test-classes, I can see they have been copied there, and are
available for the unit tests to use as needed.

So the modules consist of:
root (pom.xml, type pom)
- common (pom.xml, type jar)
- ejb (pom.xml, type jar, dependency on common)
and some others.

When I run "mvn clean package" while in the common subdirectory, all
my tests pass fine.

When I run "mvn clean package" while in the root directory, to have it
build the common and ejb packages as modules etc, the tests fail.

Here's the code for accessing the resource:
old ant style
File file = new File("src/com/mypackage/test/util/test.txt");

new maven style, works sometimes
File file = new File("target/test-classes/com/myproject/util/test.txt");

I could probably change that line to:
File file = new File("common/target/test-classes/com/myproject/util/test.txt");
and it would work in the root directory, but then it would fail again
when calling from common subdir.

I know there are ways to use getResourceAsStream() etc that would
probably solve all my problems. I just don't know the right code to
make it happen!

Thanks for any help.
Wayne

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Trouble accessing test resources after moving to Maven2 from Ant

Posted by Wayne Fay <wa...@gmail.com>.
Thanks Emmanuel. I knew it was something simple like that.

Wayne


On 2/22/06, Emmanuel Venisse <em...@venisse.net> wrote:
> test resources must be in src/test/resources
>
> You can load your resources with this code :
>
> getClass().getResource( "com/myproject/util/test.txt" );
> or
> getClass().getResourceAsStream( "com/myproject/util/test.txt" );
> or
>
>     public static File getTestFile( String path )
>     {
>         return new File( getBasedir(), path );
>     }
>
>     public static String getBasedir()
>     {
>         if ( basedirPath != null )
>         {
>             return basedirPath;
>         }
>
>         basedirPath = System.getProperty( "basedir" );
>
>         if ( basedirPath == null )
>         {
>             basedirPath = new File( "" ).getAbsolutePath();
>         }
>
>         return basedirPath;
>     }
>
> and File f = getTestFile("com/myproject/util/test.txt");
>
> Emmanuel

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: [m2] Trouble accessing test resources after moving to Maven2 from Ant

Posted by Emmanuel Venisse <em...@venisse.net>.
test resources must be in src/test/resources

You can load your resources with this code :

getClass().getResource( "com/myproject/util/test.txt" );
or
getClass().getResourceAsStream( "com/myproject/util/test.txt" );
or

     public static File getTestFile( String path )
     {
         return new File( getBasedir(), path );
     }

     public static String getBasedir()
     {
         if ( basedirPath != null )
         {
             return basedirPath;
         }

         basedirPath = System.getProperty( "basedir" );

         if ( basedirPath == null )
         {
             basedirPath = new File( "" ).getAbsolutePath();
         }

         return basedirPath;
     }

and File f = getTestFile("com/myproject/util/test.txt");

Emmanuel

Wayne Fay a écrit :
> I am trying to convert a multi-module project that we have
> traditionally used Ant to build and run tests into Maven2.
> 
> I've already done this successfully for some other internal projects
> and been very happy with it.
> 
> However, I'm running into a new problem with this particular project.
> Several of our unit tests read from external resources, and it seems
> like I'm not accessing the resources in a Maven-compatible way. And
> YES I already moved my resources to module/src/main/resources/...
> rather than keeping them in /src/main/java, and when I look in
> target/test-classes, I can see they have been copied there, and are
> available for the unit tests to use as needed.
> 
> So the modules consist of:
> root (pom.xml, type pom)
> - common (pom.xml, type jar)
> - ejb (pom.xml, type jar, dependency on common)
> and some others.
> 
> When I run "mvn clean package" while in the common subdirectory, all
> my tests pass fine.
> 
> When I run "mvn clean package" while in the root directory, to have it
> build the common and ejb packages as modules etc, the tests fail.
> 
> Here's the code for accessing the resource:
> old ant style
> File file = new File("src/com/mypackage/test/util/test.txt");
> 
> new maven style, works sometimes
> File file = new File("target/test-classes/com/myproject/util/test.txt");
> 
> I could probably change that line to:
> File file = new File("common/target/test-classes/com/myproject/util/test.txt");
> and it would work in the root directory, but then it would fail again
> when calling from common subdir.
> 
> I know there are ways to use getResourceAsStream() etc that would
> probably solve all my problems. I just don't know the right code to
> make it happen!
> 
> Thanks for any help.
> Wayne
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org