Creating a compileOnly dependency in Gradle [Updated]

Today I would like to show you, how to add a Maven-provided style configuration to a Gradle build.

Sometimes in a projects lifecycle you may encounter the situation, that you have a dependency that is only required during compile time, some annotations for example. But Gradles java plugin doesn’t supports this out of the box. The war plugin add’s this feature, but also additional tasks that aren’t required in a plain java project.

To add this feature to your build you must simply

  1. Add a new configuration
  2. Add the dependencies from this configuration to the compileClasspath of your source sets (at least main  and test )

  3. Tell your IDE that their are additional dependencies
    • IDEA
    • Eclipse

Now you can use it like compile or test configuration in the dependencies section


4 Kudos

Leave a Reply

Your email address will not be published. Required fields are marked *