Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

compilation - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...