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

2019-01-10 10:45发布

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.

11条回答
走好不送
2楼-- · 2019-01-10 11:06

If you use the war plugin, providedCompile should do the trick. However if you need to exclude dependencies from being included in a jar, you'll have to extend the jar task. Below is an example for building a "fat jar" or "uber jar" (a single jar that includes all classes of its dependencies) excluding depndencies marked provided:

configurations {
    provided
    compile.extendsFrom provided
}

dependencies {
    provided "group.artifact:version"
    compile "group.artifact:version"
}

jar {
    dependsOn configurations.runtime
    from {
        (configurations.runtime - configurations.provided).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    } 
}

Credit: http://kennethjorgensen.com/blog/2014/fat-jars-with-excluded-dependencies-in-gradle/

Update:

As of Gradle 2.12 the issue of defining compile only dependencies is finally solved in a simple and natural manner by the new "copmpileOnly" configuration:

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
}
查看更多
forever°为你锁心
3楼-- · 2019-01-10 11:07

It turns out that they have added a "provided" configuration in the gradle android plugin 0.8.0 but it doesn't quite work. It does add the provided libraries to the compile path automatically, but it also includes them in the final aar/apk.

What worked for me was the solution provided by @lukas-hanaceck but by changing the name from "provided" to any other custom name. In my case this is a library project which is a dependency for my final android application project. Heres a gist of what worked for me.

configurations {
   providedlibs
}

dependencies {
   providedlibs files('provided/library.jar')
}

libraryVariants.all {
    variant -> variant.javaCompile.classpath += configurations.providedlibs
}

It compiles perfectly and the provided/library.jar is not included in the final apk. The only issue I am having is notifying Android studio of the existence of library.jar. The idea plugin doesn't seem to work for Android studio. Im guessing that they have another custom plugin for syncing gradle with studio.

查看更多
再贱就再见
4楼-- · 2019-01-10 11:11

We don't need "provided", try to add this:

android.libraryVariants.all { variant ->
    variant.packageLibrary.exclude( 'ext_libs/amazon-device-messaging-1.0.1.jar' )
}

Enjoy!

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-10 11:11

The OP apparently didn't look for an Android answer, but some answers are specific to Android. So I suggest you look at this page : http://tools.android.com/tech-docs/new-build-system

Version 0.9.0 introduced a provided scope. So, just use

dependencies {
    provided "groupId:artifcatId:version"
}
查看更多
虎瘦雄心在
6楼-- · 2019-01-10 11:17

I didnt find a solution for Android Studio, but this what I tried:

In android studio I had to update to version 0.5.+

in gradle/gradle-wrapper.properties replace

distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-rc-3-bin.zip

by

distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip

in all my build.gradle replace

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}

by

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}

and in the library I wanted to use provided

configurations {
    provided
}

//put applicationVariants in case it is apply plugin: 'android' and not apply plugin: 'android-library'
android.libraryVariants.all {
    variant -> variant.javaCompile.classpath += configurations.provided
}

dependencies {
    provided files('ext_libs/amazon-device-messaging-1.0.1.jar')
}

and at the end it doesnt work, it seems that it works for jar but not for aar or apk as stated here https://groups.google.com/forum/#!topic/adt-dev/WIjtHjgoGwA

查看更多
干净又极端
7楼-- · 2019-01-10 11:20

In Android Studio 1.0 do this:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.packageLibrary.exclude('libs/someLib.jar')
    }
}
查看更多
登录 后发表回答