我正在尝试为基于maven的Java项目设置多阶段dockerfile。到目前为止,这是我正在使用的:
### STAGE 1: Build ###
FROM maven:3.6.1-jdk-8 as build
WORKDIR /usr/src/app
# Install app dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
# Bundle app source
COPY src src
RUN mvn package
### STAGE 2: Production Environment ###
FROM jboss/wildfly:17.0.0.Final
COPY --from=build /usr/src/app/target/Appname.war /opt/jboss/wildfly/standalone/deployments/Appname.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"]
问题与mvn
命令有关。我看到mvn dependency:go-offline
下载了一些依赖项,这很棒。但是随后mvn package
下载了更多软件包。为什么?我如何执行两个步骤:
使用mvn package -o
,找不到以下软件包:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources (default-resources) on project monolith: Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: The following artifacts could not be resolved: org.apache.maven:maven-profile:jar:2.0.6, org.apache.maven:maven-repository-metadata:jar:2.0.6, org.apache.maven:maven-plugin-registry:jar:2.0.6, classworlds:classworlds:jar:1.1-alpha-2: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.apache.maven:maven-profile:jar:2.0.6 has not been downloaded from it before. -> [Help 1]
我认为它与pom.xml
中的build插件有关。有什么想法吗?
<build>
<finalName>Monolith</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<!--<configuration>-->
<!--<webXml>src\main\webapp\WEB-INF\web.xml</webXml>-->
<!--</configuration>-->
</plugin>
</plugins>
</build>
答案 0 :(得分:1)
您最有可能受到MDEP-82 go-offline / resolve-plugins does not resolve all plugin dependencies错误的影响,该错误目前尚未解决。
根据MDEP-82问题中的this comment,可以通过指定maven-dependency-plugin版本而不是使用默认版本来解决此问题:
然后明确定义用于脱机的maven-dependency-plugin版本,而不依赖于Maven内部定义的默认版本:
mvn -s settings.xml org.apache.maven.plugins:maven-dependency-plugin:2.8:go-offline
答案 1 :(得分:0)
maven依赖插件有一些限制,因此不会下载所有依赖。当您在本地工作时,这通常可以解决,因为一个mvn package
命令就可以解决问题。
但这不适用于在Docker容器中运行的Maven。经过大量搜索,今天我偶然发现了go-offline-maven-plugin。我在用于测试的小项目上进行了尝试,并且成功了。
这是将其包含在pom.xml中的方法:
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>de.qaware.maven</groupId>
<artifactId>go-offline-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<dynamicDependencies>
<DynamicDependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.20.1</version>
<repositoryType>PLUGIN</repositoryType>
</DynamicDependency>
</dynamicDependencies>
</configuration>
</plugin>
<!-- ... -->
</plugins>
<!-- ... -->
</build>
<!-- ... -->
dynamicDependencies
是依赖项,在任何pom.xml
中都没有,但是在插件中进行了硬编码。最常见的情况是surefire插件,该插件会根据您的测试(例如JUnit4 / 5,TestNG等)加载所需的依赖项。
最后,您需要从Dockerfile
运行此插件。这应该起作用:
### STAGE 1: Build ###
FROM maven:3.6.1-jdk-8 as build
WORKDIR /usr/src/app
# Install app dependencies
COPY pom.xml .
RUN mvn -e -B de.qaware.maven:go-offline-maven-plugin:resolve-dependencies
# Bundle app source
COPY src src
RUN mvn -o -e -B package
### STAGE 2: Production Environment ###
FROM jboss/wildfly:17.0.0.Final
COPY --from=build /usr/src/app/target/Appname.war /opt/jboss/wildfly/standalone/deployments/Appname.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"]