question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: how to use maven resource filtering?

See original GitHub issue

Hello,

Let me say it’s a great project and it’s a real pleasure to use in our development life cycle.

However, I’m having a hard time with one particular use case. I’m not particular sure whether it’s the right place to ask question. Please forward me to right place if it is not.


The question: How does one use properly maven resource filtering capability? I’ve found mentioning of assembly filter flag in some other issues. I’ve tried, but I kinda failed / didn’t like the result.

Let me explain my use case first:

  • I have Docker file ADD-ing some configuration file from src/main/docker
  • That particular file contains artifactId and artifact version.
  • I would like to have that information to be provided / injected by maven resource filtering.

What I’ve tried:

  • Assembly <fileSet>, which took the original file and placed it somewhere at target/docker/registry-xyz.com/artifact/0.0.1-SNAPSHOT/build/maven/home/username/path1/path2/target/docker/registry-xyz.com/artifact/0.0.1-SNAPSHOT/build/maven/MY_FILE

I’m struggling to find the proper workaround and come up with concise and clear configuration snippet to get it done.

Any suggestions, maybe some links to examples or documentation etc … I would really appreciate that…

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
alan-czajkowskicommented, Jan 21, 2016

Hi @leonardinius @vberruchon,

below is my answer (similar to @chris-snider):

First, let’s make sure our plugin versions are properly defined in the <pluginManagement> section:

  <build>

    ...

    <pluginManagement>

      <plugins>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.7</version>
          <configuration>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.jolokia</groupId>
          <artifactId>docker-maven-plugin</artifactId>
          <version>0.13.7</version>
          <configuration>
            <verbose>true</verbose>
          </configuration>
        </plugin>

      </plugins>

    </pluginManagement>

    ...

  </build>

Step 1: setup maven-resources-plugin:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>filter-dockerfiles</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <resources>
                <!-- # Filtered Resources -->
                <resource>
                  <directory>${project.basedir}/src/main/docker</directory>
                  <filtering>true</filtering>
                  <includes>
                    <include>Dockerfile</include>
                  </includes>
                </resource>
                <!-- # Unfiltered Resources -->
                <resource>
                  <directory>${project.basedir}/src/main/docker</directory>
                  <filtering>false</filtering>
                  <excludes>
                    <exclude>Dockerfile</exclude>
                  </excludes>
                </resource>
              </resources>
              <outputDirectory>${project.build.directory}/docker</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

Note: the <outputDirectory>${project.build.directory}/docker</outputDirectory> is important for 2 reasons a) when you do custom resource filtering you should always send the filtered (modified) resource to the build output directory (${project.build.directory}) as good practice, and b) this acts as the Dockerfile input for the Docker maven plugin below

Step 2: setup docker-maven-plugin:

      <plugin>
        <groupId>org.jolokia</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <configuration>
          <images>
            <image>
              <name>${docker.image.name}:${project.version}</name>
              <alias>${project.artifactId}</alias>
              <build>
                <assembly>
                  <dockerFileDir>${project.build.directory}/docker</dockerFileDir>
                </assembly>
              </build>
            </image>
          </images>
        </configuration>
        <executions>
          <execution>
            <id>build-docker-image</id>
            <goals>
              <goal>build</goal>
            </goals>
            <phase>package</phase>
          </execution>
          <execution>
            <id>push-docker-image</id>
            <goals>
              <goal>push</goal>
            </goals>
            <phase>deploy</phase>
          </execution>
        </executions>
      </plugin>

You can now add Maven variable interpolation ${...} inside your src/main/docker/Dockerfile, for example I do something like:

FROM ${docker.repo}/${docker.groupId}/base:${project.version}
MAINTAINER ${docker.maintainer}
...

using properties such as:

  <properties>
    <docker.repo>docker.example.com</docker.repo>
    <docker.groupId>example</docker.groupId>
    <docker.artifactId>${project.artifactId}</docker.artifactId>
    <docker.image.name>${docker.repo}/${docker.groupId}/${docker.artifactId}</docker.image.name>
    <docker.maintainer>"Example Engineering" &lt;eng@example.com&gt;</docker.maintainer>
  </properties>
0reactions
jakub-bochenskicommented, Sep 5, 2016

Setting <dockerFileDir>${project.build.directory}/docker</dockerFileDir> makes my build fail with Execution docker of goal io.fabric8:docker-maven-plugin:0.15.16:build failed: A tar file cannot include itself. probbably beacuse it causes the docker-build.tar to reference itself. When I open the tar it has a Dockerfile at root, a maven directory, and a directory structure corresponding to the docker repository with tmp, build and work folders.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache Maven Resources Plugin – Filtering
Filtering. Variables can be included in your resources. These variables, denoted by the ${...} delimiters, can come from the system ...
Read more >
Maven resource filtering for single file - Stack Overflow
Now the problem is all xml files are not filtered. Is there any possible way to filter single xml file? xml · spring...
Read more >
Using Maven properties as Mule properties - Medium
The method is called resource filtering, and is provided by the Maven Resources plugin. It looks for placeholders in the resources folders. By ......
Read more >
9.3. Resource Filtering - TheNEXUS | A Community Project
Using Maven resource filtering you can reference Maven properties and then use Maven profiles to define different configuration values for different target ...
Read more >
Intellij make build maven resources filtering
If I compile the android app using maven directly "mvn clean package", resource filtering is applied successfully to my final apk. If I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found