有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Gradle下载源依赖项

在gradle中,我有以下构建。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的最佳方式。在迁移构建系统之前,这是一个临时步骤(部分)


共 (3) 个答案

  1. # 1 楼答案

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

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

    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/'
                        }
                    }
        }
    }
    
  2. # 2 楼答案

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

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

    IntelliJ

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

    日食

    apply plugin: 'eclipse'
    eclipse {
        classpath {
            downloadJavadoc = true
            downloadSources = true
        }
    }
    
  3. # 3 楼答案

    首先,我们必须提醒它

    我们需要在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()
    }
    

    第三,

    有时您在EclipseWTP中看不到源代码,尽管它们是由gradle下载的。在这种情况下,您需要手动将Web应用程序库推送到构建路径的底部。 为了解决这个问题,你需要遵循

    1. 右键单击项目,然后选择“构建路径”>;“配置 构建路径”
    2. 选择“订单和导出”
    3. 选择“Web应用程序库”,单击“底部”按钮,然后单击“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>