Custom task expects jar of library project, missing

Summary

My main project uses a library project and compiles happily.
But I also have a Groovy custom task that internally uses “project.configurations.compile.asPath”. This returns the jar version of the library project, but the jar is never created during the invocation so the task fails.
How can I tell Gradle to create the jar of the library before running the custom task? Or is there another way to solve this?

Some Details

The main project “MAIN” uses the library project “MYLIB” with the following settings.gradle:

rootProject.name = 'MAIN'
include 'MYLIB'
project(':MYLIB').projectDir = "../../somefolder/MYLIB" as File

The MYLIB is added to the MAIN compile classpath in build.gradle:

dependencies {
	compile project(':MYLIB')
}

The custom task uses a modified configuration “hibtools” in order to add a library:

configurations {
	hibtools {
		extendsFrom configurations.compile
	}
}
dependencies {
	hibtools 'org.hibernate:hibernate-tools:5.+'
}

The custom task “dbSchema” is defined as follows:

task dbSchema(dependsOn: [classes], type: com.ex.DbSchemaTask) {
	outputfilename = "some.sql"
}

The implementation of the custom task “com.ex.DbSchemaTask.groovy” uses the “hibtools” classpath:

ant.taskdef(name: 'hibernatetool',
	classname: 'org.hibernate.tool.ant.HibernateToolTask',
	classpath: project.configurations.hibtools.asPath
)

When I invoke the dbSchema task with “gradlew dbSchema”, the ant.taskdef method receives a classpath containing the jar version of the library “MYLIB\build\libs\MYLIB-0.0.7.jar” but that file is missing and the task fails.

Versions

gradlew -version

------------------------------------------------------------
Gradle 3.5
------------------------------------------------------------

Build time:   2017-04-10 13:37:25 UTC
Revision:     b762622a185d59ce0cfc9cbc6ab5dd22469e18a6

Groovy:       2.4.10
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_121 (Oracle Corporation 25.121-b13)
OS:           Windows 10 10.0 amd64

If you declare the hibtools configuration as an input to the task, then that task will have an implicit dependency on any tasks that generate the jars making up the classpath.

There’s some related discussion in Artifact builtBy task is not called when resolving a configuration

Thank you for the tip.
Indeed adding a dependency on the configuration triggered the creation of the missing jars, like so:

task dbSchema(dependsOn: [classes], type: com.ex.DbSchemaTask) {
    inputs.files configurations.hibtools;
	outputfilename = "some.sql"
}
1 Like

This has triggered a new problem though: the dependency on ‘org.hibernate:hibernate-tools:5.+’ is not added to the classpath anymore when the custom task is run, generating a

class org.hibernate.tool.ant.HibernateToolTask cannot be found

Confused.

You are probably calling project.configurations.hibtools.asPath in the constructor of your custom task. Don’t do that, that’s way too early and opaque to Gradle. Instead properly model this as an input to your task and assign that input in the build script.

MyTask extends DefaultTaks {
  @InputFiles
  FileCollection classpath
  
  @TaskAction
  def doStuff {
    //do stuff with classpath
  }
}
task myTask(type: MyTask) {
  classpath = configurations.mydeps
}