Extract specific JARs from dependencies

前端 未结 2 1529

I am new to gradle but learning quickly. I need to get some specific JARs from logback into a new directory in my release task. The dependencies are resolving OK, but I ca

相关标签:
2条回答
  • 2021-01-04 04:17

    Configurations are just (lazy) collections. You can iterate over them, filter them, etc. Note that you typically only want to do this in the execution phase of the build, not in the configuration phase. The code below achieves this by using the lazy FileCollection.filter() method. Another approach would have been to pass a closure to the Tar.from() method.

    task release(type: Tar, dependsOn: war) {
        ...
        into('lib/ext') {
            from findJar('logback-core') 
            from findJar('logback-access')
        }
    }
    
    def findJar(prefix) { 
        configurations.runtime.filter { it.name.startsWith(prefix) }
    }
    
    0 讨论(0)
  • 2021-01-04 04:34

    It is worth nothing that the accepted answer filters the Configuration as a FileCollection so within the collection you can only access the attributes of a file. If you want to filter on the dependency itself (on group, name, or version) rather than its filename in the cache then you can use something like:

    task copyToLib(type: Copy) {
      from findJarsByGroup(configurations.compile, 'org.apache.avro')
      into "$buildSrc/lib"
    }
    
    def findJarsByGroup(Configuration config, groupName) {
      configurations.compile.files { it.group.equals(groupName) }
    }
    

    files takes a dependencySpecClosure which is just a filter function on a Dependency, see: https://gradle.org/docs/current/javadoc/org/gradle/api/artifacts/Dependency.html

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