【发布时间】:2021-11-06 17:14:40
【问题描述】:
我有一个具有以下结构的项目:
+-- the_project
+-- springapp1
| `-- pom.xml
| `-- Dockerfile
+-- springapp2
| `-- pom.xml
| `-- Dockerfile
`- pom.xml
除此之外,我在一个单独的文件夹中有一个 docker-compose 文件,但我会省略它的代码,因为我认为问题不存在。
每个 Dockerfile 看起来像这样(区别——jar 名称):
FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD
COPY ../pom.xml /build/
COPY ./pom.xml /build/child/
COPY src /build/child/src/
WORKDIR /build/child/
RUN mvn package
FROM adoptopenjdk:11-jre-hotspot
WORKDIR /app
COPY --from=MAVEN_BUILD /build/target/app1-0.0.1-SNAPSHOT-jar-with-dependencies.jar /app/
ENTRYPOINT ["java","-Dspring.profiles.active=prod","-jar","/app1-0.0.1-SNAPSHOT-jar-with-dependencies.jar"]
这里是主(根)POM:
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/>
</parent>
<groupId>org.the_project</groupId>
<artifactId>Project</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springapp1</module>
<module>springapp2</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
--lot's of dependencies here--
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.4.RELEASE</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spring 应用程序 1 POM:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.the_project</groupId>
<artifactId>Project</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>springapp1</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>*I have a starter class here*</start-class>
<java.version>11</java.version>
<kotlin.version>1.5.31</kotlin.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.5.6</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
bla-bla-bla
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>bla-bla-bla</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
省略了第二个应用程序的 POM,如果需要,将提供。 (我还不得不用通用名称替换一些名称,真的很抱歉,假设我仔细检查了所有类和工件的名称,它们很好,问题不存在)
所以,当我运行 docker compose up 时,我遇到了这个错误:
#22 1.731 [INFO] Scanning for projects...
#22 1.802 [ERROR] [ERROR] Some problems were encountered while processing the POMs:
#22 1.803 [WARNING] 'parent.relativePath' of POM org.the_project:springapp1:[unknown-version] (/build/child/pom.xml) points at org.the_project:apringapp1 instead of org.the_project:Project, please verify your project structure @ line 5, column 1
0
#22 1.803 [FATAL] Non-resolvable parent POM for org.the_project:springapp1:[unknown-version]: Could not find artifact org.the_project:Project:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 5, column 10
#22 1.804 @
#22 1.805 [ERROR] The build could not read 1 project -> [Help 1]
#22 1.806 [ERROR]
#22 1.806 [ERROR] The project org.the_project:springapp1:[unknown-version] (/build/child/pom.xml) has 1 error
#22 1.806 [ERROR] Non-resolvable parent POM for org.the_project:springapp1:[unknown-version]: Could not find artifact org.the_project:Project:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 5, column 10 ->
[Help 2]
#22 1.807 [ERROR]
#22 1.807 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
#22 1.807 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
#22 1.808 [ERROR]
有一个问题与完全相同的错误 (here) 但似乎没有解决方案。
我已经花了几个小时试图弄明白出了什么问题,但我找不到任何解决方案。
问题肯定在这里(因为手动“清理包”/以及其他命令/工作得很好):
FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD
bla bla bla
它看不到父 POM。 我只是不知道为什么!((
任何想法都非常欢迎......
【问题讨论】:
-
如果你真的喜欢在容器中构建,你必须复制所有目录
springapp1和springapp2...pluspom.xml... -
所以他们的 dockerfiles 中的每个孩子(应用程序 1 和应用程序 2)都应该有这样的东西?
COPY ../pom.xml /build/ (new line) COPY . /build/child/ (new line) WORKDIR /build/child/ -
不,没用 :( 我复制了父 POM 以及所有子文件,但仍然是这个错误,他看不到我的父 POM
-
比你做错了什么......这看起来很奇怪,因为
../pom.xml引用了一个不在给定结构中的 pom 文件......问题是你的 Dockerfile 在目录结构中的位置? -
2 个 Dockerfile 中的每一个都位于 spring 应用程序根目录(在 springapp1/2 文件夹中),这就是为什么(根据您的建议)我将父 POM 复制到构建文件夹(../pom. xml 到 /build),将所有内容从当前目录复制到 build/child(点到 build/child)。这就是我认为将文件夹层次结构保存在容器中的方式(子 POM 的父级:
../pom.xml )。基本上容器中的应用程序(在子文件夹中)将在构建文件夹中上升(2 个点)时看到父 POM。我错了吗?
标签: maven kotlin dockerfile