Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Maven 'import' resource dependencies?

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?

like image 400
EngineerBetter_DJ Avatar asked Feb 16 '12 13:02

EngineerBetter_DJ


People also ask

How do I add a dependency in Maven?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How does Maven work with dependencies?

Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath. This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.

Why Maven dependencies are not getting downloaded?

If you run Maven and it fails to download your required dependencies it's likely to be caused by your local firewall & HTTP proxy configurations. See the Maven documentation for details of how to configure the HTTP proxy.


1 Answers

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.

like image 69
Tomasz Nurkiewicz Avatar answered Sep 30 '22 18:09

Tomasz Nurkiewicz