简体   繁体   English

Dockerfile 在 Java class 上失败,如何指定类路径或 Z68995FCBF4342492D15484 到 D/D

[英]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.我正在关注链接中的一个示例,该示例展示了如何开发一个简单的 React/Spring-boot CRUD 应用程序。 It works great.它工作得很好。

Now I'm attempting to move it into a Docker container.现在我正在尝试将其移动到 Docker 容器中。 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.我已经成功地为“dev”配置文件做了它,这是默认配置,但该部分不包括构建中的 React 前端。

Here's the current Docker file:这是当前的 Docker 文件:

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.现在,我想使用“prod”配置文件运行它,其中包括前端。 The prod version adds the parameter "-Pprod" prod版本增加参数“-Pprod”

./mvnw spring-root:run -Pprod 

I changed the Dockerfile to start on that command as follows:我更改了 Dockerfile 以启动该命令,如下所示:

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.这似乎违背了 jar 文件的目的,因为它似乎需要目标目录的副本。 But I don't know how to specify the classpath to.mvnw, and it seems like it shouldn't need one.但我不知道如何指定.mvnw 的类路径,而且似乎不需要它。

I've also tried to run the standard "java -jar myjar.jar -Pprod" command, but it doesn't execute the yarn build section.我也尝试运行标准的“java -jar myjar.jar -Pprod”命令,但它不执行纱线构建部分。

Here's the 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.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.解决方案:我使用 mvn 运行它,因为我尝试了带有“P”参数的 java 的几种变体,但没有运气。 Fortunately, somebody made a comment, since deleted, about running it with the "P" param before the jar file.幸运的是,自从删除后,有人发表了关于在 jar 文件之前使用“P”参数运行它的评论。 That didn't work, but this did: cmd ["java","-Dspring.profiles.active=prod","-jar","licensing-app-0.0.1-SNAPSHOT.jar"].这没有用,但是这样做了: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?如果您已经将 jar 复制到容器中,为什么还要尝试再次编译该项目?

I thik that you could simply just run the jar file with CMD.我认为您可以简单地使用 CMD 运行 jar 文件。

But if you want to the container to compile the project just copy the source files, compile and run the jar.但如果要容器编译工程只需复制源文件,编译运行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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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