Is it possible to use classes compiled by Gradle buildSrc in main Groovy project?

不问归期 提交于 2020-12-05 07:27:05

问题


Classes compiled by buildSrc/build.gradle are not resolved at runtime when they are used in main PROJECT classes.

My Groovy project structure looks like this:
-PROJECT
    -buildSrc/
        -build.gradle
        -/src/main/groovy
            - com.company.global.test.report
    -src/test/groovy
    -build.gradle

Is there something I can add to the top-level PROJECT/build.gradle to allow the classes compiled by it to use the classes compiled by buildSrc/build.gradle?


回答1:


buildSrc is its own build (not project) that gets executed before the main build. Its sole purpose is to make some classes (plugins, tasks, regular classes) available to the build scripts of the main build. Hence you could call it a "meta-build".

Technically, it would be possible to add the compiled classes of buildSrc to the compile or runtime class path of a project in the main build, but I don't recommend to do it. There is very likely a better way to achieve your goals (but I don't know what those are).




回答2:


Here is how to do it with Gradle 2.12:

In your_project/buildSrc/build.gradle

task sourcesJar(type: Jar, dependsOn: classes) {
  classifier = 'sources'
  from sourceSets.main.allSource
}

// Thanks to this, IDE like IntelliJ will provide you with "Navigate to sources"
artifacts {
  archives sourcesJar
}

In your_project/build.gradle

ext.buildSrcJars = fileTree("$rootDir/buildSrc/build/libs") { include("*.jar") exclude("*sources.jar")}

// Works in every subproject
dependencies {
    compile buildSrcJars
}


来源:https://stackoverflow.com/questions/15930646/is-it-possible-to-use-classes-compiled-by-gradle-buildsrc-in-main-groovy-project

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