【问题标题】:Gradle downloading source dependenciesGradle 下载源依赖
【发布时间】:2016-09-13 20:24:48
【问题描述】:

在 gradle 中,我有以下 build.gradle 导致很多 JAR 被复制到“源”文件夹,但只有 jersey-media-moxy-2.22.2-sources.jar 实际上包含源代码:

defaultTasks 'run'

repositories {
    mavenCentral()
}

configurations {
    sources {
        description = 'sources download'
        transitive = true
    }
    copysource {
        extendsFrom sources
    }
}

dependencies {
    sources group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.22.2', classifier: 'sources'
}


task copySources(type: Copy) {
    println 'Copying dependencies to sources directory'
    into "sources/"
    from configurations.copysource
}

task run (dependsOn: ['copySources']){
    println 'Downloading JARs'
}

run << {
    println 'Downloads complete. Finished.'
}

如何修改构建以获取所有源代码(包括传递/依赖源)?我不想要非源 jars。我不明白为什么分类器不能传递,所以请澄清我的误解。

另外,我知道这不是使用 gradle 的最佳方式。这是(部分)临时步骤,直到我们迁移构建系统。

【问题讨论】:

  • 我也很想知道这个问题的答案。请接受我对迄今为止完全无用的答案的同情!

标签: java gradle


【解决方案1】:

Gradle 没有任何与依赖项的“源”配置相关的 API。 Chapter 7. Dependency Management Basics

答案也独立于集成开发环境。如果您尚未应用特定的 IDE 插件,则需要应用此插件。

IntelliJ

apply plugin: 'idea'
idea{
    module {
        downloadJavadoc = true // defaults to false
        downloadSources = true
    }
}

Eclipse

apply plugin: 'eclipse'
eclipse {
    classpath {
        downloadJavadoc = true
        downloadSources = true
    }
}

【讨论】:

  • Gradle 的依赖配置如上图所示。文档明确提到了对分类器的支持。也许我误解了如何使用分类器。无论哪种方式,Eclipse 插件都无法解决这个问题。
  • @KyleM 我不认为sources 是依赖项的配置。从页面上您只能拥有compile, runtime, testCompile, testRuntime, compileOnly (gradle 2.13) 我看到您现在所说的与配置有关的内容。但我很确定您使用它的方式不正确。
  • 这是不正确的。您可以随意命名依赖配置。你说的配置是java插件定义的标准配置。如果您使用“编译”和“运行时”,我发布的示例具有相同的输出。 docs.gradle.org/current/userguide/…。不过谢谢你,我很感激你的回复。
【解决方案2】:

首先我们要提醒它

我们需要先在idea's module sectioneclipse classpath section 中添加两个东西

idea {
    module {
            //if you love browsing Javadoc
            downloadJavadoc = true

            //if you love reading sources :)
            downloadSources = true
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
    classpath {
       downloadSources=true
       downloadJavadoc = true
    }
}

资源链接:

  1. https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
  2. Dependency Management

其次

我们需要从存储库部分删除mavenLocal() 部分,存储库将放置在最顶部的部分。你已经做到了。

repositories {
    mavenLocal() // remove this
    mavenCentral()
}

第三,

有时您在 Eclipse WTP 中看不到源代码,尽管它们是由 gradle 下载的。在这种情况下,您需要手动将 Web App Libraries 推送到构建路径的底部。 为了解决,您需要遵循

  1. 右键单击您的项目,然后选择“构建路径”-->“配置 构建路径”;
  2. 选择“订购和导出”
  3. 选择“Web App Libraries”,然后单击“底部”按钮,然后单击“Web 应用程序库”将位于底部;

并将其放入 Gradle Eclipse 插件(因此您无需每次都手动执行): Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?

归功于@jasop

更新:

我想给你一个更新,现在我可以下载所有 javadocs 和源 jar 文件。但我无法将它们复制到源文件夹中。下面给出了我成功尝试下载 javadocs 和源 jar 的尝试:

build.gradle文件如下:

group 'com.waze'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    sources {
        description = 'sources download'
        transitive = true
    }
    copysource {
        extendsFrom sources
    }
}

eclipse {
    classpath {
       downloadSources = true
       downloadJavadoc = true
    }
}

dependencies {
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.22.2'
    sources group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.22.2', classifier: 'javadoc'
    sources group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.22.2', classifier: 'sources'
}

task copySources(type: Copy) {
    println 'Copying dependencies to sources directory'
    into "sources/"
    from configurations.copysource
}

task run (dependsOn: ['copySources']){
    println 'Downloading JARs'
}

run << {
    println 'Downloads complete. Finished.'
}

命令提示符下的输出:下载javadocs和源jar文件

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\PCPC>F:

