5

I have a .jar that contains multiple public static void main(psvm)'s that I want to be able to call when I do docker run ... -e <class.path.from.env> on the image and pass an environment variable to specify the class path. Something like this:

  <plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
      <images>
        <image>
          <name>${project.artifactId}</name>
          <build>
            <from>java:8-jre</from>
            <tags>
              <tag>${build.environment}-latest</tag>
              <tag>${build.environment}-${build.number}</tag>
            </tags>
            <entryPoint>
              <exec>
                <arg>java</arg>
                <arg>-Duser.timezone=UTC</arg>
                <arg>-cp</arg>
                <arg>/opt/${project.artifactId}-${project.version}.jar</arg>
                <arg>${class.path.from.env}</arg>
              </exec>
            </entryPoint>
            <assembly>
              <basedir>/opt</basedir>
              <inline>
                <files>
                  <file>
                    <source>target/${project.artifactId}-${project.version}.jar</source>
                  </file>
                </files>
              </inline>
            </assembly>
          </build>
        </image>
      </images>
    </configuration>
  </plugin>

Although I read the whole documentation for docker-maven-plugin, I'm not sure how I can make this work. Basically where do I declare the environment variable class.path.from.env and how can I make sure it gets the one I pass through -e in docker run ...?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
breezymri
  • 3,975
  • 8
  • 31
  • 65

1 Answers1

2

I think you need to declare a <run> section next to your <build> section, and add your env variable to <env>, as described here: https://dmp.fabric8.io/#misc-env

<run>
  <env>
    <CATALINA_OPTS>-Xmx32m</CATALINA_OPTS>
    <JOLOKIA_OFF/>
  </env>
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • is it possible to set the environment variable during the build process? – genonymous Feb 11 '23 at 23:54
  • @genonymous That's a different question, rather Maven related. And I think yes, there are plugins that can set `Maven` properties during the lifecycle, and then you can use them here. – Ondra Žižka Feb 15 '23 at 09:44