Can Maven 'import' resource dependencies?
-
30-04-2021 - |
سؤال
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?
المحلول
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.