6

Across several projects I have some resources (specifically Flyway database migration scripts) that I'd like to be shared.

Is it possible to have these shared resources exist as a Maven artifact, and prior to a build have Maven resolve that dependency and unpack the contents of the artifact to /src/main/resources/? If so, how would one go about this?

DeejUK
  • 12,891
  • 19
  • 89
  • 169

1 Answers1

7

If you place some files in /src/main/resources they will be placed on the CLASSPATH in the target JAR artifact. This means if you depend on such an artifact, you will have access to all resources, just as you have access to classes in it.

<dependency>
    <groupId>com.example.foo</groupId>
    <artifactId>my-resources</artifactId>
    <version>0.1</version>
</dependency>

If my-resources artifact contains some resources in /src/main/resources, you can access them at runtime just like you (or any other library) can access /src/main/resources contents from the same artifact.

Note that this won't work with /src/test/resources because test resources are only placed on CLASSPATH during surefire execution of current artifact.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thanks! Would the same work for `/src/test/resources` if the dependency scope was `test`? – DeejUK Feb 16 '12 at 13:31
  • @Deejay: no. Also you cannot import classes from `/src/test/java`, even when imported using `test` scope. – Tomasz Nurkiewicz Feb 16 '12 at 13:34
  • You can simply put the resources in your `test` artitact's `src/main/resources`. – carlspring Feb 16 '12 at 14:38
  • @carlspring: I was wondering if the resources from the artifact could end up in `/src/test/resources/`, as I have use cases where said resources are only needed for setting up JUnit tests. – DeejUK Feb 16 '12 at 15:40
  • @Deejay should work by following http://stackoverflow.com/a/12299238/1266906 in the sence of classpaths – TheConstructor Feb 19 '14 at 17:47
  • @TomaszNurkiewicz When importing a dependency of Project B in Project A, how can we STOP importing all the resources of Project B in A. – user3187479 Jun 20 '20 at 11:00