F:\>cd F:\eclipse\workspace\log4j_sift-master
F:\eclipse\workspace\log4j_sift-master>gradle cleanEclipse eclipse
Copying dependencies to sources directory
Downloading JARs
:cleanEclipseClasspath
:cleanEclipseJdt
:cleanEclipseProject
:cleanEclipse
:eclipseClasspath
Download https://repo1.maven.org/maven2/org/glassfish/jersey/core/jersey-common/
2.22.2/jersey-common-2.22.2-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/jersey/core/jersey-common/
2.22.2/jersey-common-2.22.2-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/jersey/ext/jersey-entity-f
iltering/2.22.2/jersey-entity-filtering-2.22.2-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/jersey/ext/jersey-entity-f
iltering/2.22.2/jersey-entity-filtering-2.22.2-javadoc.jar
Download https://repo1.maven.org/maven2/org/eclipse/persistence/org.eclipse.pers
istence.moxy/2.6.0/org.eclipse.persistence.moxy-2.6.0-sources.jar
Download https://repo1.maven.org/maven2/javax/ws/rs/javax.ws.rs-api/2.0.1/javax.
ws.rs-api-2.0.1-sources.jar
Download https://repo1.maven.org/maven2/javax/ws/rs/javax.ws.rs-api/2.0.1/javax.
ws.rs-api-2.0.1-javadoc.jar
Download https://repo1.maven.org/maven2/javax/annotation/javax.annotation-api/1.
2/javax.annotation-api-1.2-sources.jar
Download https://repo1.maven.org/maven2/javax/annotation/javax.annotation-api/1.
2/javax.annotation-api-1.2-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/jersey/bundles/repackaged/
jersey-guava/2.22.2/jersey-guava-2.22.2-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-api/2.4.0-b34/hk2-
api-2.4.0-b34-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-api/2.4.0-b34/hk2-
api-2.4.0-b34-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/external/javax.inject/
2.4.0-b34/javax.inject-2.4.0-b34-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/external/javax.inject/
2.4.0-b34/javax.inject-2.4.0-b34-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-locator/2.4.0-b34/
hk2-locator-2.4.0-b34-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-locator/2.4.0-b34/
hk2-locator-2.4.0-b34-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/osgi-resource-locator/
1.0.1/osgi-resource-locator-1.0.1-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/osgi-resource-locator/
1.0.1/osgi-resource-locator-1.0.1-javadoc.jar
Download https://repo1.maven.org/maven2/org/eclipse/persistence/org.eclipse.pers
istence.core/2.6.0/org.eclipse.persistence.core-2.6.0-sources.jar
Download https://repo1.maven.org/maven2/javax/validation/validation-api/1.1.0.Fi
nal/validation-api-1.1.0.Final-sources.jar
Download https://repo1.maven.org/maven2/javax/validation/validation-api/1.1.0.Fi
nal/validation-api-1.1.0.Final-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/javax.json/1.0.4/javax.jso
n-1.0.4-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/javax.json/1.0.4/javax.jso
n-1.0.4-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-utils/2.4.0-b34/hk
2-utils-2.4.0-b34-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/hk2-utils/2.4.0-b34/hk
2-utils-2.4.0-b34-javadoc.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/external/aopalliance-r
epackaged/2.4.0-b34/aopalliance-repackaged-2.4.0-b34-sources.jar
Download https://repo1.maven.org/maven2/org/glassfish/hk2/external/aopalliance-r
epackaged/2.4.0-b34/aopalliance-repackaged-2.4.0-b34-javadoc.jar
Download https://repo1.maven.org/maven2/org/javassist/javassist/3.18.1-GA/javass
ist-3.18.1-GA-sources.jar
Download https://repo1.maven.org/maven2/org/javassist/javassist/3.18.1-GA/javass
ist-3.18.1-GA-javadoc.jar
Download https://repo1.maven.org/maven2/org/eclipse/persistence/org.eclipse.pers
istence.asm/2.6.0/org.eclipse.persistence.asm-2.6.0-sources.jar
Download https://repo1.maven.org/maven2/javax/inject/javax.inject/1/javax.inject
-1-sources.jar
Download https://repo1.maven.org/maven2/javax/inject/javax.inject/1/javax.inject
-1-javadoc.jar
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL

Total time: 7 mins 5.896 secs
F:\eclipse\workspace\log4j_sift-master>

【讨论】:

  • 你运行了我的例子吗?关键是将依赖项复制到“源”文件夹中,而这不是这样做的。我没有尝试将源 JAR 附加到 Eclipse 构建路径 JAR。
  • @KyleM 现在我可以下载所有dependent javadocssources jar 文件。但我不复制它。现在我正在尝试将其复制到sources 文件夹中。如果我得到了,我会告诉你的。如果你有,那么请分享。谢谢。
  • 谢谢。也许如果将编译依赖组复制到源文件夹,这将导致依赖的 JAR 也被复制。我还没有尝试过,但我会在工作时尝试
  • 仅供参考 - 我给了你尝试的赏金,虽然这不能回答问题,因为它不会将任何传递源复制到文件夹。
  • @KyleM 你有答案吗?如果你有答案,请分享。我对此非常感兴趣。非常感谢。
【解决方案3】:

我发现的方法是遍历传递依赖树,并分别下载每个源的源代码。这远非理想,但它确实有效。

请注意,在声明依赖项时不再需要指定分类器。

plugins {
    id 'java'
}

defaultTasks 'run'

repositories {
    mavenCentral()
}

configurations {
    sources {
        description = 'sources download'
        transitive = true
    }
    copysource {
        extendsFrom sources
    }
}

dependencies {
    sources group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.22.2'
}


Dependency toSourceDependency(ResolvedDependency dependency) {
    dependencies.create([
            group     : dependency.module.id.group,
            name      : dependency.module.id.name,
            version   : dependency.module.id.version,
            classifier: 'sources',
            transitive: false
    ])
}

task('run') {
    doLast {
        configurations.copysource
                .resolvedConfiguration
                .lenientConfiguration
                .allModuleDependencies
                .each { dependency ->
                    def sourceResources = configurations
                            .detachedConfiguration([toSourceDependency(dependency)] as Dependency[])
                            .resolvedConfiguration
                            .lenientConfiguration
                            .getFiles(Specs.SATISFIES_ALL)

                    copy {
                        from sourceResources
                        into 'sources/'
                    }
                }
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 2023-03-15
    • 2015-03-20
    • 2019-01-30
    • 2012-09-24
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多