Exclude dependency from compile but not runtime

How can I exclude a transitive dependency from compile but not runtime? For example I would like to prevent commons logging from being used in our code, but some transitive dependencies (such as Spring Framework) require it.

I tried using
configurations { compile.exclude group: 'commons-logging', module: 'commons-logging' }
but that excludes it from runtime also.

You can exlude them from the compileClasspath configuration. Compilation will fail if they are used, but runtime will still work.

That worked nicely.

Thanks!