Stop gradle task 'generateMetadataConstants' from running during gradle build

Hello,

I was wondering if anyone on the leaf team might have some insight on this. I have implemented a custom service registry and extended the bean metadata for it. I have a gradle task that runs during a gradle build, which ensures a leidos proprietary header is in the top of all files.

So the process works like this:

./gradlew licenseFormat

This will generate the header in all files.

Updating license header in: build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\Falc2onServiceMetadataConstants.java
Updating license header in: build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\MetadataTypes.java
./gradlew build

This will run a ‘generateMetadataConstants’ task, that then goes and overwrites a ‘MetadataTypes.java’ and a ‘Falc2onServiceMetadataConstants.java’. The issue is that these new files will not have the header.

Then a task runs that checks to ensure all files have the header, and will error here.

PS C:\Users\bostwickj\development\tmp\tmp\service-registry> ./gradlew build        
Unknown file extension: src\main\resources\META-INF\services\javax.el.ExpressionFactory
Missing header in: build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\Falc2onServiceMetadataConstants.java
Missing header in: build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\MetadataTypes.java
> Task :licenseMain FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':licenseMain'.
> License violations were found: C:\Users\bostwickj\development\tmp\tmp\service-registry\build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\Falc2onServiceMetadataConstants.java,C:\Users\bostwickj\development\tmp\tmp\service-registry\build\generated\src\main\java\com\leidos\falc2on\services\serviceregistry\generated\metadata\MetadataTypes.java}

I have tried adding this to my build.gradle to no avail. Suggested here

project.afterEvaluate {
    build.dependsOn -= generateMetadataConstants
}

Any ideas? I don’t want to remove the plugin completely, just make it so that task does not run during a gradle build

I should also note that the ‘generateMetadataConstants’ task is from a leaf plugin, here

Hi Jacob, one way to fix this is going into your build.gradle and adding

licenseFormat {
  dependsOn 'generateMetadataConstants'
  dependsOn 'generateLookupListConstants' // can potentially exclude this one if not needed
}
1 Like

The licenseFormat task does not run automatically, i.e. when gradle build is called. So doing this will work after a ./gradlew licenseFormat but then those files are overwritten, since licenseFormat does not run in a ./gradlew build but ‘generateMetadataConstants’ does.

So that’s why my current idea is figuring out a way to make the build task not depend on the generateMetadataConstants task

There are two options that I can think of

licenseFormat.mustRunAfter `generateMetadataConstants`
licenseFormat.mustRunAfter `generateLookupListConstants ` // again not sure if this one is needed or not

or

compileJava.dependsOn.remove(“generateMetadataConstants”)
1 Like

I have a minor clarification to make on the above suggestion. Note that the metadata plugin modifies compileJava to depend on these tasks in an afterEvalute block, so your code to remove those dependencies also needs to be in an afterEvaluate block.

afterEvaluate {
    compileJava.dependsOn.remove('generateMetadataConstants')
}

Also keep in mind the implications of removing those dependencies if you decide to do that. Based on your original post, it looks like you are using the default configuration for the metadata plugin which stores the output in the build directory. I assume that means you are not checking the constants files into source control. If that is the case, removing the dependency on generateMetadataConstants and generateLookupListConstants means that you will need to manually run those tasks when you check out the repository or switch branches if the BeanMetadata changed.

Also note that mustRunAfter does not add a dependency. It simply specifies order. If you want to make licenseFormat run after the other tasks, you may want to consider adding it as a dependency of compileJava in addition to the mustRunAfter lines above so the generation of the constants occurs first and then the licenses are added.

compileJava.dependsOn licenseFormat
2 Likes