You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Maven项目多阶段Docker构建:依赖解析与编译分离问题

Maven多阶段Docker构建:解决dependency:go-offline未下载全量依赖的问题

Hey there, let's dig into why your mvn dependency:go-offline isn't pulling all the dependencies you need, and fix that frustrating offline build error you're seeing.

为什么mvn dependency:go-offline后仍有依赖下载?

The short answer: dependency:go-offline only handles your project's direct and transitive business dependencies (the ones in your <dependencies> block). It doesn't account for the dependencies of Maven's own build plugins—like maven-resources-plugin, maven-compiler-plugin, or maven-war-plugin—which are required to run the package goal. When you run mvn package, Maven has to fetch these plugin dependencies on the fly, which is why you're still seeing downloads.

如何实现真正的"先全量下载依赖,再离线构建"?

To cover both project dependencies and plugin dependencies, you need two key Maven commands in your build stage:

1. 预下载所有依赖(项目+插件)

Add mvn dependency:resolve-plugins before mvn dependency:go-offline. This command specifically fetches all dependencies required by the plugins configured in your pom.xml.

2. 执行离线构建

Once all dependencies are cached, run mvn package with the -o (offline) flag to enforce no remote repository access.

调整后的Dockerfile

Here's the updated multi-stage Dockerfile that implements this:

### STAGE 1: Build & Cache Dependencies ###
FROM maven:3.6.1-jdk-8 as build
WORKDIR /usr/src/app

# Step 1: Cache all dependencies (project + plugins)
COPY pom.xml .
RUN mvn dependency:resolve-plugins && mvn dependency:go-offline

# Step 2: Copy source code and run offline build
COPY src src
# -o = offline mode; -DskipTests skips test execution (optional, remove if you need tests)
RUN mvn package -o -DskipTests

### STAGE 2: Production Environment ###
FROM jboss/wildfly:17.0.0.Final
# Note: Your pom.xml uses <finalName>Monolith</finalName>, so the war file is Monolith.war
COPY --from=build /usr/src/app/target/Monolith.war /opt/jboss/wildfly/standalone/deployments/Monolith.war
RUN /opt/jboss/wildfly/bin/add-user.sh admin admin123 --silent
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]

针对你的pom.xml的优化建议

Your error mentions an old version of maven-resources-plugin (2.6) which relies on outdated Maven core components. To avoid similar issues, explicitly specify a newer version of this plugin in your <build> block:

<build>
  <finalName>Monolith</finalName>
  <plugins>
    <!-- ... your existing plugins ... -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.3.1</version>
    </plugin>
  </plugins>
</build>

Newer plugin versions are more reliable, have fewer dependency conflicts, and are better supported.

验证效果

Now when you build your Docker image:

  • The dependency caching step will only re-run if your pom.xml changes (thanks to Docker's layer caching)
  • The source code build step will run entirely offline, no more unexpected downloads
  • You'll avoid the "cannot resolve plugin dependencies" error you saw earlier

内容的提问来源于stack exchange,提问作者hasdrubal

火山引擎 最新活动