【问题标题】:Maven docker plugin for spring boot build error用于 Spring Boot 构建错误的 Maven docker 插件
【发布时间】:2018-04-18 11:53:04
【问题描述】:

我有一个简单的maven project 有多个模块(目前只有一个)用于Spring Boot microservices

在root folder 中包含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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>be.demo.microservices</groupId>
    <artifactId>master</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>hello-service</module>
    </modules>
</project>

在hello-service folder 中,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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.demo.microservices</groupId>
    <artifactId>hello-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <docker.image.prefix>springio</docker.image.prefix>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <baseImage>openjdk:8-jdk-alpine</baseImage>
                    <entryPoint>["sh","-c","java -Djava.security.egd=file:/dev/./urandom -jar /${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

使用mvn clean package docker:build 构建项目时,我有

...
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ hello-service ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ hello-service ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying D:\dev\microservices\demo\hello-service\target\hello-service-1.0.0-SNAPSHOT.jar -> D:\dev\microservices\demo\hello-service\target\docker\hello-service-1.0.0-SNAPSHOT.jar
[INFO] Building image springio/hello-service
Step 1/3 : FROM openjdk:8-jdk-alpine

 ---> 224765a6bdbe
Step 2/3 : ADD /hello-service-0.0.1-SNAPSHOT.jar //

 ---> 259b1622e107
Step 3/3 : ENTRYPOINT ["sh","-c","java -Djava.security.egd=file:/dev/./urandom -jar /hello-service-1.0.0-SNAPSHOT.jar"]

 ---> Running in e86870c47b6a
Removing intermediate container e86870c47b6a
 ---> 847840116367
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 847840116367
Successfully tagged springio/hello-service:latest
[INFO] Built springio/hello-service
[INFO] Tagging springio/hello-service with 1.0.0-SNAPSHOT
[INFO] Tagging springio/hello-service with latest
[INFO]
[INFO]     ------------------------------------------------------------------------
[INFO] Building master 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ master ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ master ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
 [INFO] hello-service ...................................... SUCCESS [  9.881 s]
 [INFO] master ............................................. FAILURE [  0.102 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.650 s
[INFO] Finished at: 2018-04-18T13:33:49+02:00
[INFO] Final Memory: 54M/520M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project master: Exception caught: Must specify baseImage if dockerDirectory is null -> [Help 1]
[ERROR] .....

所以服务被打包,图像被构建,但我不明白为什么在主 pom maven 中即使没有 docker 插件也在尝试构建图像?

【问题讨论】:

标签: docker spring-boot maven-3


【解决方案1】:

默认情况下,这个 docker-maven-plugin 与 maven 的包构建阶段绑定。

查看以下链接bind-docker-commands-to-maven-phases

您可以跳过绑定到 Maven 阶段的 Docker 目标:

-DskipDockerBuild to skip image build
-DskipDockerTag to skip image tag
-DskipDockerPush to skip image push
-DskipDocker to skip any Docker goals

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2018-12-01
    • 2023-01-09
    • 2016-05-18
    • 2018-05-02
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多