SourceSetOutput DSL example doesn't work as expected


(Ivo Houbrechts) #1

Hi,

I’m trying to get a generated class on the compile classpath by following the SourceSetOutput DSL example:

sourceSets {
  main {
    output.dir(generatedResources, builtBy: 'compileJs')
  }
}

The compileJs task uses Rhino to compile javascript into a class file before the java compilation starts, but the java compilation fails because the generatedResources dir is not in the compile classpath. Is this expected?

As workaround I deleted the sourceSets but added the directory explicitly to the classpath:

dependencies {
    compile files(generatedResources) { builtBy 'compileJs' }
}

But then I also had to add the generatedResources dir manually to the jar.

The sourceSets solution would look better if it worked :frowning:

Grtz, Ivo


(Perryn Fowler) #2

Hi Ivo,

I think you have two distinct problems here

  1. You need your Rhino generated classes to be included in your Jar.

The Jar will include the output of the main SourceSet. So as you found, you should be able to achieve this by considering your Rhino generated classes like special resources and adding them to the SourceSet output.

However, ALSO:

  1. You need the Rhino generated classes on the classpath when you compile your java classes.

The javaCompile task takes INPUT source from the SourceSet and generates classes that it places in the SourceSet OUPUT. It doesn’t really make sense to expect it to treat classes already in the output as part of its input. Instead you need to declare a compile dependency so that gradle knows those classes are needed to compile against.

So long story short, you need to do both things to get the effect you want

sourceSets {
  main {
    output.dir(generatedResources)
  }
}
  dependencies {
    compile files(generatedResources) { builtBy 'compileJs' }
}

(Ivo Houbrechts) #3

This was exactly what I was looking for. It does the same thing as what I had before, but now the intent is better communicated by the code :slight_smile:

Tnx