gpt4 book ai didi

java - Dockerfile在找不到Java类时失败,如何将类路径或jar指定给./mvnw?

转载 作者:行者123 更新时间:2023-12-02 20:58:58 28 4
gpt4 key购买 nike

我正在从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”配置文件(包括前端)运行它。产品版本添加参数“-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”命令,但它不执行yarn build部分。

这是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”]。

最佳答案

如果您已经将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"]

关于java - Dockerfile在找不到Java类时失败,如何将类路径或jar指定给./mvnw?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com