【问题标题】:Spring Boot with Docker and Maven - manifest is missing in JAR带有 Docker 和 Maven 的 Spring Boot - JAR 中缺少清单
【发布时间】:2019-09-20 07:05:05
【问题描述】:

我目前正在尝试在 Docker 中运行具有多个 pom 文件的 Java Spring Boot 项目。我有以下项目结构:

.
├── application1
|   |── src
│   └── pom.xml
|── application2
|   |── src
│   └── pom.xml
|── shared 
|   └── src
|   └── pom.xml
|
└── pom.xml

我的目标是为 application1 和 application2 运行 Docker 容器。这两个应用程序都依赖于通过 pom.xml 文件作为依赖项包含的共享模块。

根目录下的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>com.example.parent</groupId>
    <artifactId>com-example-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <!-- sub modules -->
    <modules>
        <module>application1</module>
        <module>application2</module>
        <module>shared</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <maven.plugins.version>2.22.1</maven.plugins.version>
    </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-starter-security</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.plugins.version}</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

共享目录中的 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>

    <parent>
        <groupId>com.example.parent</groupId>
        <artifactId>com-example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.example.shared</groupId>
    <artifactId>com-example-shared</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

</project>

application1 目录下的 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>

    <parent>
        <groupId>com.example.parent</groupId>
        <artifactId>com-example-parent</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.example.application1</groupId>
    <artifactId>com-example-application1</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.example.shared</groupId>
            <artifactId>com-example-shared</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

我的 Dockerfile:

FROM maven:3-jdk-8 as builder

WORKDIR /app
ADD ./ /app
RUN mvn install -pl application1 -am

FROM openjdk:8-jre-alpine AS runtime
COPY --from=builder /app/application1/target .
ENTRYPOINT ["java", "-jar", "com-example-application1-1.0.jar"]

构建映像没有任何问题。但是,当我尝试执行 docker run 来启动容器时,出现错误:no main manifest attribute, in com-example-application1-1.0.jar。有谁知道我做错了什么?我应该在 Dockerfile 中运行不同的 mvn 命令还是 pom.xml 文件中存在问题? (full example project on Github)

【问题讨论】:

  • 每个应该是可执行文件的子模块都需要使用 spring-boot-maven-plugin 进行配置,而不是在不起作用的父 pom 中...顺便说一句:配置 maven-surefire 的原因是什么-plugin 和 maven-failsafe-plugin 在你的父母?我怀疑这是否真的需要也没有用?
  • @khmarbaise 我删除了 maven-surefire-plugin/maven-failsafe-plugin 部分并将构建插件 maven 条目移动到子模块,但不幸的是我仍然遇到同样的错误
  • 对于这两个模块,您希望有一个正在运行的应用程序?
  • @khmarbaise 是的,我想基于application1和application2构建镜像和运行容器
  • 啊,现在我看到了这个问题。你必须使用com-example-application1-1.0-exec.jar 工件而不是com-example-application1-1.0.jar...顺便说一句:你为​​什么要在容器内构建?最好在外面构建应用程序并将结果放入容器中,否则您正在创建人员配置层等以及容器内不需要的东西......

标签: java spring maven spring-boot docker


【解决方案1】:

我认为还有另一种解决方案。

如果在POM.xml中使用了spring-boot-maven-plugin,而没有使用spring-boot-starter-parent,则需要配置spring-boot-maven-plugin 部分,如果清单不正确,例如这样:

<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>${start-class}</mainClass>
      <layout>JAR</layout>
    </configuration>
  </plugin>
</plugins>

【讨论】:

    【解决方案2】:

    在@khmarbaise 的帮助下,我设法让它工作了。我对问题中的代码做了以下操作:

    • 从根 pom.xml 中删除了故障安全和万无一失的插件
    • 将 Spring Boot maven 构建插件移至 application1 和 application2。不能在根目录的 pom.xml 中使用 Maven 插件
    • 将 Dockerfile 的入口点更改为:ENTRYPOINT ["java", "-jar", "com-example-application1-1.0-exec.jar"]

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 1970-01-01
      • 2015-01-18
      • 2016-01-16
      • 1970-01-01
      • 2020-09-05
      • 2014-08-30
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多