3

背景:

我目前有一个多模块(多项目)应用程序存储库。“根”不是可运行的应用程序。它只是我有一个根build.gradle.kts文件的源目录,其中包含我所有子项目之间通用的依赖项和插件。我的每个子项目都有自己的build.gradle.kts.

所以我的整体项目结构看起来像这样:

my_root_project
    - gradle
        - wrapper
            - gradle-wrapper.jar
            - gradle-wrapper.properties
    - gradle.build.kts
    - settings.gradle.kts
    - my_nested_project_a
        - src
            - main
                - kotlin
    - my_nested_project_b
        ...

问题:

每次我运行时gradle build,我都会收到一条错误消息:

> Task :bootJar FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootJar'.
> Main class name has not been configured and it could not be resolved

然而,当我运行我的任何一个子项目(例如build :my_nested_project_a:build)时,它构建得很好。

当前 Gradle 构建文件

这是我目前在“根”中的内容gradle.build.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "com.example"
version = "1.0.0"
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

plugins {
    id("org.springframework.boot") version "2.1.8.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.8.RELEASE" apply false
    kotlin("jvm") version "1.3.50"
    kotlin("plugin.spring") version "1.3.50"
    kotlin("plugin.jpa") version "1.3.50"
    kotlin("plugin.allopen") version "1.3.50"
}

allprojects {
    repositories {
        maven(url = "https://my.company.com/repo/with/all/the/stuff/I/need")
    }

    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "java")
    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
    apply(plugin = "org.jetbrains.kotlin.plugin.spring")

    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "1.8"
        }
    }
}

注意:我apply false在我的插件上使用,因为我认为它会阻止 gradle 在使用gradle build.

我正在尝试做的事情:

我有一个 CI 管道,我想简单地运行gradle build它应该build为所有子项目运行任务。但是,在那个过程中,我想忽略为“根”项目运行构建或绕过它,因为它不是一个可运行的应用程序,而只是构建子项目。

任何帮助将不胜感激!:)

4

4 回答 4

2

如果要忽略任务bootJar,请添加以下配置。

bootJar {
    enabled = false
}
于 2019-10-07T20:12:09.557 回答
0

在您的 root中,使用Kotlin DSLbuild.gradle.kts忽略 bootJar任务:

import org.springframework.boot.gradle.tasks.bundling.BootJar

tasks.getByName<BootJar>("bootJar") {
    enabled = false
}
于 2019-11-18T22:29:18.373 回答
0

我有类似的设置和问题。我替换allprojectssubprojects添加jar.enabled(false)到根 build.gradle 文件,它工作。

plugins {
    id("java-library")
    id('org.jetbrains.kotlin.jvm') version "${kotlinVersion}"
    id("com.diffplug.spotless") version "${spotlessVersion}"
    id("maven-publish")
}

jar.enabled(false)

subprojects {
    apply plugin: "java-library"
    apply plugin: "org.jetbrains.kotlin.jvm"
    apply plugin: "com.diffplug.spotless"
    apply plugin: "maven-publish"

    group = GROUP
    version = VERSION_NAME

    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    dependencies {
        testImplementation 'org.jetbrains.kotlin:kotlin-test'
        testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
    }

    publishing {
        publications {
            library(MavenPublication) {
                from components.java
            }
        }
        repositories {
            maven {
                url "https://gitlab.mhighpoint.com/api/v4/projects/${System.getenv('CI_PROJECT_ID')}/packages/maven"
                credentials(HttpHeaderCredentials) {
                    name = "Job-Token"
                    value = System.getenv('CI_JOB_TOKEN')
                }
                authentication {
                    header(HttpHeaderAuthentication)
                }
            }
        }
    }

    spotless {
        java {
            googleJavaFormat() // googleJavaFormat('1.1') to specify a specific version
        }
        kotlin {
            target '**/src/**/*.kt'
            ktlint("0.41.0").userData('disabled_rules': 'no-wildcard-imports,import-ordering')

            trimTrailingWhitespace()
            indentWithSpaces()
            endWithNewline()
        }
        format 'misc', {
            target '**/*.gradle'

            trimTrailingWhitespace()
            indentWithSpaces() // or spaces. Takes an integer argument if you don't like 4
            endWithNewline()
        }
    }

    test {
        useJUnitPlatform()
    }

    jar {
        archiveBaseName = "${rootProject.name}-${project.name}"
    }

    tasks {
        assemble.dependsOn(spotlessApply)
    }
}
于 2021-09-01T14:02:56.150 回答
0

如果您在allprojects会话中应用了插件,那么您也将其应用到根目录,并且由于它是第一个在 中解决的gradle build,您应该在那里配置主类。

或者,您可以apply(plugin = "org.springframework.boot")从根目录中删除该行,并将插件仅应用于具有main注解方法的模块,@SpringBootApplication并将插件指向那里的主类。

假设您的主要课程在my_nested_project_a/src/main/com/example/MainClass.kt.

my_nested_project_a/build.gradle.kts应该看起来像:

plugins {
    id("org.springframework.boot")
}

springBoot {
    mainClassName = "com.example.MainClass"
}

dependencies {
    ...
}

你应该从根目录中删除这一行build.gradle.kts

apply(plugin = "org.springframework.boot")
于 2019-11-29T17:52:03.123 回答