Gradle 无法构建具有 Docker 依赖项的 JAR
Posted
技术标签:
【中文标题】Gradle 无法构建具有 Docker 依赖项的 JAR【英文标题】:Gradle cannot build JAR with dependecies for Docker 【发布时间】:2021-03-12 04:18:03 【问题描述】:我有一个Spring
MVC
应用程序,我想将它添加到Docker
。我创建了image
,配置了Docker
,但是Docker
中的应用程序不想启动。我该如何解决这个问题?
我尝试了不同的方法来修复这个错误,例如,我将以下代码添加到gradle.build
:
dependencies
extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
// ... dependencies ...
configurations.compile.extendsFrom(configurations.extraLibs)
但它没有用。
梯度:
plugins
id 'java'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories
mavenCentral()
//configurations
// configuration that holds jars to include in the jar
// extraLibs
//
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
//
jar
manifest
attributes "Main-Class": 'ru.coffeetearea.CoffeeTeArea'
ext
javaMainClass = "ru.coffeetearea.CoffeeTeArea"
task fatJar(type: Jar)
classifier = 'all'
from configurations.compile.collect it.isDirectory() ? it : zipTree(it)
with jar
dependencies
// extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.3.RELEASE'
// Thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.3.3.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation
compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.3.3.RELEASE'
// Swagger UI
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
// Swagger 2
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.14'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.1.RELEASE'
// https://mvnrepository.com/artifact/org.flywaydb/flyway-core
compile group: 'org.flywaydb', name: 'flyway-core', version: '6.5.1'
// MapStruct
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
// https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
annotationProcessor('org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5')
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.2.RELEASE'
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
// JUnit
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
// configurations.compile.extendsFrom(configurations.extraLibs)
test
useJUnitPlatform()
testLogging
events "passed", "skipped", "failed"
DOCKERFILE:
FROM openjdk:11
ADD build/libs/Coffeetearea-0.0.1-SNAPSHOT.jar Coffeetearea-0.0.1-SNAPSHOT.jar
EXPOSE 5432
ENTRYPOINT ["java", "-jar", "Coffeetearea-0.0.1-SNAPSHOT.jar"]
清单.MF:
Manifest-Version: 1.0
Main-Class: ru.coffeetearea.CoffeeTeArea
错误:
C:\Users\vartanyan\IdeaProjects\Coffeetearea>docker run -p 5432:5432 coffeetearea
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at ru.coffeetearea.CoffeeTeArea.main(CoffeeTeArea.java:12)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
【问题讨论】:
您正在使用 Spring Boot,但在您的构建中完全忽略它并解决它。别。见docs.spring.io/spring-boot/docs/current/reference/html/… 【参考方案1】:您在您的应用程序中使用 Spring Boot,并且在您的构建中您正在努力不使用它。总之不要,使用Spring Boot Gradle插件来构建一个合适的jar
plugins
id 'org.springframework.boot' version '2.3.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories
mavenCentral()
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
dependencies
// extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation group: 'org.apache.commons', name: 'commons-lang3', version:
'3.11'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation group: 'org.springframework.boot:spring-boot-starter-test'
// Swagger UI
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
// Swagger 2
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation group: 'org.postgresql', name: 'postgresql'
// https://mvnrepository.com/artifact/org.flywaydb/flyway-core
implementation group: 'org.flywaydb', name: 'flyway-core'
// MapStruct
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
// https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jpamodelgen
annotationProcessor('org.hibernate:hibernate-jpamodelgen:6.0.0.Alpha5')
// https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
test
useJUnitPlatform()
testLogging
events "passed", "skipped", "failed"
现在,当您执行./gradlew build
时,它将生成一个正确的 Spring Boot jar,您可以在命令行上运行它。你也可以在你的 docker 镜像中使用这个 jar。
从 Spring Boot 2.3 开始,可以让 Spring Boot 使用 build packs 或 regular docker 创建映像。
使用构建包时,上面的build.gradle
足以创建镜像。只需运行./gradlew bootBuildImage
,它将使用构建包生成图像。
【讨论】:
以上是关于Gradle 无法构建具有 Docker 依赖项的 JAR的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Gradle 创建具有实现依赖项的可执行胖 jar
如何找到跨文件重命名和重新排序添加特定 Gradle 构建依赖项的 Git 提交?
在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?