Ben
Ben

Reputation: 6741

How to debug a project in Intellij while setting the breakpoints in another project

I have a (Maven-based) project A loaded in Intellij that has a number of dependencies.

I have the source files of one of those dependencies (say B) in another Intellij project.

I would like to put breakpoints in B and debug project A so that A stops when breakpoints are reached in project B.

In Eclipse, I just need to have both projects in the same workspace and it works. Since there are no workspaces in Intellij, I wonder how to do it, and if it is possible.

Upvotes: 9

Views: 11683

Answers (4)

gluttony
gluttony

Reputation: 569

Based on Paulo Merson's answer that did no fully worked for me (see my comment in it for more information), but was almost there, I had to go to:

File -> Project Structure... (Ctrl+Alt+Shift+S)

> Modules

> On the Dependencies tab for the module that has dependencies, find the dependency for which you want to add sources (you can sort by name to find it easily),

> Right click on it and select "Edit..."

> Click the + button above the "Classes", "Sources" and "JavaDocs" lists (or Alt+Insert), then navigate to the src folder of the dependency project.

Now you can add breakpoints and do step by step debugging.

Upvotes: 1

TT--
TT--

Reputation: 3195

The other answers sound like they would work, but all I had to do was go to:

Project Structure (Ctrl+Alt+Shift+S)

> Modules

> on the Sources tab for the module that has dependencies, look on the right side,

click the + Add Content Root, then add the src folder of the dependency project.

I was able to then put breakpoints in those src files and IntelliJ would step to them in debugging.

(Note you may see the warning "Alternative source available for the class ...")


Reference

From https://www.jetbrains.com/help/idea/creating-and-managing-modules.html,

"Modules normally have one content root. You can add more content roots. For example, this might be useful if pieces of your code are stored in different locations on your computer."

Upvotes: 5

Paulo Merson
Paulo Merson

Reputation: 14477

1) Build project B to your localRepository with source files.

2) Open project A on IntelliJ. Project A has a dependency to B, so IntelliJ can see B-version-sources.jar. You just need to open the class on B that you want to debug and set the breakpoints. On the IntelliJ editor you will see a little lock symbol that indicates the class is read-only.

(Depending on your IntelliJ version, you may not even need to have the "-sources" in your local repository in step (1) above, because IntelliJ may be able to decompile the classes to the editor and allow you to set breakpoints.)

Upvotes: 3

Hank D
Hank D

Reputation: 6471

When working with multiple maven projects, I find it convenient to put both under a parent maven project. The two child projects are not aware of the parent and remain independent of each other, but by aggregating them on one parent pom, you can conveniently build and test them at the same time, keeping the dependent in sync with its dependency. When you do that, you can also create Run configurations for each project, launch them in debug mode, and put breakpoints in either or both of them.

The parent pom stays in the parent folder of both projects, and does not need to go into source control because the child poms don't refer to it as their parent--its only for your convenience in working on both at the same time. Such a pom might look like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>anything</groupId>
    <artifactId>anything</artifactId>
    <version>0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>All projects</name>
    <modules>
        <module>project-1-subdirectory</module>
        <module>project-2-subdirectory</module>
    </modules>
</project>

Upvotes: 3

Related Questions