28

I've set up a Java project with IntelliJ and Gradle. I have a build.gradle file in my root project and I can compile and run my app.

However... I'm using a Java library which comes with a sources and javadoc zip file. If I'm in my source code and want to go to the declaration of a class or method from this library, IntelliJ brings up the .class file instead of the source .java file provided in the zip.

How can I tell gradle to use the sources and javadoc zips provided with the external library?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
kruze
  • 361
  • 1
  • 3
  • 8
  • For dependencies resolved from a Maven/Ivy/flatDir repo, sources will be downloaded and linked automatically (if they exist in the repo). – Peter Niederwieser Jul 16 '14 at 03:48
  • 1
    My jar files are in a folder in my computer. The two zip files with the sources and javadoc are in the same folder. Will gradle resolve this? I have this configuration: `dependencies { compile fileTree(dir: "/my/directory/on/computer/libs", includes: ['*.jar']) }` If I remove the includes paramter, will it use the source.zip file for the library? – kruze Jul 16 '14 at 14:01
  • No, this won't work. It might work with a `flatDir` repository (see [Gradle User Guide](http://gradle.org/docs/current/userguide/userguide_single.html) for details) if sources and javadoc zips follow the Maven naming convention, but I'm not sure. – Peter Niederwieser Jul 16 '14 at 14:27
  • I give it a try when I get home and share the results with you. – kruze Jul 16 '14 at 14:34
  • 1
    Sorry for the late reply. I decided to stay at my current configuration and adding the sources manually from IDEA. Unfortunately I don't have much free time so I can't allow myself to sit over a 'minor' problem like this for days. I need to concentrate on the project's code. I haven't managed to figure out the flatDir repos correct configuration. Thank you for your help. – kruze Aug 12 '14 at 17:53
  • I found a solution to this in Android Studio. It might work in IntelliJ. If you just add the sources.jar to the classpath the IDE seems to just figure it out. For now I just drag/drop the sources.jar into the projectRoot/libs folder where I have a gradle entry compile fileTree(dir: 'libs', include: ['*.jar']) here. If you add a sources.zip you could just add a similar entry which includes all zip files in the folder in your CP. – Cliff Jul 23 '16 at 03:42
  • @elect My answer addresses adding the sources to your Gradle build so they can be considered in the IDE when debugging and such. I'm not sure what your need is when you say "I need it working in Gradle". Are you loking to distribute 3rd party source with your solution in a Gradle build? Are you looking to be able to drill into the source similar to what the OP was asking? – Cliff Dec 12 '18 at 19:51

4 Answers4

11

I'm not sure if your library is stored in a maven repository or not. I assume it is.

I know of two ways of importing a gradle project into IntelliJ. The first being the "Import Project..." wizard from IntelliJ which works nicely. But it does not import the javadoc jars if they exist. At least I never managed it.

The second method uses the idea plugin in gradle 2.1. The plugin generates the project for you. This way I got the javadoc inside. A fully working example for a build.gradle:

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = '1.7'
targetCompatibility = '1.7'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.8.1'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'

    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.8.5'
}

idea{
    project {
        languageLevel = '1.7'
    }
    module {
        downloadJavadoc = true // defaults to false
        downloadSources = true
    }
}

The you can call gradle cleanIdea idea which creates your project for IntelliJ.

If your library is not stored in a maven repository you can simply put up a nexus where you upload it. This would allow you to use the method described above.

Robert Kühne
  • 898
  • 1
  • 12
  • 33
9

It isn't Gradle you need to tell where the sources are. It is IntelliJ.

File -> Project Structure -> Modules -> Select correct module if you have more than one -> Dependencies Tab -> Click on the + sign near the bottom -> Select Jars or Directories -> Select the directory that holds your sources/javadocs jars.

IntelliJ will see that they are javadoc and sources and index them appropriately.

enter image description here

Opal
  • 81,889
  • 28
  • 189
  • 210
Michael
  • 2,683
  • 28
  • 30
  • 22
    The problem with this method is IntelliJ forgets this configuration if I refresh my gradle project or import it again into IntelliJ. I need a way to declare this in the build.gradle file else I always need to add the source jar files by hand. – kruze Jul 16 '14 at 13:57
  • Oh, I see. IntelliJ configures the project based on the build.gradle. So when you edit build.gradle it configures itself based on build.gradle (much like it does for a Grails project). Have you checked the Gradle documentation? Also, if you don't seem to be able to configure Gradle with source jars you can open a feature request with Jetbrains to not override source settings. I would check the dependency management of the Gradle docs and see if you can include source jars (type=source ???) – Michael Jul 16 '14 at 15:12
3

go to any IntelliJ Decompiled .class file of any library. as shown in image attached here. you will notice Intellij is giving you 2 options

  1. Choose source - From Directory
  2. Download - from Maven

enter image description here

Asad Abdin
  • 140
  • 11
1

I use intellij and gradle to build my java projects. This will be very easy by following these simple steps:

  • First at the same level as your src directory add a "directory" with the name of "libs". enter image description here

  • Add your jar files to this directory "libs", (this makes your code portable).

  • Follow the steps:
  • Click Project Structure from File menu.

  • Select Modules that you want to add the new jars at the left panel.

  • Dependencies tab. enter image description here
  • '+' → JARs or directories and add the jar files in the \libs.

enter image description here

  • In the gradle.build file in the dependencies section add:

    dependencies { compile files('libs/"nameOfJarFile".jar') } enter image description here

  • Although not necessary, it is a good practice to invalidate cache and restart at some point. enter image description here

Behrouz
  • 41
  • 3
  • Additional information: if there is a `compileOnly` parameter in the `gradle.build` file's dependencies section, it won't work the import. Before it must to change the `compileOnly` to a simple `compile` parameter. – Sirsemy Mar 22 '19 at 12:35