如何通过Docker构建依赖GitHub Maven库的应用并每次获取最新Jar?
mvn package) when building an app image? Absolutely—this is totally doable with Docker, and there are a couple of practical workflows to choose from depending on your needs. Let’s break them down step by step:
Approach 1: Build the library directly from GitHub during Docker build
This method pulls your library’s source code straight from GitHub, builds it into a JAR, and uses it to build your application—all within the Docker build process. It guarantees you’re using the absolute latest code from your repo every time you build the image.
Here’s a multi-stage Dockerfile example to implement this:
# Stage 1: Build your GitHub-hosted library FROM maven:3.8.6-openjdk-11 AS library-builder WORKDIR /app/library # Clone your library repo (replace with your actual GitHub repo URL) # For private repos, use a GitHub Personal Access Token (PAT) for authentication ARG GITHUB_TOKEN RUN git clone https://${GITHUB_TOKEN}@github.com/your-username/your-library-repo.git . # Build the library to generate the JAR file RUN mvn clean package -DskipTests # Stage 2: Build your application using the freshly built library FROM maven:3.8.6-openjdk-11 AS app-builder WORKDIR /app/application # Copy your application's pom.xml and source code COPY pom.xml . COPY src ./src # Copy the built library JAR from the first stage into Maven's local repository # Update the path and coordinates to match your library's group ID, artifact ID, and version RUN mkdir -p ~/.m2/repository/com/your-org/your-library/1.0.0-SNAPSHOT COPY --from=library-builder /app/library/target/your-library-1.0.0-SNAPSHOT.jar ~/.m2/repository/com/your-org/your-library/1.0.0-SNAPSHOT/ # Build your application RUN mvn clean package -DskipTests # Stage 3: Create a lightweight runtime image for your app FROM openjdk:11-jre-slim WORKDIR /app COPY --from=app-builder /app/application/target/your-application.jar . CMD ["java", "-jar", "your-application.jar"]
Key notes for this approach:
- Private repos: When building, pass your GitHub PAT as a build argument to authenticate:
docker build --build-arg GITHUB_TOKEN=your-personal-access-token -t your-app-image . - Dependency alignment: Make sure your application’s
pom.xmlreferences the exact same group ID, artifact ID, and version as your library.
Approach 2: Publish the library to GitHub Packages first, then pull it during Docker build
If you don’t want to build the library from scratch every time (which can speed up your app’s Docker builds), you can publish your library to GitHub Packages first, then have your app’s Docker build pull the latest version from there.
Step 1: Configure your library to publish to GitHub Packages
Add this to your library’s pom.xml to set up GitHub Packages as a Maven repository:
<distributionManagement> <repository> <id>github</id> <name>GitHub Packages</name> <url>https://maven.pkg.github.com/your-username/your-library-repo</url> </repository> </distributionManagement>
Then publish the library using:
mvn deploy
(You’ll need a Maven settings.xml with your GitHub PAT to authenticate the deploy.)
Step 2: Build your app with Docker, pulling from GitHub Packages
Create a Dockerfile that configures Maven to access GitHub Packages and pulls the latest library version:
# Stage 1: Build your application FROM maven:3.8.6-openjdk-11 AS app-builder WORKDIR /app/application # Copy a Maven settings file with GitHub Packages credentials COPY settings.xml ~/.m2/ # Copy your app's source code COPY pom.xml . COPY src ./src # Build the app—Maven will pull the latest library from GitHub Packages RUN mvn clean package -DskipTests # Stage 2: Runtime image FROM openjdk:11-jre-slim WORKDIR /app COPY --from=app-builder /app/application/target/your-application.jar . CMD ["java", "-jar", "your-application.jar"]
Your settings.xml should include authentication for GitHub Packages:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>github</id> <username>your-username</username> <password>your-github-token</password> </server> </servers> <repositories> <repository> <id>github</id> <url>https://maven.pkg.github.com/your-username/your-library-repo</url> <snapshots> <enabled>true</enabled> <!-- Enable if using SNAPSHOT versions for auto-updates --> </snapshots> </repository> </repositories> </settings>
Which approach should you choose?
- Use Approach 1 if you want to guarantee you’re always using the latest commit from your library’s repo (great for active development where you push changes frequently).
- Use Approach 2 if you want faster builds (since you’re reusing a pre-built library) and prefer a more structured release workflow (e.g., auto-publish the library via CI/CD when you merge to main).
内容的提问来源于stack exchange,提问作者Valt




