简体   繁体   中英

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. 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. Both applications are dependent on the shared module which is included as a dependency through the pom.xml file.

Pom.xml file in root directory:

<?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:

<?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:

<?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 . 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? ( full example project on Github )

With help of @khmarbaise I managed to get it working. I did the following to the code in the question:

  • Removed failsafe and surefire plugins from root pom.xml
  • Moved Spring Boot maven build plugin to both application1 and application2. The Maven plugin should not be used within the pom.xml in the root directory
  • Changed the entrypoint the Dockerfile to: 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. 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. Both applications are dependent on the shared module which is included as a dependency through the pom.xml file.

Pom.xml file in root directory:

<?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:

<?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:

<?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 . 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? ( full example project on Github )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM