2 个不同的 gradle 构建:一个 spring 引导和一个非 spring 引导可执行文件
2 different builds with gradle: one spring boot and one non sping boot executables
我是 gradle (6.4.1) 的新手并启动了一个 CLI 可执行 jar。
它曾经有效。
然后我使用 spring 引导在 CLI 旁边添加了一个 REST 接口。 Spring 启动正常,但 CLI 可执行文件 jar 已损坏。它总是生成 spring 引导可执行文件。我需要保留这两种可执行方法。
为了解决这个问题,我创建了一个专用于 CLI 可执行文件的单独构建文件,但 jar 不可执行并且出现错误 "no main manifest attribute".
能否请您提供指导以解决我的需求。
tldr;
为了使用第二个构建文件成功构建,我从 spring 引导构建文件中获取了所有依赖项,但将 spring 引导插件切换为 Maven BOM 方法以避免生成 spring仅引导可执行文件。
Spring 启动构建文件
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
jar {
manifest {
attributes 'Main-Class': 'console.ConsoleExercices'
}
}
group = 'be.challenge'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.4.0'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile group: 'org.apache.camel', name: 'camel-core', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-swagger-java', version: '3.4.0'
testCompile group: 'org.apache.camel', name: 'camel-test', version: '3.4.0'
compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
}
test {
useJUnit()
}
可执行的 CLI jar 构建文件
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE' apply false
id 'war'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'be.console.ConsoleExercises'
}
archivesBaseName = 'console-exercises'
}
group = 'be.challenge'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
jcenter()
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.4.0'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile group: 'org.apache.camel', name: 'camel-core', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-swagger-java', version: '3.4.0'
testCompile group: 'org.apache.camel', name: 'camel-test', version: '3.4.0'
compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
}
test {
useJUnit()
}
CLI 项目应该使用 application
插件。它将确保它可以作为常规 JAR/CLI 应用程序完全运行。
除此之外,使用 implementation
而不是 compile
。 compile
配置早已被弃用,不应再使用。
disables the default JAR archive generation of the Java plugin
删除 'war' 插件以恢复 jar 存档行为。
我是 gradle (6.4.1) 的新手并启动了一个 CLI 可执行 jar。
它曾经有效。
然后我使用 spring 引导在 CLI 旁边添加了一个 REST 接口。 Spring 启动正常,但 CLI 可执行文件 jar 已损坏。它总是生成 spring 引导可执行文件。我需要保留这两种可执行方法。
为了解决这个问题,我创建了一个专用于 CLI 可执行文件的单独构建文件,但 jar 不可执行并且出现错误 "no main manifest attribute".
能否请您提供指导以解决我的需求。
tldr;
为了使用第二个构建文件成功构建,我从 spring 引导构建文件中获取了所有依赖项,但将 spring 引导插件切换为 Maven BOM 方法以避免生成 spring仅引导可执行文件。
Spring 启动构建文件
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
jar {
manifest {
attributes 'Main-Class': 'console.ConsoleExercices'
}
}
group = 'be.challenge'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
jcenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.4.0'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile group: 'org.apache.camel', name: 'camel-core', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-swagger-java', version: '3.4.0'
testCompile group: 'org.apache.camel', name: 'camel-test', version: '3.4.0'
compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
}
test {
useJUnit()
}
可执行的 CLI jar 构建文件
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE' apply false
id 'war'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'be.console.ConsoleExercises'
}
archivesBaseName = 'console-exercises'
}
group = 'be.challenge'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
jcenter()
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.4.0'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile group: 'org.apache.camel', name: 'camel-core', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.4.0'
compile group: 'org.apache.camel', name: 'camel-swagger-java', version: '3.4.0'
testCompile group: 'org.apache.camel', name: 'camel-test', version: '3.4.0'
compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0'
}
test {
useJUnit()
}
CLI 项目应该使用 application
插件。它将确保它可以作为常规 JAR/CLI 应用程序完全运行。
除此之外,使用 implementation
而不是 compile
。 compile
配置早已被弃用,不应再使用。
disables the default JAR archive generation of the Java plugin
删除 'war' 插件以恢复 jar 存档行为。