首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle包括目录中的传递依赖项。

Gradle包括目录中的传递依赖项。
EN

Stack Overflow用户
提问于 2019-10-15 13:52:09
回答 2查看 1.9K关注 0票数 3

我有一个java项目,它有几个依赖项,例如:

代码语言:javascript
复制
dependencies {
    compile("com.whatever.jar:jar-1:1.2.3")
    compile("com.whatever.jar:jar-2:4.5.6")
    compile("com.whatever.jar:jar-3:7.8.9")
} 

我将其打包为jar库,并将其与描述这些依赖关系的POM文件一起发布到存储库中:

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.my.project</groupId>
    <artifactId>my-library</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.whatever.jar</groupId>
            <artifactId>jar-1</artifactId>
            <version>1.2.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.whatever.jar</groupId>
            <artifactId>jar-2</artifactId>
            <version>4.5.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.whatever.jar</groupId>
            <artifactId>jar-3</artifactId>
            <version>7.8.9</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

我还有另一个项目,它依赖于编译时以前构建的jar:

代码语言:javascript
复制
dependencies {
    compileOnly("com.my.project:my-library:1.0.0")
}

我希望创建一个任务,该任务构建第二个项目,并创建一个包含com.my.project:my-library:1.0.0 jar及其传递依赖项(jar-1jar-2jar-3)的zip文件。

代码语言:javascript
复制
task createZip << {
    into('lib') {
        // here I would like to do something like (it's a horrible pseudo-code):
        from configurations.(com.my.project:my-library:1.0.0).jar.transitiveDependencies.files
    }
}

我怎样才能做到这一点?

编辑:我还想以编程的方式访问my-library的传递依赖项列表(确切地说是那些依赖项,而不是项目中的任何其他依赖项),以便使用这些依赖项构建Loader-Path清单属性。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-21 20:55:28

下面是一个独立的build.gradle脚本,它演示了如何创建您的createZip任务(它只向ZIP存档添加特定的依赖项):

代码语言:javascript
复制
/* make the "compileOnly" configuration available */
plugins {
    id 'java'
}

/* add some demo dependencies to the project */
repositories {
    jcenter()
}
dependencies {
    compileOnly 'junit:junit:4.12'
    compileOnly 'org.slf4j:slf4j-simple:1.7.28'
}

task createZip(type: Zip) {
    // the "lib" directory in the ZIP file will only have all those dependency
    // files of the "compileOnly" configuration which belong to the
    // "junit:junit:4.12" module:
    def depOfInterest = dependencies.create('junit:junit:4.12')
    into('lib') {
        from configurations.compileOnly.resolvedConfiguration.getFiles({dep ->
            dep == depOfInterest
        })
    }

    // the "justForDemonstration" directory in the ZIP file will have all
    // dependency files of the "compileOnly" configuration, incl. SLF4J:
    into('justForDemonstration') {
        from configurations.compileOnly
    }
}

这可以在./gradlew createZip中运行(用Gradle 5.6.2进行测试)。它在build/distributions/<your_project_name>.zip下生成ZIP文件。如果你的伪码是这么想的,请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2019-10-16 22:40:27

代码语言:javascript
复制
task uberJar(type: Jar) {
   dependsOn classes
   from sourceSets.main.runtimeClasspath.collect { it.directory?fileTree(it):zipTree(it)}
   classifier = 'uber-jar'
} 
assemble.dependsOn uberJar
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58396335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档