简体   繁体   中英

Dockerfile fails on Java class not found, how to specify classpath or jar to ./mvnw?

I'm following an example from this link to which shows how to develop a simple React/Spring-boot CRUD app. It works great.

Now I'm attempting to move it into a Docker container. I've succeeded in doing it for the "dev" profile, which is the default, but that part doesn't include the React frontend in the build.

Here's the current Docker file:

From openjdk:8
copy ./target/licensing-app-0.0.1-SNAPSHOT.jar licensing-app-0.0.1-SNAPSHOT.jar
copy ./mvnw mvnw
copy ./pom.xml .
copy ./.mvn/wrapper/maven-wrapper.properties .mvn/wrapper/maven-wrapper.properties
copy ./app .
#cmd ["java","-jar","licensing-app-0.0.1-SNAPSHOT.jar"]
cmd ./mvnw spring-boot:run -Pprod

Now, I'd like run it with the "prod" profile, which includes the front-end. The prod version adds the parameter "-Pprod"

./mvnw spring-root:run -Pprod 

I changed the Dockerfile to start on that command as follows:

copy ./target/licensing-app-0.0.1-SNAPSHOT.jar licensing-app-0.0.1-SNAPSHOT.jar
copy ./mvnw mvnw
copy ./pom.xml .
COPY ./.mvn/wrapper/maven-wrapper.properties .mvn/wrapper/maven-wrapper.properties
copy ./app .
CMD ./mvnw spring-boot:run -Pprod

However, when I run it, it fails on this error:

licensing-app_1  | [WARNING] 
licensing-app_1  | java.lang.ClassNotFoundException: com.license.gen.app.LicenseGenApplication
licensing-app_1  |     at java.net.URLClassLoader.findClass (URLClassLoader.java:382)
licensing-app_1  |     at java.lang.ClassLoader.loadClass 

This seems to defeat the purpose of the jar file, because it seems to require the copy of the target directory. But I don't know how to specify the classpath to.mvnw, and it seems like it shouldn't need one.

I've also tried to run the standard "java -jar myjar.jar -Pprod" command, but it doesn't execute the yarn build section.

Here's the 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.okta.developer</groupId>
    <artifactId>licensing-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>license-gen</name>
    <description>Analytics License Gen App</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <frontend-maven-plugin.version>1.6</frontend-maven-plugin.version>
        <node.version>v10.13.0</node.version>
        <yarn.version>v1.12.1</yarn.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
        </dependency>
        <!-- For Java 8 Date/Time Support -->
        <!--test without this, originally from example online poll program -->
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

        <!--<dependency>-->
        <!--<groupId>com.h2database</groupId>-->
        <!--<artifactId>h2</artifactId>-->
        <!--<scope>runtime</scope>-->
        <!--</dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.github.oshi</groupId>
            <artifactId>oshi-core</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.6</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.license.gen.app.LicenseGenApplication</mainClass>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <id>prod</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-resources</id>
                                <phase>process-classes</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>app/build</directory>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                        <version>${frontend-maven-plugin.version}</version>
                        <configuration>
                            <workingDirectory>app</workingDirectory>
                        </configuration>
                        <executions>
                            <execution>
                                <id>install node</id>
                                <goals>
                                    <goal>install-node-and-yarn</goal>
                                </goals>
                                <configuration>
                                    <nodeVersion>${node.version}</nodeVersion>
                                    <yarnVersion>${yarn.version}</yarnVersion>
                                </configuration>
                            </execution>
                            <execution>
                                <id>yarn install</id>
                                <goals>
                                    <goal>yarn</goal>
                                </goals>
                                <phase>generate-resources</phase>
                            </execution>
                            <execution>
                                <id>yarn test</id>
                                <goals>
                                    <goal>yarn</goal>
                                </goals>
                                <phase>test</phase>
                                <configuration>
                                    <arguments>test</arguments>
                                    <environmentVariables>
                                        <CI>true</CI>
                                    </environmentVariables>
                                </configuration>
                            </execution>
                            <execution>
                                <id>yarn build</id>
                                <goals>
                                    <goal>yarn</goal>
                                </goals>
                                <phase>compile</phase>
                                <configuration>
                                    <arguments>build</arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>
</project>

Solution: I was running it with mvn because I had tried several variations of the java with the "P" param but no luck. Fortunately, somebody made a comment, since deleted, about running it with the "P" param before the jar file. That didn't work, but this did: cmd ["java","-Dspring.profiles.active=prod","-jar","licensing-app-0.0.1-SNAPSHOT.jar"].

If you already made a copy of the jar to the container why do you try to compile the project again?

I thik that you could simply just run the jar file with CMD.

But if you want to the container to compile the project just copy the source files, compile and run the jar.

trye something like that:

From openjdk:8
CPOY ./app .
RUN ./mvnw clean install -Pprod
WORKDIR /target
CMD ["java","-jar","licensing-app-0.0.1-SNAPSHOT.jar"]

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