【问题标题】:2 different builds with gradle: one spring boot and one non sping boot executables2 种不同的 gradle 构建:在 spring boot 和一个非 spring boot 可执行文件中
【发布时间】:2020-11-09 02:51:30
【问题描述】:

我是 gradle (6.4.1) 的新手并启动了一个 CLI 可执行 jar。
它曾经工作过。
然后我使用 spring boot 在 CLI 旁边添加了一个 REST 接口。 Spring boot 正在运行,但 CLI 可执行 jar 已损坏。它总是产生一个弹簧启动可执行文件。我需要保留这两种可执行的方法。
为了解决这个问题,我创建了一个专用于 CLI 可执行文件的单独构建文件,但 jar 不是可执行文件,并且出现 “no main manifest attribute” 错误。
您能否为我提供指导以解决我的需求。

tldr;
为了使用第二个构建文件成功构建,我从 spring boot 构建文件中获取了所有依赖项,但将 spring boot 插件切换为 maven BOM 方法以避免生成 spring boot 唯一可执行文件。

Spring Boot 构建文件

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()
}

【问题讨论】:

    标签: spring-boot gradle build.gradle executable-jar


    【解决方案1】:

    CLI 项目应该使用application 插件。它将确保它可以作为常规 JAR/CLI 应用程序完全运行。

    除此之外,使用implementation 而不是compilecompile 配置早已被弃用,不应再使用。

    【讨论】:

    • 谢谢@francisco-mateo。使用应用程序插件时我仍然遇到同样的问题。当运行 gradle -b build-console.gradle clean assemble 并尝试执行 jar $ java -jar build/libs/multiplicationtable-0.0.1-SNAPSHOT.jar 时,它会返回错误 no main manifest attribute, in build/libs/multiplicationtable-0.0.1-SNAPSHOT.jar。但是,使用 run 任务运行会产生执行主类的预期行为的开始,但 CLI 是交互式的,这不会阻止等待用户输入
    • 感谢 @fransisco-mateo 对 compile 弃用的评论
    【解决方案2】:

    war plugin

    disables the default JAR archive generation of the Java plugin
    

    移除 'war' 插件以恢复 jar 存档行为。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2018-05-26
      • 2017-08-10
      • 2018-08-29
      • 1970-01-01
      相关资源
      最近更新 更多