[英]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:
我对问题中的代码做了以下操作:
ENTRYPOINT ["java", "-jar", "com-example-application1-1.0-exec.jar"]
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.