Gradle正在下载源代码依赖项。

9
在gradle中,我有以下build.gradle文件,它会导致许多JAR文件被复制到“sources”文件夹中,但只有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.'
}

我该如何修改构建过程,以便获取所有源代码(包括传递性/依赖项源代码)?我不想要非源码的jar包。我不明白为什么分类器不能被传递使用,请帮我澄清一下我的误解。

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


我也非常渴望知道这个问题的答案。请接受我对迄今为止完全无用的答案的同情! - Tom Anderson
3个回答

6

首先我们需要提醒一下

IDEA的模块部分Eclipse的classpath部分,我们需要先添加两个东西。

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. 依赖管理

其次

我们需要从存储库部分中移除 mavenLocal() 部分,并将存储库放置在最顶部的部分。你已经完成了这一步。

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

第三步:

有时候,即使使用gradle已经下载了源代码,但在Eclipse WTP中仍然看不到源文件。这种情况下,您需要手动将Web应用程序库推送到构建路径的底部。

解决方法如下:

  1. 右键单击项目,然后选择“Build Path”-->“Configure Build Path”;
  2. 选择“Order and export”;
  3. 选择“Web App Libraries”,点击“Bottom”按钮,然后“Web App Libraries”将会处于底部位置;

为了让Gradle Eclipse插件自动执行上述操作(这样每次都不需要手动进行),请参考以下链接:Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?

感谢@jasop

更新:

我想告诉你们一个最新进展:现在我可以下载所有的javadoc和源代码jar文件了。但是我无法将它们复制到源代码文件夹中。以下是我成功尝试下载javadoc和源代码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>

你运行了我的示例吗?关键是要将依赖项复制到“sources”文件夹中,但这并没有做到。我不想将源JAR附加到Eclipse构建路径JAR中。 - KyleM
@KyleM,现在我可以下载所有的“依赖Javadocs”和“源jar”文件。但我没有复制它。现在我正在尝试将其复制到“源”文件夹中。如果我成功了,我会告诉你的。如果你成功了,请分享一下。谢谢。 - SkyWalker
谢谢。也许如果编译依赖组被复制到源文件夹中,那么依赖的JAR文件也会被复制。我还没有尝试过,但一旦我上班了就会去尝试。 - KyleM
我给你奖励是因为你尝试了,虽然这并没有回答问题,因为它没有将任何可传递的源文件复制到文件夹中。 - KyleM
@KyleM 你有任何答案吗?如果你有答案,请分享一下。我对此非常感兴趣。非常感谢。 - SkyWalker
不好意思,我还不知道答案。我猜想通过学习Eclipse插件,我可以编写出一个答案。如果我能够做到,我会告诉你的。 - KyleM

4

Gradle没有与依赖项的“source”配置相关的任何API。第7章 依赖管理基础

这个答案也独立于集成开发环境。如果您还没有应用特定的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
@KyleM 我没有看到sources作为依赖项的配置。从页面上,你只能有compile, runtime, testCompile, testRuntime, compileOnly (gradle 2.13)。我现在明白你所说的关于配置的问题了。但我相当确定你使用的方式是不正确的。 - Mr00Anderson
那是不正确的。您可以随意命名依赖配置。您所指的配置是由Java插件定义的标准配置。我发布的示例如果使用“compile”和“runtime”将具有相同的输出。https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html#configurations。不过还是谢谢,感谢您的回复。 - KyleM

2
我发现的方法是遍历传递依赖关系的树,并单独下载每个依赖项的源代码。虽然不太理想,但它能够工作。
请注意,在声明依赖关系时,您不再需要指定分类器。
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/'
                    }
                }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接