Java 在脱机生成中缓存Maven父级
TL/DR:如何缓存Java 在脱机生成中缓存Maven父级,java,maven,docker,Java,Maven,Docker,TL/DR:如何缓存pom.xml文件的,以便在脱机模式下运行生成? 我正在使用Docker构建一个maven项目。我的目标是为构建添加两个步骤:一个是下载所有依赖项,另一个是构建项目。到目前为止,我的Dockerfile是这样的: FROM maven:3.3-jdk-8 # Download the project dependencies (so they can be cached by Docker) ADD pom.xml /runtime/ WORKDIR /runtime RU
pom.xml
文件的
,以便在脱机模式下运行生成?
我正在使用Docker构建一个maven项目。我的目标是为构建添加两个步骤:一个是下载所有依赖项,另一个是构建项目。到目前为止,我的Dockerfile是这样的:
FROM maven:3.3-jdk-8
# Download the project dependencies (so they can be cached by Docker)
ADD pom.xml /runtime/
WORKDIR /runtime
RUN mvn dependency:go-offline
RUN mvn dependency:resolve-plugins
# Mount the local repository
ADD . /runtime
# Build the service
RUN mvn clean package -o -DskipTests
这对于插件来说似乎很好。我检查了/root/.m2/存储库
,似乎一切都正常
编辑:当仔细检查/root/.m2/repository
目录时,它不再存在。出于某种原因,Maven没有将任何依赖项保存到此位置
编辑2:构建Docker映像后,没有
/root/.m2/repository
目录。但是,如果我在Docker容器内的shell中运行mvn dependency:go offline
,则创建目录时不会出现问题。
尝试构建应用程序时,出现以下错误:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.example:service:1.2: Cannot access central (http://jcenter.bintray.com) in offline mode and the artifact org.springframework.boot:spring-boot-starter-parent:pom:1.4.0.M3 has not been downloaded from it before. and 'parent.relativePath' points at wrong local POM @ line 14, column 13
@
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.sample:service:1.2 (/runtime/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.oe:graph-service:1.2: Cannot access central (http://jcenter.bintray.com) in offline mode and the artifact org.springframework.boot:spring-boot-starter-parent:pom:1.4.0.M3 has not been downloaded from it before. and 'parent.relativePath' points at wrong local POM @ line 14, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
问题似乎是
mvn依赖项:脱机无法解析父级。当我在脱机模式下运行构建时,它会中断
以下是我的pom.xml
文件的相关部分:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.M3</version>
</parent>
...
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>repository.springsource.snapshot</id>
<name>SpringSource Snapshot Repository</name>
<url>http://repo.springsource.org/snapshot</url>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.scala</groupId>
<artifactId>spring-scala_2.11</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- disabling Spring cloud AWS until proper testing harnesses can be set up -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-autoconfigure</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
...
</dependencies>
...
</project>
4.0.0
...
org.springframework.boot
spring启动程序父级
1.4.0.M3
...
假的
中心的
垃圾箱
http://jcenter.bintray.com
repository.springsource.snapshot
SpringSource快照存储库
http://repo.springsource.org/snapshot
春季里程碑
http://repo.spring.io/milestone
...
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
org.springframework.boot
弹簧靴起动器码头
org.springframework.boot
弹簧起动试验
测试
org.springframework.scala
spring-scala_2.11
1.0.0.1构建快照
org.springframework.boot
弹簧启动安全
org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
弹簧靴启动器jdbc
org.apache.httpcomponents
httpcore
最新的
org.springframework.cloud
春季云自动配置
1.1.0.1发布
...
...
对Docker使用RUN
似乎就像在shell脚本中执行命令,但事实并非如此。RUN
的每个实例都将应用于一个新容器,该容器是由上一个命令创建的更改产生的。因此,每个命令都在一个新的容器上下文中执行
DockerRifles可以包含一个卷
引用,它在Docker容器中装载一个外部目录。如果卷中有任何文件,这些文件将被清除。如果您没有明确指定卷,Docker很乐意创建一个空文件夹
虽然我的Dockerfile不包含对卷的显式引用,但它的父级包含。因此,即使我的mvn-dependency:go-offline
命令正在运行,这些文件在下一步也会被中指定的VOLUME
清除
最后,我找不到一个好办法让maven
Docker映像正常工作,所以我切换到openjdk
映像,并通过apt get
安装maven
。如果自定义maven的settings.xml
文件,您可以将存储库文件保存在映像上
创建自定义版本的settings.xml
,修改localRepository
设置,如下所示:
<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">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository -->
<localRepository>/usr/share/maven/repo</localRepository>
...
现在,您的存储库存储在/usr/share/maven/repo
中
使用onbuild
您还可以使用创建基本映像,这将允许为每个maven项目自定义配置映像
像这样:
FROM maven:3-jdk-8
COPY settings.xml /usr/share/maven/conf/settings.xml
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ONBUILD ADD . /usr/src/app
ONBUILD RUN mvn dependency:go-offline
ONBUILD RUN mvn clean package
然后构建图像:
docker build -t mvn_bldr .
这将为其他maven图像创建一个模板。然后,您可以使用以下工具创建自定义下游图像:
FROM mvn_bldr
如果您想进一步自定义图像,您可以添加更多说明,模板的每条说明都将在FROM mvn_bldr
命令后触发,如文档中所示:
触发器将在下游构建的上下文中执行,
就好像它是在中的FROM指令之后立即插入的一样
下游Dockerfile
顺便说一句,这是我第一次与Maven合作。我继承了这个repo,没有编写pom.xml
文件。如果您在pom.xml
文件或我的方法中发现任何其他问题,请毫不犹豫地指出它们mvn依赖项:脱机
应该已经下载父级的POM文件并将其安装到本地存储库中。我用Maven 3.3.9进行了测试,它做到了(所以在离线模式下运行之后就可以了)。您使用的是哪个版本?您能否验证父POM是否存在于本地存储库中的~/.m2/repository/org/springframework/boot/spring boot starter parent/1.4.0.M3
?我也遇到了这个问题。Maven dependency tree显示我的pom的父级未解析。@Tunaki在查看存储库目录时做得很好。在仔细检查之后,这个目录似乎不再存在了。我在问题中添加了一个编辑。Maven一定在使用另一个本地回购协议
FROM mvn_bldr