How to download dependencies in gradle

前端 未结 8 1294
暗喜
暗喜 2020-11-30 23:00

I have a custom compile task.

task compileSpeedTest(type: JavaCompile) {
    classpath = files(\'build\')
    source = fileTree(\'src/test/java/speed\')
             


        
相关标签:
8条回答
  • 2020-11-30 23:30

    A slightly lighter task that doesn't unnecessarily copy files to a dir:

    task downloadDependencies(type: Exec) {
        configurations.testRuntime.files
        commandLine 'echo', 'Downloaded all dependencies'
    }
    

    Updated for kotlin & gradle 6.2.0, with buildscript dependency resolution added:

    fun Configuration.isDeprecated() = this is DeprecatableConfiguration && resolutionAlternatives != null
    
    fun ConfigurationContainer.resolveAll() = this
      .filter { it.isCanBeResolved && !it.isDeprecated() }
      .forEach { it.resolve() }
    
    tasks.register("downloadDependencies") {
      doLast {
        configurations.resolveAll()
        buildscript.configurations.resolveAll()
      }
    }
    
    0 讨论(0)
  • 2020-11-30 23:30

    It is hard to figure out exactly what you are trying to do from the question. I'll take a guess and say that you want to add an extra compile task in addition to those provided out of the box by the java plugin.

    The easiest way to do this is probably to specify a new sourceSet called 'speedTest'. This will generate a configuration called 'speedTest' which you can use to specify your dependencies within a dependencies block. It will also generate a task called compileSpeedTestJava for you.

    For an example, take a look at defining new source sets in the Java plugin documentation

    In general it seems that you have some incorrect assumptions about how dependency management works with Gradle. I would echo the advice of the others to read the 'Dependency Management' chapters of the user guide again :)

    0 讨论(0)
  • 2020-11-30 23:33

    I have found this answer https://stackoverflow.com/a/47107135/3067148 also very helpful:

    gradle dependencies will list the dependencies and download them as a side-effect.

    0 讨论(0)
  • 2020-11-30 23:37

    There is no task to download dependencies; they are downloaded on demand. To learn how to manage dependencies with Gradle, see "Chapter 8. Dependency Management Basics" in the Gradle User Guide.

    0 讨论(0)
  • 2020-11-30 23:43

    Downloading java dependencies is possible, if you actually really need to download them into a folder.

    Example:

    apply plugin: 'java'
    
    dependencies {
      runtime group: 'com.netflix.exhibitor', name: 'exhibitor-standalone', version: '1.5.2'
      runtime group: 'org.apache.zookeeper',  name: 'zookeeper', version: '3.4.6'
    }
    
    repositories { mavenCentral() }
    
    task getDeps(type: Copy) {
      from sourceSets.main.runtimeClasspath
      into 'runtime/'
    }
    

    Download the dependencies (and their dependencies) into the folder runtime when you execute gradle getDeps.

    0 讨论(0)
  • 2020-11-30 23:44

    For Intellij go to View > Tool Windows > Gradle > Refresh All Projects (the blue circular arrows at the top of the Gradle window.

    0 讨论(0)
提交回复
热议问题