spotify / dockerfile-maven

MATURE: A set of Maven tools for dealing with Dockerfiles
Apache License 2.0
2.75k stars 493 forks source link

Spring boot project no main manifest attribute #359

Closed Gavery closed 4 years ago

Gavery commented 4 years ago

Description

I'm using 'mvn clean package' command to build the image. the JAR file on the terminal can working success.but running from the mirror I built will cause an error with 'no main manifest attribute, in demo-0.0.1-SNAPSHOT.jar'. If I build the image with ‘docker build ...’ command alone, it will run successfully

How to reproduce

1.mvn clean package 2.docker run -d '...'

What do you expect

the image run success.

Software:

Full backtrace

pom.xml

 <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <executions>
                    <execution>
                        <id>build-images</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>192.168.1.200/library/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Dockerfile

FROM 192.168.1.200/library/openjdk:8-jdk-alpine
MAINTAINER gaojiayiner@163.com
LABEL version="1.0.0"
RUN mkdir -p /project && mkdir -p /data
ENV workdir /project
WORKDIR $workdir
ARG JAR_FILE
COPY ${JAR_FILE} /project/demo.jar
EXPOSE 8080
#ENTRYPOINT ["sh","-c","java -jar /project/demo.jar"]
CMD ["/bin/sh"]
[INFO]  ---> Running in b833377e2f6d
[INFO] Removing intermediate container b833377e2f6d
[INFO]  ---> 25994c58821b
[INFO] Successfully built 25994c58821b
[INFO] Successfully tagged 192.168.1.200/library/demo:0.0.1-SNAPSHOT
[INFO] 
[INFO] Detected build of image with id 25994c58821b
[INFO] Building jar: /root/docker/docker-java/target/demo-0.0.1-SNAPSHOT-docker-info.jar
[INFO] Successfully built 192.168.1.200/library/demo:0.0.1-SNAPSHOT
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.2.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.2.RELEASE:repackage (default) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  56.051 s
[INFO] Finished at: 2020-07-21T00:34:24+08:00
[INFO] ------------------------------------------------------------------------
[root@localhost docker-java]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.1.200/library/demo      0.0.1-SNAPSHOT      25994c58821b        41 seconds ago      106MB
[root@localhost docker-java]# docker run -it 25994c58821b sh
/project # ls
demo-0.0.1-SNAPSHOT.jar
/project # java -jar demo-0.0.1-SNAPSHOT.jar 
no main manifest attribute, in demo-0.0.1-SNAPSHOT.jar

This jar file, runs alone in the terminal is successful

[root@localhost target]# java -jar demo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-07-21  00:57:39.584 [main] INFO  com.example.demo.DemoApplication - Starting DemoApplication v0.0.1-SNAPSHOT on localhost.localdomain with PID 97835 (/root/docker/docker-java/target/demo-0.0.1-SNAPSHOT.jar started by root in /root/docker/docker-java/target)
2020-07-21  00:57:39.587 [main] INFO  com.example.demo.DemoApplication - No active profile set, falling back to default profiles: default
mattnworb commented 4 years ago

Hi, I don’t know anything about spring-boot-maven-plugin but if you are expecting the output of its repackage goal to be included in your docker image, then you probably want the docker plugin to run after the spring plugin runs. The mvn output you included indicates the spring plugin is running after, not before.

In Maven if you have two plugins bound to the same phase (package) then the plugins are run in the order that they appear in your pom.xml file, so I think you simply need to move the spring plugin’s xml element to be before dockerfile-maven-plugin’s .