Gradle fails to build at Test

浪子不回头ぞ 提交于 2021-02-11 17:18:20

问题


I have a spring boot project that has multiple modules each with its own gradle file.

Spring boot version: 1.5.2.RELEASE

gradle version:3.4

  • server (which includes main class) and other configurations
  • shared (Utility classes used by other modules)
  • service (module with various repository and services)
  • transaction (module which handles transaction) I need to write test for the project but I cannot change the project structure. I created a test in my transaction module.

The parent gradle file is as such :

description = "mohen project $version"

defaultTasks "clean", "build"

buildscript {

    repositories {
        mavenLocal()
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}"
    }
}


apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'

subprojects {

    configurations {
        querydslapt
    }

    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'

    sourceCompatibility = javaVersion
    targetCompatibility = javaVersion

    repositories {
        mavenLocal()
        jcenter()

        maven {
            url "http://repo.spring.io/snapshot"
        }
        maven {
            url "http://repo.spring.io/milestone"
        }
    }
    dependencies {
        testCompile "org.springframework.boot:spring-boot-starter-test",
                "com.h2database:h2",
                "org.springframework.restdocs:spring-restdocs-mockmvc",
                "org.hamcrest:hamcrest-all:$hamcrestVersion",
                "org.powermock:powermock-api-mockito:1.7.3",
                "org.mockito:mockito-all:$mockitoVersion"
        testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.3'
        testCompile group: 'org.powermock', name: 'powermock-module-junit4-rule-agent', version: '1.7.3'

        compile "org.springframework.boot:spring-boot-starter-web",
                "org.springframework.boot:spring-boot-starter-data-jpa",
                "org.springframework.boot:spring-boot-starter-security",
                "org.springframework.ws:spring-ws-core:$wsReleaseVerison",
                "org.springframework.boot:spring-boot-devtools",
                "org.springframework.data:spring-data-redis:$redisVersion"
    }

    sourceSets {
        test {
            java {
                srcDir "${projectDir}/src/test/java"
            }
            resources {
                srcDir "${projectDir}/src/test/resources"
            }
        }

        generated {
            java {
                srcDirs = ['src/main/generated']
            }
        }
    }


    bootRepackage {
        mainClass = 'com.mohen.amatya.Application'
    }

}


task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
    gradleVersion = '3.4'
}

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
        sourceDirs += file('src/main/generated')
    }
}

and the subprojects gradle file are as such. The below is transaction modules build.gradle file.

jar {
    enabled = true
}
bootRepackage{
    enabled = false
}
dependencies {

    compile project(':service')
    compile project(':shared')

    testCompile project(':service').sourceSets.test.output
    testCompile project(':shared').sourceSets.test.output

}

The service depends on shared, and shared has no build.gradle file.

I added the bootRepackage enabled false as there was no bootJar for this version of gradle. The tests run file normally, or when I do.

./gradlew clean :transaction:compileTestJava

But when I build it normally using clean build. The test fails with errors like:

cannot find symbol

error: package com.mohen.amatya.shared.rest.user.schema does not exist

I am new to gradle and multi module, so I don't get what is wrong? I added the test srcDirs in the sourceSet of the parent gradle file as suggested by other posts. What is the reason that I get errors at compileTestJava?

来源:https://stackoverflow.com/questions/61058907/gradle-fails-to-build-at-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!