Add jar to exported classpath

I have a task that is producing a jar outside of the java plugin (running an external tool). I would like it so any project that includes the project that gens this jar to use it as part of their classpath. I don’t want to just add this jar to the dependencies section, as it is being generated by a task in this project (not pre-gen).

I would like to, on my consumer project, just do this and have it compile against the produced jar, just as if gradle had compiled it (Well, not exactly, since gradle pulls in the classes folder, and this is a jar, but functionally the same):

apply plugin: JavaPlugin
dependencies {
   compile ":ExternalWrappedProject"
}

In the external project, I have something like:

task genProject //Makes a jar
  //This would seem to be the way to do this, but no luck
Configuration compileConfig = project.getConfigurations().add(JavaPlugin.COMPILE_CONFIGURATION_NAME);
 artifacts {
   compileConfig file new File(project.buildDir, "someOutputJar"), builtBy: genProject
}
  task compileJava (dependsOn: genProject);

This works to have my genProject code called at the right time, but I am not getting the jar onto the classpath.

Hello Benjamin,

you can use what we call a “buildable filecollection” for that:

dependencies
    compile files("$buildDir/someGeneratedOutput") {
        builtBy 'genProject'
    }
}

Have a look at the “file dependencies” section in the gradle userguide for the details: http://www.gradle.org/docs/current/userguide/dependency_management.html

cheers, René

Try:

artifacts {
  runtime(file("$project.buildDir/someJar.jar")) {
     builtBy genProject
   }
 }

Make sure that the Jar path is correct, and that you reference the project from another project like this:

dependencies {
  compile project(":foo")
}

Both of these methods work if the java plugin is applied to the project producing the jar. However, I was hoping to not apply that plugin, as it does no work on the project (the jar is created by an external utility), and so adding all the tasks from the Java plugin is silly, as they do nothing.

Since adding the plugin got it working, I’ll leave it, but it means that when java builds happen, this project run empty tasks, which could possibly cause confusion later down the road :frowning:

I don’t see why the producing project would have to apply the ‘java’ plugin. Should be good enough to apply the ‘base’ or ‘java-base’ plugin, and add the artifact to the ‘default’ configuration (or your own).

PS: A project dependency on a ‘java’ project does get resolved to the Jar (not the classes folder).

The following case (Applying Java Plugin and using one of its configurations) works

apply plugin: 'java'
artifacts {
   //also works with compile
   runtime(artifact)
}

The following do not work:

apply plugin: 'java'
artifacts {
   archives(artifact)
}
// base also does not work
apply plugin: 'java-base'
artifacts {
   archives(artifact)
}
// base also does not work
apply plugin: 'java-base'
configurations {
   // or compile
   runtime
}
artifacts {
   // or compile
   runtime(artifact)
}

In all these cases, by ‘not work’ I mean that the project compiles fine but the consumer projects gets compile errors (jar not on classpath).

Yea, I see it does get the full jar, I misread the debug output, sorry :slight_smile: