Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a compile-time *only* classpath in Gradle?

Can someone please give me a simple build.gradle example of how I can specify compile-time-only classes that are not included in the runtime deployment (war).

Gradle seems to have gotten this the wrong way around since 'runtime' inherits from 'compile'. I can't imagine a situation where I would want classes at runtime that I wouldn't want at compile time. However, there are many circumstances where I need classes to generate code at compile time that I do not wish to deploy at runtime!

I've ploughed through the bloated gradle documentation but cannot find any clear instructions or examples. I suspect this might be achieved by defining a 'configuration' and setting it as the classpath of the CompileJava plugin - but the documentation falls short on explaining how to achieve this.

like image 347
Alex Worden Avatar asked May 01 '12 23:05

Alex Worden


1 Answers

There has been a lot of discussion regarding this topic, mainly here, but not clear conclusion.

You are on the right track: currently the best solution is to declare your own provided configuration, that will included compile-only dependencies and add to to your compile classpath:

configurations{   provided }  dependencies{   //Add libraries like lombok, findbugs etc   provided '...' }  //Include provided for compilation sourceSets.main.compileClasspath += [configurations.provided]  // optional: if using 'idea' plugin idea {   module{     scopes.PROVIDED.plus += [configurations.provided]   } }  // optional: if using 'eclipse' plugin eclipse {   classpath {     plusConfigurations += [configurations.provided]   } } 

Typically this works well.

like image 106
rodion Avatar answered Oct 08 '22 09:10

rodion