views:

272

answers:

2

Hi, My test code in Maven project B (which is a child of project A) has something like

String filePath = "src/main/webapp";
//do something with the filePath

The above test case runs fine when I run the project from child (i.e, level B) but when I run from Parent project A (i.e, doing a mvn install at parent level) this fails because obviously there is no folder called "src/main/webapp" under parent level (It is however available in child level).

I know I could do some coding do check if a test case is running from parent/child module but apparently I want to know what others have done when they have had this problem ?

And no, I cant use the classpath instead (for various boring reasons).

I also tried relative path but then the test case sort of starts to know too much. Is there actually a solution at all for this ?

UPDATE (12/Feb) - I have a web.xml under webapp folder and create a jetty server in a test case using that web.xml. Ideally src/main/webapp is not placed in the classpath. It is used by the WAR plugin to package the WAR. Now, I tried an alternative where I put my web.xml in the src/main/resource/console-webapp/WEB-INF/web.xml directory and altered the webXml attribute in the maven war plugin. This seems to solve my problem. However, in the output WAR I have a web.xml and another redundant web.xml (which was copied because it is in the classpath).

I have tried "packageExcludes" and "webResources/excludes" and got the war plugin to omit the second web.xml but still the directory "console-webapp" gets copied (although is empty). Is there any way to tell maven war plugin to ignore a directory completely (i.e, what is the ant pattern for that?)

+5  A: 

The Maven 2 conventions state that all test resources, including files such as the one you are trying to use in your test, must be stored in the src/test/resources folder. If you respect this convention, you will be able to get your file from the classpath directly. These resources will not be included in the final packaging (JAR, WAR, EAR...).

Of course, you can change the directory and then specify the new test resources directory in your pom.xml:

<build>
    <testResources>
        <testResource>
            <directory>...</directory>
        </testResource>
    </testResources>
    ...

In your question, you specified that you can't use the classpath. Why so?

romaintaz
+1 for mentioning that by convention all test resources should go into src/test/resources.
Nils Schmidt
The above is true but the OP is very likely not trying to load a test resource but a file from the webapp.
Pascal Thivent
Pascal you are right. I have updated the question with some details from my findings. Thanks :)
Calm Storm
+1  A: 

Is the file you are trying to access on the test classpath? If so, how about:

public class IOUtils {

  public URL getResourceAsURL(String resource) {
    ClassLoader cl = getClass().getClassLoader();
    return cl.getResource(resource);
  }


  public static InputStream getResourceAsStream(String resource) throws IOException  {
    ClassLoader cl = getClass().getClassLoader();
    InputStream in = cl.getResourceAsStream(resource);

    if (in == null) 
      throw new IOException("resource \"" + resource + "\" not found");

    return in;
  }
}
Caffeine Coma