0

INTRODUCTION

The project is in Kotlin and builds using Gradle. I'm trying to generate a basic data class with some build info, let's say for now that I need it [re]generated every time before running.

Here's the Gradle task I have now:

def generatedDir = "$buildDir/generated"

// noinspection GroovyAssignabilityCheck
task generateBuildInfo {
    inputs.property "version", rootProject.version.toString()
    inputs.property "name", rootProject.name.toString()

    outputs.dir generatedDir
    outputs.upToDateWhen { false }

    doFirst {
        def buildInfoFile = file("$generatedDir/BuildInfo.kt")
        buildInfoFile.parentFile.mkdirs()
        buildInfoFile.text = """
        internal data class BuildInfo(
          val version: String = "${project.version.toString() ?: "unspecified"}",
          val name: String = "${project.name.toString() ?: "unspecified"}"
        )
        """.replace("        ", "").trim()
    }
}

To be able to resolve this from IntelliJ IDEA, I added my new folder to project sources, and obviously wired up the dependencies, like so:

sourceSets.main.kotlin.srcDirs += generatedDir

project.afterEvaluate {
    compileJava.dependsOn generateBuildInfo
    compileKotlin.dependsOn generateBuildInfo
}

This is all done in a separate file (to avoid polluting my main scripts). Due to this organization, after applying plugins, I just include the generator in my main script, like this:

apply from: "gradle/scripts/build-info-generator.gradle"

THE PROBLEM

It looks like the generator code is executed only once, after running assemble when I first ran clean on this module. This is not what I want, because when I change some of the project properties (like version), the source does not get updated... as if compileJava/compileKotlin and my custom task are not executed.

They do not appear in build logs as executed.

Is there any way I can run this task every time I want to run my module's launcher? Sure, I can do some smart file comparison to see if generation is needed, but for now I just want it done each time. Am I missing something?

milosmns
  • 3,595
  • 4
  • 36
  • 48
  • If I understand, correctly, you expect IntelliJ to run your gradle tasks when it internally builds and runs the project? That won't happen unless you explicitly configure it in IntelliJ, or if you configure IntelliJ to delegate the build/run actions to gradle. Otherwise, Gradle has its own build and run system. It just loads the dependencies and some other properties from the Gradle build when loading the project. – JB Nizet Mar 23 '19 at 14:34
  • @JBNizet - Well, sort of. I expect IntelliJ to run `compileKotlin` in this case, thus running my task which `compileKotlin` depends on. Is this not a reasonable expectation? – milosmns Mar 23 '19 at 14:37
  • As I just said, no. IDEA has its own build system, indepenant from Gradle. You can configure it to run a Gradle task before its own build task. You can also configure it to delegate all the build/run tasks to Gradle. But that's not the default. – JB Nizet Mar 23 '19 at 14:39
  • @JBNizet - Actually I just realized a very interesting thing... if I run the "Build" command from IntelliJ, it does not run my task at all (not even after `clean`). Since my Run configuration is currently set up to run this IntelliJ's `Build` task before running (default setup), my task is thus not executed. – milosmns Mar 23 '19 at 14:43
  • So is this `Build` task from IntelliJ executing **any** Gradle tasks from my build script? – milosmns Mar 23 '19 at 14:45
  • That's what I just told you: IDEA has its own build system, independant from Gradle. It just loads the dependencies and some other properties from the Gradle build when loading the project. – JB Nizet Mar 23 '19 at 14:45
  • Ok, just needed to confirm this one more time, because it's kinda unexpected. But looks like this is exactly what is happening. Thanks! (you can answer if you want, I'll accept it) – milosmns Mar 23 '19 at 14:47

1 Answers1

2

IDEA has its own build system, indepenant from Gradle.

You can configure it to run a Gradle task before its own build task.

You can also configure it to delegate all the build/run tasks to Gradle. But that's not the default.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255