Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Using mave in the buildArgs parameter of configuration, can't get ${project.basedir} normally #209

Closed
ToQuery opened this issue Aug 29, 2018 · 9 comments

Comments

@ToQuery
Copy link

ToQuery commented Aug 29, 2018

Is this a BUG REPORT or FEATURE REQUEST?:

Description

Using mave in the buildArgs parameter of configuration, can't get ${project.basedir} normally.
Customize the dockerfile file address
The actual output is /var/lib/docker/tmp/xxxxxxxxxxxx/${projextPath}/target

eq:

<plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>dockerfile-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <configuration>
             <repository>${docker.repository}/${docker.image.prefix}/${docker.image.name</repository> 
                        <contextDirectory>${project.basedir}/src/main/docker</contextDirectory>
                      <tag>${docker.image.tag}</tag>
                        <buildArgs>
                            <JAR_FILE>${project.basedir}/${docker.image.jar}</JAR_FILE>
                        </buildArgs>
                    </configuration>
</plugin>

How to reproduce

As described above

What do you expect

How to get the target path of the Maven project normally

What happened instead

Software:

  • mac os 10.13.6
  • docker version: 18.06.0-ce
  • Spotify's dockerfile-maven version: 1.4.4

Full backtrace

No
@ToQuery
Copy link
Author

ToQuery commented Aug 29, 2018

I don't know if this is a bug or design.

@boskoop
Copy link
Contributor

boskoop commented Oct 16, 2018

I think this is a bug, as the plugin somehow modifies the Maven ${project.basedir} variable.

I was using the following configuration in order to avoid hard-coding of the build-directory in the Dockerfile (normally ./target):

<buildArgs>
	<JAR_FILE>${project.build.directory}/${project.build.finalName}.jar</JAR_FILE>
	<PROJECT_NAME>${project.artifactId}</PROJECT_NAME>
	<PROJECT_VERSION>${project.version}</PROJECT_VERSION>
</buildArgs>

The variable ${project.build.directory} points to the directory /var/lib/docker/tmp/docker-builder<xxxxxxxxx>/${project.basedir}/target instead of ${project.basedir}/target when executing the dockerfile:build goal.

I think that the plugin modifies the ${project.basedir} variable by prefixing the docker-builder temp directory.

Note: this happened on Linux 3.10.

@bbortt
Copy link

bbortt commented Oct 17, 2018

I think it might be intentionally.. If you take a look at the current README: They pass the jar-name like this:

<buildArgs>
      <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>

and kind of "configure" the target directory right in the Dockerfile with ADD target/${JAR_FILE} /usr/share/myservice/myservice.jar. Presuming the Dockerfile is located in your root directory, which is kind of standard.

Assuming you've changed your standard output directory, you misbehave to their very first design goal:

Don't do anything fancy.

Edit: Besides that I've got no idea why they would change the argument. I just consider it to be intentional.

@boskoop
Copy link
Contributor

boskoop commented Oct 17, 2018

The Maven variable ${project.build.directory} is a standard variable, which should not be changed by the plugin. Using it in the POM is by no means fancy, imho it is best practice to use this variable to reference the build directory instead of hard-coding it with 'target'.

To summarize:
I think this is a bug!

@mattnworb
Copy link
Member

I can’t recall any place in the plugin where this variable is modified - I’m not even sure if it is possible to modify a Maven supplied variable like that. Can anyone point to a piece of code that is doing this?

@mattnworb
Copy link
Member

One thing to note is that your local workspace is wrapped up and sent to the docker daemon as your “build context”

@boskoop
Copy link
Contributor

boskoop commented Oct 17, 2018

That probably is the explanation. But I do not understand why the following works:

<buildArgs>
	<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>

Dockerfile:

ARG JAR_FILE
ADD ${JAR_FILE} /usr/share/myservice/myservice.jar

While, as said before, the following does not work (with the same Dockerfile):

<buildArgs>
	<JAR_FILE>${project.build.directory}/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>

I will try to investigate with a sample project.

@boskoop
Copy link
Contributor

boskoop commented Oct 17, 2018

So I created a sample project: https://github.com/boskoop/dockerfile-maven-issue-209

Actually, this issue seems to be a duplicate of #101.

The problem is not with the Maven variable itself but with the design of the docker build context, where absolute paths are not allowed. The variable ${project.build.directory} is an absolute path to the build directory.

Since more and more people are running into this (as I was too), we should think about improving the documentation.

boskoop added a commit to boskoop/dockerfile-maven that referenced this issue Oct 17, 2018
boskoop added a commit to boskoop/dockerfile-maven that referenced this issue Oct 17, 2018
@mattnworb
Copy link
Member

for a more complete explanation see #101

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants