0

Context

I am using the maven plugin Fabric8 to build a docker container during my integration tests. One of my maven dependencies has a Dockerfile in its src/main/resources folder.

I would like to refer to that Dockerfile in the build phase of the plugin's image configuration.

In Intellij I can see the Dockerfile in the External Libraries section.

Maven: com.example.project:myartifact:1.0-SNAPSHOT
  myartifact-1.0-SNAPSHOT.jar
    container-dir
      Dockerfile

Question

How do I refer to a file in an External Library / Maven Dependency?

In other words, what is the relative path to a mvn project's dependency files?

Other Info

Where it is in the code:

<configuration>
    <images>
        <image>
            <alias>database</alias>
            <name>container-name</name>
            <build>
                <dockerFileDir>?????</dockerFileDir>
            </build>

I have seen this question: Reading a resource file from within jar but this only answer the question for how to refer to the file within Java but not with Maven itself.

Rorschach
  • 3,684
  • 7
  • 33
  • 77

1 Answers1

0

Dependencies are JARs. JARs are ZIPs. They are resolved from the local Maven repository (${user.home}/.m2/repository by default in settings.xml)(and downloaded from a remote repository before if there is such and if one isn't in the local repository yet). So, you can't access files in them just by a (relative or absolute) path directly.

If this is a dependency you create yourself (you mention src/main/resources of it) the relative path depends on your directory structure, e.g.:

+- dependency
|  +- src/main/resources
|     +- Dockerfile
+- project
   + pom.xml

From project's view it would be ../dependency/src/main/resources/Dockerfile then, IF you run mvn ... from within /project. If you run it from an other location with .../not-project $ mvn -f /path/to/project/pom.xml ... you're out of luck with this.

And if, for instance, someone checks out project but not dependency (since the latter is resolved from a repository anyway) (s)he is completely out of luck.

So, I don't recommend this since relying on local paths external to a project is not too safe. The safer and Maven way is to place this resource in project/src/main/resources (this dir's name is not just by chance ;).

OTOH, I understand to keep Dockerfile in its dependency project since that's were it belongs to for its development.

To make it availble to project you can use maven-dependency-plugin:unpack with its Usage page section (which is a bit confusing concerning <executions> and <configuration>):

Goal that retrieves a list of artifacts from the repository and unpacks them in a defined location.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107