Skip to content

Instantly share code, notes, and snippets.

@jonashackt
Last active April 19, 2024 13:19
Show Gist options
  • Save jonashackt/65f1700b487f49573832dbd763686a6e to your computer and use it in GitHub Desktop.
Save jonashackt/65f1700b487f49573832dbd763686a6e to your computer and use it in GitHub Desktop.
Download Maven artifact from Nexus with current version in Dockerfile
# SHORT VERSION, if artifactId and groupId are hard-coded
FROM maven:3-jdk-11
# Add sources including pom.xml to Docker build context and use as workdir for later Maven execution
ADD . /build
WORKDIR /build
# Download Maven artifact from Nexus with current version
# And do it all inside ONE 'RUN' statement, see https://stackoverflow.com/a/37860420/4964553
# Otherwise you can't define vars like ARTIFACT_VERSION for later steps in following 'RUN' commands!
RUN \
# Export current Maven Artifact version from pom.xml with https://stackoverflow.com/a/26514030/4964553
export ARTIFACT_VERSION=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.version}' \
--non-recursive \
exec:exec); \
\
# Show Maven Artifact version
echo "Artifact version used $ARTIFACT_VERSION"; \
\
# Download Maven Artifact with current version
# see https://stackoverflow.com/questions/1895492/how-can-i-download-a-specific-maven-artifact-in-one-command-line
mvn dependency:get \
-DremoteRepositories=https://nexus.your.lan/repository/maven-public/ \
-DgroupId=your.group.id \
-DartifactId=your-artifact-id \
-Dversion=$ARTIFACT_VERSION \
-Dpackaging=war \
-Dtransitive=false;
# LONG VERSION, where artifactId and groupId are also dynamically set by pom.xml values
FROM maven:3-jdk-11
# Add sources including pom.xml to Docker build context and use as workdir for later Maven execution
ADD . /build
WORKDIR /build
# define your Nexus Repository URL
ARG NEXUS_URL=https://nexus.your.lan/repository/maven-public/
# Download Maven artifact from Nexus with current version
# And do it all inside ONE 'RUN' statement, see https://stackoverflow.com/a/37860420/4964553
# Otherwise you can't define vars like ARTIFACT_VERSION for later steps in following 'RUN' commands!
RUN \
# Export current version from pom.xml with https://stackoverflow.com/a/26514030/4964553
export ARTIFACT_VERSION=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.version}' \
--non-recursive \
exec:exec); \
\
echo "version: $ARTIFACT_VERSION"; \
\
# Export groupId from pom.xml with https://stackoverflow.com/a/26514030/4964553
export ARTIFACT_GROUP_ID=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.groupId}' \
--non-recursive \
exec:exec); \
\
echo "groupId: $ARTIFACT_GROUP_ID"; \
\
# Export artifactId from pom.xml with https://stackoverflow.com/a/26514030/4964553
export ARTIFACT_ID=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.artifactId}' \
--non-recursive \
exec:exec); \
\
echo "artifactId: $ARTIFACT_ID"; \
\
# Export packaging from pom.xml with https://stackoverflow.com/a/26514030/4964553
export PACKAGING=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.packaging}' \
--non-recursive \
exec:exec); \
\
echo "packaging: $PACKAGING"; \
\
# Download Maven Artifact with current version
# see https://stackoverflow.com/questions/1895492/how-can-i-download-a-specific-maven-artifact-in-one-command-line
mvn dependency:get \
-DremoteRepositories=$NEXUS_URL \
-DgroupId=$ARTIFACT_GROUP_ID \
-DartifactId=$ARTIFACT_ID \
-Dversion=$ARTIFACT_VERSION \
-Dpackaging=$PACKAGING \
-Dtransitive=false \
-Ddest=$ARTIFACT_ID-$ARTIFACT_VERSION.$PACKAGING;
RUN ls -la
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment