在脱机构建中缓存Maven父级

时间:2016-11-30 21:32:02

标签: java maven docker

TL / DR:如何缓存pom.xml文件<parent>,以便我的构建可以在离线模式下运行?

我使用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/repository,一切似乎都是有序的。

编辑:当仔细检查/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 dependency:go-offline没有解析父母。当我在离线模式下运行构建时,它会中断。

以下是我的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>

2 个答案:

答案 0 :(得分:3)

如果您自定义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>
...

然后在构建图像时覆盖默认配置:

FROM maven:3-jdk-8

COPY settings.xml /usr/share/maven/conf/settings.xml

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ADD . /usr/src/app

RUN mvn dependency:go-offline

RUN mvn clean package

现在您的存储库存储在/usr/share/maven/repo

使用onbuild

您还可以使用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。

答案 1 :(得分:1)

使用Docker的RUN似乎就像在shell脚本中执行命令一样,但事实并非如此。 RUN的每个实例都应用于由上一个命令创建的更改产生的新容器。因此,每个命令都在新的容器上下文中执行。

Dockerifles可以包含VOLUME引用,该引用在Docker容器中安装外部目录。如果卷中有任何文件,则会清除这些文件。如果您没有明确指定卷,Docker很乐意创建一个空文件夹。

虽然我的Dockerfile没有包含对VOLUME的显式引用,但它的父代却有。{1}}。因此,即使我的mvn dependency:go-offline命令正在运行,这些文件也会在docker-maven Dockerfile中指定的VOLUME在下一步中被删除。

最后,我无法找到使maven Docker镜像正常工作的好方法,因此我切换到openjdk图片并通过{{安装maven 1}}而不是。