有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

docker Dockerfile在找不到Java类时失败,如何指定类路径或jar/mvnw?

下面是this链接中的一个示例,它展示了如何开发一个简单的React/Spring boot CRUD应用程序。效果很好

现在,我正试图将其移动到Docker容器中。我已经成功地为“dev”配置文件完成了这项工作,这是默认配置文件,但该部分不包括构建中的React前端

以下是当前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

现在,我想用“prod”配置文件运行它,其中包括前端。prod版本添加了参数“-Pprod”

./mvnw spring-root:run -Pprod 

我将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

但是,当我运行它时,它会因以下错误而失败:

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 

这似乎违背了jar文件的目的,因为它似乎需要目标目录的副本。但是我不知道如何指定到的类路径。mvnw,似乎不需要

我还尝试运行标准的“java-jar myjar.jar-Pprod”命令,但它不执行构建部分

这是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>

解决方案:我用mvn运行它,因为我用“p”参数尝试了几种java变体,但没有成功。幸运的是,自从被删除后,有人就在jar文件之前用“P”参数运行它发表了评论。这不起作用,但它起作用了:cmd[“java”、“-Dspring.profiles.active=prod”、“-jar”、“licensing-app-0.0.1-SNAPSHOT.jar”]


共 (1) 个答案

  1. # 1 楼答案

    如果已经将jar复制到容器中,为什么还要再次编译该项目

    我认为你可以简单地用CMD运行jar文件

    但是如果你想让容器编译项目,只需复制源文件,编译并运行jar即可

    试着这样做:

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