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:21

It's quite common to have runtime dependencies that aren't compile time dependencies. The other way around is a fairly special case and as such requires a few lines of configuration in Gradle. I suggest to search the Gradle forum for provided.

It sounds like what you are really after is declaring dependencies for your build, not for the compile class path. How this is done depends on how the desired functionality gets invoked (Ant task, Gradle task/plugin, ad-hoc use from build script). If you provide more detailed information on what you are trying to do, I can provide a more specific answer.

Here are some links to relevant information in the Gradle user guide:

查看更多
何必那么认真
3楼-- · 2019-01-10 11:23

I figured it out for my project setup. I use Android Studio running with the gradle plugin 0.9.+ with gradle 1.11 The main project uses amazon ads and amazon inapp purchases. It depends on a library project using amazon device messaging(ADM).

My main issue was with the the ADM where I got the "RuntimeException: Stub!" error.

1.) Library Project: The "provided configuration" proposed by Lukas does not work, as stated by him, so I used Richards approach, which however did not work as well out of the box. I had to change it a little since I could not find the lib in the ext_libs folder of the aar file. Gradle seems to pack all libraries in the libs folder in the in the final aar-file.

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

2.) Application Project : Here, the approach with the "provided configuration" worked.

configurations{
    provided
} 
dependencies {
    compile 'fr.avianey:facebook-android-api:+@aar'
    compile files('ext_libs/amazon-ads-5.3.22.jar')
    compile files('ext_libs/in-app-purchasing-1.0.3.jar' )
    provided files('ext_libs/amazon-device-messaging-1.0.1.jar')
}

android.applicationVariants.all {
    variant -> variant.javaCompile.classpath += configurations.provided
}
查看更多
小情绪 Triste *
4楼-- · 2019-01-10 11:23

In Gradle 2.12 a compileOnly configuration has been introduced. A blog post introducing this features can be found here:

Gradle latest feature: Compile only dependencies

Please be aware of one important side effect:

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.

查看更多
做个烂人
5楼-- · 2019-01-10 11:24

If you use the WAR plugin, you can use providedCompile as in this example

dependencies {
    compile module(":compile:1.0") {
        dependency ":compile-transitive-1.0@jar"
        dependency ":providedCompile-transitive:1.0@jar"
    }
    providedCompile "javax.servlet:servlet-api:2.5"
    providedCompile module(":providedCompile:1.0") {
        dependency ":providedCompile-transitive:1.0@jar"
    }
    runtime ":runtime:1.0"
    providedRuntime ":providedRuntime:1.0@jar"
    testCompile "junit:junit:4.11"
    moreLibs ":otherLib:1.0"
}
查看更多
看我几分像从前
6楼-- · 2019-01-10 11:28

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.

查看更多
登录 后发表回答