简体   繁体   English

使用 Docker 和 Maven 的 Spring Boot - JAR 中缺少清单

[英]Spring Boot with Docker and Maven - manifest is missing in JAR

I'm currently try to run a Java Spring Boot project with multiple pom files in Docker.我目前正在尝试在 Docker 中运行一个包含多个 pom 文件的 Java Spring Boot 项目。 I have the following project structure:我有以下项目结构:

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

My goal is to run Docker containers for application1 and application2.我的目标是为 application1 和 application2 运行 Docker 容器。 Both applications are dependent on the shared module which is included as a dependency through the pom.xml file.这两个应用程序都依赖于共享模块,该模块通过 pom.xml 文件作为依赖项包含在内。

Pom.xml file in root directory:根目录下的 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 in shared directory:共享目录下的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>

Pom.xml file in application1 directory: 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>

My 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"]

Building the image goes without any problems.构建图像没有任何问题。 However, when I try to execute docker run to start a container I get an error: no main manifest attribute, in com-example-application1-1.0.jar .但是,当我尝试执行 docker run 以启动容器时,出现错误: no main manifest attribute, in com-example-application1-1.0.jar Does anyone know what I'm doing wrong?有谁知道我做错了什么? Should I run a different mvn command in the Dockerfile or is there a problem in the pom.xml files?我应该在 Dockerfile 中运行不同的 mvn 命令还是 pom.xml 文件中有问题? ( full example project on Github ) Github 上的完整示例项目

With help of @khmarbaise I managed to get it working.在@khmarbaise 的帮助下,我设法让它工作了。 I did the following to the code in the question:我对问题中的代码做了以下操作:

  • Removed failsafe and surefire plugins from root pom.xml从根 pom.xml 中删除了 failsafe 和 surefire 插件
  • Moved Spring Boot maven build plugin to both application1 and application2.将 Spring Boot maven 构建插件移动到 application1 和 application2。 The Maven plugin should not be used within the pom.xml in the root directory Maven 插件不应在根目录的 pom.xml 中使用
  • Changed the entrypoint the Dockerfile to: ENTRYPOINT ["java", "-jar", "com-example-application1-1.0-exec.jar"]将 Dockerfile 的入口点更改为: ENTRYPOINT ["java", "-jar", "com-example-application1-1.0-exec.jar"]

I'm currently try to run a Java Spring Boot project with multiple pom files in Docker.我目前正在尝试在 Docker 中运行带有多个 pom 文件的 Java Spring Boot 项目。 I have the following project structure:我有以下项目结构:

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

My goal is to run Docker containers for application1 and application2.我的目标是为 application1 和 application2 运行 Docker 容器。 Both applications are dependent on the shared module which is included as a dependency through the pom.xml file.这两个应用程序都依赖于通过 pom.xml 文件作为依赖项包含的共享模块。

Pom.xml file in root directory:根目录下的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 in shared directory:共享目录中的 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>

Pom.xml file in application1 directory: 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>

My Dockerfile:我的 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"]

Building the image goes without any problems.构建图像没有任何问题。 However, when I try to execute docker run to start a container I get an error: no main manifest attribute, in com-example-application1-1.0.jar .但是,当我尝试执行 docker run 以启动容器时,出现错误: no main manifest attribute, in com-example-application1-1.0.jar Does anyone know what I'm doing wrong?有谁知道我做错了什么? Should I run a different mvn command in the Dockerfile or is there a problem in the pom.xml files?我应该在 Dockerfile 中运行不同的 mvn 命令还是 pom.xml 文件中存在问题? ( full example project on Github ) Github 上的完整示例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring-boot-devtools在JAR上失败,缺少MANIFEST.MF - spring-boot-devtools fails on JAR with missing MANIFEST.MF JAR清单中缺少Maven工作空间和系统依赖关系 - Maven Workspace and System Dependencies Missing in JAR Manifest 使用 Spring Boot Maven 插件时,jar 文件中缺少 Spring Boot 应用程序中的资源 - resources in a Spring Boot application are missing from jar file when using Spring Boot Maven Plugin Spring Boot JAR - 缺少EmbeddedServletContainerFactory - Spring Boot JAR - missing EmbeddedServletContainerFactory java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar error using Selenium through Maven in Spring Boot - java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar error using Selenium through Maven in Spring Boot 在maven中创建可执行jar文件,清单中缺少主类 - Create executable jar file in maven,missing main-class in manifest Spring Boot Maven 插件 - 重命名原始 JAR - Spring Boot Maven Plugin - rename original JAR 创建Jar of Maven Spring Boot项目 - Create Jar of maven spring boot project 添加外部 jar 到 Spring 引导 maven 项目 - add external jar to Spring boot maven project Maven jar 中没有主要清单 - no main manifest in maven jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM