spotify / dockerfile-maven

MATURE: A set of Maven tools for dealing with Dockerfiles
Apache License 2.0
2.75k stars 493 forks source link

feat request: Have <resources> configuration like docker-maven-plugin #294

Closed princerachit closed 5 years ago

princerachit commented 5 years ago

FEATURE REQUEST:

Description

https://github.com/spotify/docker-maven-plugin supports which lets you copy artifacts before triggering image build. This is helpful as I can move all the required artifacts into one directory and I don't need to modify my Dockerfile to include different paths like target/xxxx.jar and target/classes/myresource etc. rather I can use . i.e. current directory.

Was this feature intentionally not included?

dcfsc commented 5 years ago

We do what you are asking, I think, copy everything into a staging dir and run docker build there, essentially.

First define a property

<container.staging.dir>${project.build.directory}/image-staging</container.staging.dir>

Then copy some dependencies

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-jar-to-image-staging</id>
      <phase>package</phase>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>${project.groupId}</groupId>
            <artifactId>ci-demo-jar</artifactId>
            <version>${project.version}</version>
            <type>jar</type>
          </artifactItem>
        </artifactItems>
        <outputDirectory>${container.staging.dir}</outputDirectory>
        <stripVersion>true</stripVersion>
      </configuration>
    </execution>
  </executions>
</plugin>

Then copy the Dockerfile from src/main/docker/Dockerfile (I usually also include a .dockerignore) and other resources.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>

    <execution>
      <id>copy-docker</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>

      <configuration>
        <outputDirectory>${container.staging.dir}</outputDirectory>
        <overwrite>true</overwrite>
        <resources>
          <resource>
            <filtering>false</filtering>
            <directory>src/main/docker</directory>
          </resource>
          <resource>
            <filtering>false</filtering>
            <directory>src/main/resources</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

Then the plugin just defines the location.

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>dockerfile-maven-plugin</artifactId>
  <version>1.4.10</version>
  <executions>
    <execution>
      <id>build-tag-push-release</id>
      <goals>
        <goal>build</goal>
        <goal>tag</goal>
        <goal>push</goal>
      </goals>
      <configuration>
        <contextDirectory>${container.staging.dir}</contextDirectory>
    ...
</plugin>            

and the Dockerfile just assumes everything is based in the staging dir:

WORKDIR /opt/fred
COPY ci-demo-jar.jar .

Hope this helps.

princerachit commented 5 years ago

This is not exactly same as what docker-maven plugin offers. I have to explicitly use another plugin to copy jars and artifacts in this case. @dcfsc

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.