DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Gradle Compile-only Dependencies

Gradle Compile-only Dependencies

Compile-only dependencies can now be declared for all Java projects using the Java plugin from Gradle.

Mark Vieira user avatar by
Mark Vieira
·
Apr. 05, 16 · Tutorial
Like (4)
Save
Tweet
Share
19.69K Views

Join the DZone community and get the full member experience.

Join For Free

One of the most highly anticipated Gradle features has arrived in Gradle 2.12, support for declaring compile only dependencies (release notes here). For Java developers coming from Maven, compile only dependencies function similarly to Maven’s “provided” scope, allowing you to declare non-transitive dependencies used only at compilation time. While a similar capability has been available for users of the Gradle War Plugin, compile only dependencies can now be declared for all Java projects using the Java Plugin.

These can be leveraged for a number of use cases, including, but not limited to:

  • Dependencies required only during source compilation, such as source only annotations or annotation processors
  • Optional dependencies which are required at compilation time, but only needed at runtime only when using certain application features
  • Dependencies whose API is needed at compile time, but an implementation is expected to be provided by the project’s consumer or runtime environment

Compile only dependencies are distinctly different than regular “compile” dependencies. They are not included on the runtime classpath and are non-transitive, meaning they are not included in dependent projects. This is true when using Gradle project dependencies and also when publishing to Maven or Ivy repositories. In the latter case, compile only dependencies are simply omitted from published metadata.

As part of our goal of quality IDE support, compile only dependencies continue to work with Gradle’s IDEA and Eclipse plugins. When used in conjunction with IntelliJ IDEA, compile only dependencies are mapped to IntelliJ’s “provided” scope. With Eclipse, compile only dependencies are not exported via project dependencies.

In the Gradle model we consider tests to be a “consumer” of the production code. With this in mind, compile only dependencies are not inherited by the test classpath. The intention is that tests, like any other runtime environment, should provide their own implementation, either in the form of mocks or some other dependency.

Declaring compile only dependencies is simple, just assign dependencies to the new “compileOnly” configuration for the appropriate source set.

dependencies{

compileOnly 'javax.servlet:servlet-api:2.5'

As a result of the addition of the “compileOnly” configuration, the “compile” configuration no longer represents a complete picture of all compile time dependencies. When needing reference a compile classpath in build scripts or custom plugins, the appropriate source set’s compileClasspath property should be used instead.

For more information be sure to check out the Java Plugin chapter of the Gradle user guide and as always, we welcome any feedback and questions. Feel free to tell us what you think about this, or any other topic, at discuss.gradle.org.

Original article by Mark Vieira

Dependency Gradle

Published at DZone with permission of Mark Vieira, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Public Key and Private Key Pairs: Know the Technical Difference
  • Choosing the Right Framework for Your Project
  • 3 Main Pillars in ReactJS
  • Introduction to Spring Cloud Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: