5

I want to re-use an sql script from the test resources of the database module in my in-memory test in the pizza module, but I can't get the script from the classpath.

My directory structure looks like this:

(I left most of the files/directories out for brevity)

|   pom.xml
|   
|           
+---database
|   |   pom.xml
|   |   
|   \---src
|       \---test
|           \---resources
|               \---db
|                   \---migration
|                       \---hsqldb
|                               V1__create_schema.sql
|                               V2__config_data.sql
|                               V3__user_data.sql
|                               
+---pizza
|   |   pom.xml
|   |   
|   \---src
|       +---main
|       |   +---java
|       |   |   \---com
|       |   |       \---example
|       |   |           +---domain
|       |   |           |       DoStuff.java
|       |   |                   
|       |   \---resources
|       |       |   applicationContext.xml
|       |               
|       \---test
|           +---java
|           |   \---com
|           |       \---example
|           |               DoStuffTest.java
|           |                   
|           \---resources
|                   insert-test-data.sql
|                   test-applicationContext.xml
|                   test-in-memory-database.xml
|                   
\---poms
    |   pom.xml
    |   
    \---parent
            pom.xml

Now, I would like test-applicationContext in the pizza module to create an in-memory database from the V1__create_schema.sql script in the database module, so I can run tests against it. I put this into my test-applicationContext.xml:

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:V1__create_schema.sql"/>
    <jdbc:script location="insert-test-data.sql"/>
</jdbc:embedded-database>

... but it can't find V1__create_schema.sql on my classpath. I have tried many different ways of reaching it, including maven-remote-resources-plugin, without luck.

How would I go about getting hold of that resource?

... or maybe I'm using the wrong approach?

EDIT: Thanks a lot for all the suggestions, the answer I was looking for was the one from Java1337. However, it seems like this question has already been answered here on SO. I can't believe I missed it! Sorry about the inconvenience!

Roger
  • 2,684
  • 4
  • 36
  • 51
  • Place a dependency on database in the pizza pom – Software Engineer Nov 09 '15 at 21:13
  • Yeah, I've got that. Doesn't help. – Roger Nov 09 '15 at 21:42
  • how are you defining your dependencies to the database module from within the pizza module? You might need to have the database module create a test-jar and then add a dependency to that in your pizza module. See following for more info: https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html – DB5 Nov 09 '15 at 21:53
  • As the database project is configured currently, that POM would need to build a test-jar and then pizza would have to depend on the test-jar. Or - if the database project only exists for tests, change the path from `src/test/resources` to `src/main/resources`. Then the dependency may be on the regular jar with test scope. – user944849 Nov 09 '15 at 22:08
  • I am actually using a reactor pom and a parent pom, so I straight up toss the database dependency into the pizza pom.xml dependencies element. For now, the (hsqldb) scripts should only belong to the test scope, yes. I haven't tried the test-jar dependency yet, but I will try tomorrow morning! Could it be that easy? – Roger Nov 09 '15 at 22:17
  • Yes, Engineer Dolley, you are right. I can't believe I missed it! Thanks. – Roger Nov 10 '15 at 06:56

1 Answers1

14

If you want to get "test-resources", you will need to generate a "test-jar" in the database/pom.xml as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

and then in the pizza/pom.xml, reference the test jar as follows:

<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>database</artifactId>
        <type>test-jar</type>
        <version>version</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Cheers!

java1337
  • 437
  • 3
  • 8