Hi, I’m still pretty new to Gradle, and have been having some trouble with artifacts. I have a multi-project build with about 33 sub-projects. Some of these projects have to create their own archive files after the assemble has finished. For some reason I haven’t been able to consistently get this to work. Sometimes one or two projects don’t produce the extra jar file and I’m assuming that they are running my jar task before the compile task has completed (since I get no errors).
Maybe I’m just misunderstanding the proper way to do this in Gradle. Here is an example from one of these projects:
What’s producing the class files? If it’s a JavaCompile task, you should be able to do:
task createClassesJar(type: Jar) {
// other stuff above from() in original code
from(nameOfTheCompileTask)
}
The ArtifactHandler understands that archives that come from particular tasks require dependencies, so if you just use the task name it’ll translate that into a file + dependency:
artifacts {
archives createClassesJar
}
I think that also makes assemble depend on the task that creates the jar too.
It would be great if I could get that to work. I didn’t quite realize that you should be able to specify tasks to the from property of the Jar obj, or to the archives property.
However right now when I say from(JavaCompile) in the jar task I get and error:
Cannot convert the provided notation to a File or URI: class org.gradle.api.tasks.compile.JavaCompile.
The following types/formats are supported:
A String or CharSequence path, e.g ‘src/main/java’ or ‘/usr/include’
A String or CharSequence URI, e.g ‘file:/usr/include’
It still doesn’t like it. Gradle says that javaCompile is not a property of the createClassesJar task. I’ve seen this error a lot over the past few days. Here is the current task definition:
Using the com.android.application plugin. Ok, I guess using this plugin removes the compileJava task? There is a compileReleaseJava task listed, however if I use that in the from property I still get the error message about compileReleaseJava not being a valid property
Ah, that’s the culprit. Because Android projects can have multiple variants, there are multiple compile tasks (at least two, debug and release). You’ll want to additionally create a JAR task for each of these.
Hey Mark thanks for all the help yesterday. I’m still not able to get the whole thing to run. how do I get the archives to refer to the tasks created in the applicationVariants loop?
If I use this
artifacts {
archives debugClassesJar
}
I once again get the error message about not finding the property debugClassesJar
It goes further, but still isn’t happy, returns an error about not being able to convert the provided notation to an object of type PublishArtifact: debugClassesJar.