Can't navigate to sources for Maven dependencies easily with IntelliJ IDEA 12 - maven

I added a library to maven by searching from within IntelliJ, and I checked the boxes to download sources and javadoc, which Idea tells me it did.
However, in some of the library files I click into, it just says "/* compiled code */", even though the exact source code for those files is available in another jar which i can see right there in the project.
Does anyone know how to fix this?

If you check the libraries tab in the project settings you'll see that each library can have an entry for its sources jar. If the one you're looking for doesn't have one, add it manually.
This is all a lot easier if you use maven.

Related

How to find a library reference in Intellij Idea

I have a complex project with a lot of Maven libraries. From time to time I am facing the problem of a library version conflict. I can open the class which is conflicting and see all libraries a class is packed in. But unfortunately I don't find an easy way to check where these libraries are referenced in the Maven dependencies. Do you have an idea how to find a library in the Maven tree?
You need Maven Helper plugin
https://plugins.jetbrains.com/plugin/7179-maven-helper
It has a dependencies viewer and lets you resolve conflicts right there. Open your pom file after installing the plugin and you will see another tab at the bottom of the editor window.
Right click on any dependency to bring up the context menu and you will see an option to exclude it. It will add exclude to your pom file. That obviously won't work for uberjars.
Going more low-tech, you can let maven tell you the answer with the command
mvn dependency:tree -Dincludes=groupId:artifactId
You can also invoke this command simply from an Intellij terminal window.
mvn dependency:tree will print the entire tree, the includes addition will let you filter on a specific artifact making it easier to search (although I've been known to just copy the entire tree into a scratch file and just do searches on it there).
If you don't know the groupId or the artifactId is already unique enough, you can filter like this:
mvn dependency:tree -Dincludes=*:artifactId

Can I use maven to add sources as part of my project?

I am using a dependency and I have some issues with it.
I could download the sources and include it as part of my project and then start modifying the source to help me debug my issue.
However, is there an easier way to do this, using maven ? I have the source-jars downloaded but I am not sure if I can then use these source jars and modify the code as well ?
I could in theory unbar the sources and add them to my source build path, but is there an easier way to accomplish this ?
I am using maven and IntelliJ.
Are you able to get a successful build of the dependency you're having issues with (in it's own project?).
If so, change the version, e.g. 1.2.3-CUSTOM-1, make the improvements, rebuild, and use it as a proper maven depdendency with <version>1.2.3-CUSTOM-1</version>.
This might seem a lot of work, but it's not really - you end up with a properly versioned jar.... having a "hacked" version of 1.2.3 jar is asking for all sorts of problems later.
On the plus side, you can share and deploy the -CUSTOM-1 jar if you need to, and you can keep versioning -CUSTOM-2, etc.
This is the "proper way" I would say.
As Thorbjørn Ravn Andersen wrote, source jars are read-only. You can setup debugger breakpoints in them, but you can't "write" in them
Either unjar the sourced jar or if you know it came from a git release tag, clone the original repository and checkout the correct tag
Make sure its sources compile
Bump up the version in case you know you are going to hack the source
On the right pane, use Maven projects / Plus icon to add sources and use the "m" Execute goal icon to build the sources in IDEA :

How to link individual Java classes to jars displayed by dependency:tree?

Is there a way to easily link a specific Java class to a jar that is listed by mvn dependency:tree? I tried -X to and -Dverbose to add verbosity but it didn't do it.
I have an import statement that I suspect is pulled from one jar in one version of my pom and from another when I change some dependency declarations in the pom because the API becomes different. But I know of no easy way to trace the imported class to a jar in the dependency tree.
To locate the maven dependency for a specific import, I usually do a Go to -> Declaration (⌘B on OSX) followed by clicking the Scroll from Source button in the Project tab like so:
I noticed that the first time I did this, the source for junit-3.8.1 was not present in my local maven cache due to which, Intellij IDEA decompiled the Testcase class. The Scroll from Source did not work for this de-compiled source. However after clicking the Download Sources and retrying the Scroll from Source worked fine as above.
This approach has stood me well for checking the maven dependency which is the source of that one off import.

Intellij maven options - What are differences between them

Im a bit confused with maven options in intellij.
What are the differences between and when would you use them?
Reimport
Generate Sources and Update Folders
Download Sources
Download Documentation
Reimport forces a reload of the POM. This should only be necessary if you have any major changes.
Generate Sources generates code. Think WSDL to Java, etc.
Downoad ... downloads the source or documentation packages for dependencies. Generally not required, but it can help if you try to debug your code and you want to jump into (nicely formatted and documented) dependencies or with writing proper JavaDocs.
PS: In rare cases when your project is really messed up and Reimport does not help, use File -> Invalidate Caches / Restart...

Intellij IDEA Breakpoints stop in JARs instead of source code of my project

I set a breakpoint in my .java file. I started jetty via maven-jetty plugin. The java file where I set the breakpoint is also packaged into a JAR.
Intellij stops at the breakpoint, but it shows me the file which is packaged into the JAR instead of the java file. It behaves as if I set a breakpoint in a java file of 3rd party libraries source code.
How can I either make Intellij ignore my JAR or force Intellij to stop at my .java file?
In the current latest version of IntelliJ IDEA (2018.1) there is an option "Show alternative source switcher":
After enabling this option IDEA detects discrepancy of *.java and *.class files and offers to choose source of sources (sorry about tautology). It can ease pain of debugging in projects that consist of many modules.
The setup for this is two fold.
Add the source to one of your modules' classpath. (Project Structure -> Dependencies -> Add -> Module Dependency -> Pick your source and drag it above all library dependencies)
Point your Remote Configuration to search for the sources in that particular module's classpath. See picture below
There is an option to search "whole project" for sources, but this did not set precedence to locally available sources, over downloaded sources, when I attempted to use it. The above solution fixed this problem for me, but it is relatively unsatisfactory when working with a large multiple project setup. i.e I had to pick one particular module's classpath as the source of truth in Remote Configurations. In my opinion, if the local source is present, and "whole project" is the search option, IntelliJ should be smart enough to choose local source over libaries

Resources