9

In gradle I have the following build.gradle which results in a lot of JARs being copied to the 'sources' folder, but only the jersey-media-moxy-2.22.2-sources.jar actually contains source code:

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.'
}

How can I modify the build so that all source code (including transitive/dependency sources) are obtained? I don't want non-source jars. I do not understand why the classifier is not applied transitively so please clear up my misunderstanding.

Also, I understand that this is not the best way to use gradle. This is (part of) a temporary step until we migrate the build system.

KyleM
  • 4,445
  • 9
  • 46
  • 78
  • I am also very keen to know the answer to this question. Please accept my sympathies for the complete uselessness of the answers so far! – Tom Anderson Apr 25 '19 at 09:22

3 Answers3

6

Primarily we have to remind it that

we need to add 2 things first in idea's module section or eclipse classpath section

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

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

or

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

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

Resource Link:

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

Secondly,

we need to remove mavenLocal() portion from repositories section and repositories will be placed in top most section. That you have already done it.

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

Thirdly,

Sometimes you don't see the sources in Eclipse WTP although they're downloaded by gradle. In such case you need to manually push Web App Libraries to the bottom of build path. For solving, you need to follow

  1. Right click your project, then select "Build Path" --> "Configure Build Path";
  2. Select "Order and export"
  3. Select "Web App Libraries", and click "Bottom" button, then the "Web App Libraries" will be on the bottom;

And to get this into the Gradle Eclipse plugin (so you don't need to do it manually every time): Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?

Credit goes to @jasop

UPDATE:

I want to give you update that now I can download all javadocs and sources jar file. But I cannot copy them in sources folder. My successful try attempt to download javadocs and sources jar are given below:

build.gradle File is given below:

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.'
}

Output in command prompt: which downloading javadocs and sources jar file

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>
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • Did you run my example? The point is for dependencies to be copied into the "sources" folder which this doesn't do. I'm not trying to attach source JARs to the eclipse build path JARs. – KyleM May 25 '16 at 14:13
  • @KyleM now I can download all `dependent javadocs` and `sources jar` file. But I do not copy it. Now I am trying it to copy in `sources` folder. If I got, I will let you know. If you got, then please share. Thanks. – SkyWalker May 26 '16 at 18:51
  • thanks. maybe if it's the compile dependency group being copied to the sources folder, that will cause the dependent JARs to also be copied. I haven't tried that yet but I will once I'm at work – KyleM May 26 '16 at 20:17
  • FYI - I gave you the bounty for trying, although this does not answer the question, since it does not copy any transitive sources to the folder. – KyleM Jun 01 '16 at 13:19
  • @KyleM Have you got any answer? If you got answer, please share. I am highly interested about that. Thanks a lot. – SkyWalker Jun 01 '16 at 13:35
  • No, I do not know the answer yet. I assume that by studying the Eclipse plugin, I could code an answer. If/when I am able to do so I'll let you know. – KyleM Jun 01 '16 at 13:41
4

Gradle does not have any API relating to "source" configuration for dependencies. Chapter 7. Dependency Management Basics

The answer is also independent to the Integrated Development Environment. You need to apply the specific IDE plugin if you have not done so already.

IntelliJ

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

Eclipse

apply plugin: 'eclipse'
eclipse {
    classpath {
        downloadJavadoc = true
        downloadSources = true
    }
}
Mr00Anderson
  • 854
  • 8
  • 16
  • Gradle's dependency configuration is shown above in my example. The docs explicitly mention support for classifiers. Maybe I am misunderstanding how to use classifiers. Either way, the Eclipse plugin doesn't help with this issue. – KyleM May 17 '16 at 16:41
  • @KyleM I do not see `sources` as a configuration for dependencies. From the page you can only have `compile, runtime, testCompile, testRuntime, compileOnly (gradle 2.13)` I see what your saying now relating to the configuration. But I am pretty sure the way you are using it is incorrect. – Mr00Anderson May 17 '16 at 16:44
  • That is incorrect. You can name the dependency configuration whatever you want. The configurations you are referring to are the standard configurations defined by the java plugin. The example I posted has the same output if you use "compile" and "runtime". https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html#configurations. Thank you though, I appreciate the response. – KyleM May 17 '16 at 17:02
2

The approach i found is to walk the tree of transitive dependencies, and download the sources for each one separately. It's far from ideal, but it works.

Note that you no longer need to specify a classifier on the dependency when you declare it.

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/'
                    }
                }
    }
}
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133