我目前正在尝试在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)
答案 0 :(得分:1)
在@khmarbaise的帮助下,我设法使其正常运行。我对问题中的代码执行了以下操作:
ENTRYPOINT ["java", "-jar", "com-example-application1-1.0-exec.jar"]
答案 1 :(得分:0)
我认为还有另一种解决方案。
如果在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>