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

前端 未结 3 828
情深已故
情深已故 2021-01-05 19:49

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 Intell

相关标签:
3条回答
  • 2021-01-05 20:24

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

    • If B is a maven project, you can use the source plugin. Then mvn install should generate file B-version-sources.jar.
    • If B is a gradle project, you can define a task to generate the sources.

    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.)

    0 讨论(0)
  • 2021-01-05 20:36

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

    Project Structure (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."

    0 讨论(0)
  • 2021-01-05 20:50

    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>
    
    0 讨论(0)
提交回复
热议问题