How to debug grails 3 plugins - debugging

It may be a newbie question but very important thou: how can I debug grails 3 plugins? To make question more clear please check out my previous issue: Grails 3 database-migration-plugin initialization error. There is part saying:
liquibase.exception.ChangeLogParseException
I would very like to try to debug it my self but I don't know how to get to the source of related plugin and put there a break point or simply print a message to the console.
I am using NetBeans IDE, but I can easily switch to any other IDE.

In Jetbrains at least, you can set Java exception breakpoints for the exception you are getting. Also, plugins appear in the list of External Libraries in the Project window. You can expand those libraries and set breakpoints in specific files.
Then, when you run gradle tasks, run them by right clicking the task in the gradle tool window and choose 'debug' instead of 'run'.

Related

How does IntelliJ decide what bytecode (classes) to use when running a maven app?

Questions
My questions are:
(Q) When running an app in the IntelliJ debugger, how does IntelliJ decide which bytecode to use ?
(Q) Does IntelliJ use whatever maven build plugin is being used or simulate it?
(Q) If multiple conflicting versions of code are being used (see mvn dependency:tree -Dverbose), how does IntelliJ decide which version of the code to use?
(Q) Where is the JAR file that IntelliJ creates so I could inspect it to see what versions of classes were used?
(Q) Is the process of selecting classes to use any different when running vs debugging?
Note: This question is being directed to IntelliJ's support staff as well as the SO community.
Background
I have an app that has been very problematic with IntelliJ with regards to picking the desired classes when running the application. 
The app is a Java Executable Jar that has been packaged with the maven-assembly-plugin.  It is not a SpringBoot application.
The problem stems from IntelliJ picking different class files than the maven-assembly-plugin which copies all of these classes (from all of the dependent jar files) into a single jar file. Naturally, only one version of a class can exist in the jar app (I believe the first class copied wins and the others are ignored).
IntelliJ (AFAIK) has no way of knowing what version of what class will be put into the JAR without knowing how the maven-assembly-plugin is implemented; So I'm looking for this answer.
Understanding the Question
To explain, see the example I created to understand how the maven-shade-plugin works. See Is there a sample project in github that shows how to use the maven-shade-plugin?. It's not a perfect fit for my problem/question, but it is in the public domain and it's close enough.
I'll frame my question based on the helloworld example. The helloworld.pom depends directly on loglib:2.0.0 so I would expect IntelliJ to select and use that version when compiling and running the application.  
The problem (code changes not being picked up)
The problem I ran into was when I changed the helloworld/pom.xml file to depend on loglib:2.1.0-SNAPSHOT and IntelliJ did not pick up the code changes and used a previous version of the dependency even though the source code was loaded inside of IntelliJ and the loglib/pom.xml file was changed to be version 2.1.0-SNAPSHOT.
The application is run by specifying the main class of the application com.steranka.play.HelloWorldApp. The Run/Debug configuration is:
NOTE: I've done this exact thing many/many times and it's always worked, so I was puzzled. I also tried to reproduce the problem with a sample example and could not reproduce it. IntelliJ worked flawlessly picking up changes as it has always done in the past.
Trying to fix the problem (bytecode not matching source code)
I went through the list of things I've done in the past but after they were all completed, the problem remained.  The list of steps I followed included:
(1) Rebuilt the project (which included helloworld and loglib packages). Rebuild was done inside of IntelliJ. Nope.
(2) Verify that the correct maven dependency loglib:2.1.0-SNAPSHOT was being used in helloworld module, and that it shows up in the Maven Tools dependency list. 
(3) Clicked Reload all Maven Projects - run from Maven Tool. 
After IntelliJ restart, I made a change to a log.info() statement in the loglib:2.1.0-SNAPSHOT module ran my program and my changes to the log statement did not appear, only the previous version of the log message appeared.
This is the crux of the problem, and reason for my question.  Where is IntelliJ getting the bytecode that it is running? Is it from loglib/target/classes/* directory (this is my belief), or from maven .m2/repository/ ... / loglib:2.0.0-SNAPSHOT.
(3) I put a breakpoint on the log statement (that I changed) and ran the app in Debug mode and IntelliJ stopped on the log statement when I stepped over the statement the wrong log message was printed.  This is when I knew the bytecode IntelliJ was executing did not match the source code.
Detailed steps that reproduced the problem
To be clear I'll give timestamp versions because I don't know exactly what POM versions were being used:
T1: Starting with line that says: log.info("orig message");
T2: I changed the message to log.info("orig message - change1");
T3: IntelliJ did not pick up my change and log/console shows orig message
T4: I added some lines to the source code (multiple log statements)
     log.info("this is strange")
     log.info("why isn't this working")
T5: I put a breakpoint on the line why isn't this working and the breakpoint symbol appeared
T6: However, when I ran the program and IntelliJ stopped at the first breakpoint (orig message - change1) and when it stopped the 2nd breakpoint symbol was greyed out (indicating that the debugger could not stop on that line). 
T7: I stepped and IntelliJ was obviously running different code because the original message appeared instead of the changed message.
More Questions
(Q) When the IntelliJ debugger shows code that doesn't match what is being actually executed, how can I track down what code is actually being used? The answer to my previous questions should answer this.
The observable things I know to look at are:
The Project Settings modules.
The maven tool modules list and the dependencies.
The External Libraries list in the Project Tool.
The target/.jar and target/.classes files.
Settings - java compiler used, maven used,
Common Reasons for this problem (bytecode not matching source code in debugger)
Reason 1: The most common reason for the above problem (in my past) has been making a code change in some module (say loglib when the loglib/pom.xml is at version 2.1.0-SNAPSHOT) and forgetting to update helloworld/pom.xml to refer to the -SNAPSHOT version. In my case I performed this check.
Reason 2: Years ago debuggers did not automatically compile code before the code was run, and this let to the problem whereby the bytecode being used matched the last time the file was compiled. The fix was/is simple, recompile the code and update the *.class file contains your code changes and the debugger code matches the bytecode that is being executed.
How to Reproduce the problem
The maven-shade-example (referred to above) can be used to reproduce the problem where the code shown in the debugger is not used at runtime.
git clone git#github.com:steranka/maven-shade-example.git
cd maven-shade-example
git checkout feature/fixing-problem-another-way
mvn clean install
java -jar helloworld\target\helloworld-1.0.0.jar
The output is:
Hello World!
What's up, Sam
Hello, Sam
Goodbye, Sam
But if you try to open this project inside IntelliJ and build and run it, you'll get an error. To run this, I right clicked on the hello word application and selected Run HelloWorldApp.main() (or Debug).
Hello World!
What's up, Sam
Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String com.steranka.play.LogIt.sayHello(java.lang.String)'
at com.steranka.play.GoodFeature.sayGoodbye(GoodFeature.java:6)
at com.steranka.play.HelloWorldApp.main(HelloWorldApp.java:15)
What makes this un-nerving is the debugger jumps to the correct code if you click on the method, it just doesn't use that method at runtime.
The runtime configuration inside IntelliJ is:
#CrazyCoder points out that IntelliJ will run the JAR file created using mvn clean install in the helloworld\target\helloworld-1.0.0.jar if you create a run configuration that says to use Java.
When I created a run configuration to use the Jar file, then IntelliJ's debugger properly executed the application and stepped into the correct files. That configuration looks like this:
When I tried this solution on the actual code causing problems, it also worked. Strangely enough (to me) after switching to the JAR configuration and successfully running, I switched back to my Main class configuration and it also worked! So toggling between the two configurations might work for others as it did for me.
#CrazyCoder also points out that there is an outstanding bug/feature request with IntelliJ that asks for better support of the maven-shade-plugin. See https://youtrack.jetbrains.com/issue/IDEA-266746
#Thorbjørn Ravn Andersen states that IntelliJ emulates Maven so in general the behavior between building and running inside IntelliJ should match what happens with Maven.

Unable to find Groovy method when building Rhino with Intellij IDEA

While trying to build https://github.com/mozilla/rhino, I'm getting Unable to find method on org.codehaus.groovy.runtime.StringGroovyMethods.
so the build fails, and I'm unable to test Rhino built from source code, which is the goal.
Steps Taken
From the opening screen in IntelliJ, I check out the project:
I get this error Unable to find method ''java.lang.String org.codehaus.groovy.runtime.StringGroovyMethods.capitalize(java.lang.String)'' right away, which I have not been able to fix:
There are two suggestions in the above Re-download dependencies and sync project and Stop Gradle build processes. I have tried both with no improvement.
When I look at the build.gradle file, I see another suggestion ...configure Gradle wrapper to use distribution with sources.... This also does not get past the missing Groovy method problem.
I have looked on S.O. for similar issues and tried various things under File > Settings > Build, Execution, Deployment, but I realized I'm over my head since, conceptually, I'm not sure what I need and where that would go in the settings.
The command line items from the readme work as expected, but going back to File > Build still fails (added after tim_yates comment).
What changes are required to the IDE or to the build definitions to allow Mozilla Rhino to build properly?
I installed the latest (2022.1) version of IntelliJ IDEA Community Edition. During the install, it asked if you wanted this IDE to be associated with Groovy file types, and I answered in the affirmative.
On this new install, the Unable to find method on org.codehaus.groovy.runtime.StringGroovyMethodserror did not appear, so the problem was solved by upgrading the IDE (and also that required updating GIT).

Netbeans 11 Gradle Project does not run gradle on save

I have just installed Netbeans 11.1 and when I save one of my Java files, Netbeans does not start a gradle build automatically. This used to work in Netbeans 8.2.
I have installed nb-javac and have also tried the newest Beta version to no avail.
Is this a known bug or do I need to reconfigure something when going from NB 8.2 to 11.1?
(This is only an explanation rather than a solution to your problem.)
First, Compile on Save is an option which is set or unset at the individual project level, rather than at the global level. So for a NetBeans Gradle project, select Properties > Build > Compile to view the setting for the Compile on Save checkbox. For that checkbox, note that:
It is unchecked by default, so there will not be an automatic Gradle build when you save a project file.
It is disabled, so you cannot trigger a build whenever you save a project file.
The problem persists in the latest beta of NetBeans 11.2.
I don't see a bug report for this issue, so perhaps you can raise one? Click the Log In button to sign up first if necessary.
That said, there is a related issue which may explain why the check box cannot be enabled. See closed bug NETBEANS-680 Erroneous Gradle Compile-on-Save activity which relates to Gradle projects using version 9.0 of NetBeans. Apparently there were spurious and unwanted compile-on-save runs being triggered even though Compile on Save was unchecked. Perhaps the feature has been deliberately disabled for Gradle projects because of that issue, though that is just speculation on my part.
Also see the GitHub page for the Gradle plugin. Comments from the NetBeans team for NETBEANS-680 suggest that the problem was with the plugin rather than NetBeans. That said, if NetBeans is offering functionality that cannot be enabled (i.e Compile on Save), it is definitely a NetBeans issue regardless of the underlying cause.
Finally, note that you can configure the Gradle plugin using Tools > Options > Java > Gradle, but I dodn't see any options there that would help with this issue.

SpringBoot Kotlin project wont run and debug from Intellij

We have inherited a SpringBoot project written in Kotlin. Using Intellij, I would expect to be able to go to the Application main method and click the green arrow to start the project. I would also expect to be able to run the project in debug mode and set breakpoints, this only works with tests.
When I try and run the project I get a exception FileNotFound which has yielded me no answers. Every file appears to be accounted for and there is clearly nothing missing.
This is a multi-module gradle project with many unique configurations and I suspect one is causing an issue. I am having trouble determining which configuration is causing the issue because the project doesn't even seem to get to the point of standing up Spring (no banner).
I have tried many different combinations of bash scripts, environment variables, and gradle tasks and the project does not seem to run at all. Is there any way I can use Intellij to debug the sequence of configurations and gradle tasks?
So, I am answering my own question because searches lead me nowhere and this was a surprisingly difficult problem to debug. It turns out I was taking the wrong approaches and asking the wrong questions. I took a lot of time to study up on Spring configurations and Gradle tasks to realize none of our stuff was wrong. The Kotlin compiler was failing at the very beginning.
What had happened was one developer naively cd'ed into the Application's module and ran an echo statement that piped gradle output to a file called out with no extensions.
Kotlin would find this file and then proceed to not compile anything starting at the Application main. When we ran the app from terminal the app would recompile itself from the very beginning with no problems. But the automatically generated Intellij config simply ran the app with the bad file every time.
The troubling part was that our .gitignore file was configured to ignore all of the kotlin /out/ directories, but not files like /out so this troublesome file was committed to the repository for quite a while.
Surprisingly, deleting this out file fixed most of the issues our project had with Intellij.
Another note:
Our script was also set to source certain variables from other scripts, which meant we either had to carry them over to the runtime configs. We could also run Intelli from the same terminal we had already sourced the script in (using Tools> Create command line launcher). Once I had that sorted the project ran and debugged perfectly.

Debugging of a JVM application (Java or Scala) with breakpoints in Gradle and IntelliJ 2016.3.5

I have a JVM application that I need to debug using breakpoints with a Gradle task (run and test as dependencies) within IntelliJ 2016.3.5.
There are various sources on how to accomplish debugging with Gradle and IntelliJ:
Debug Gradle plugins with IntelliJ
Using Intellij to set breakpoints in gradle project (most helpful one)
https://youtrack.jetbrains.com/issue/IDEA-119551
https://youtrack.jetbrains.com/issue/IDEA-86465
https://youtrack.jetbrains.com/issue/IDEA-119494
However, these sources are either outdated or meant for another scenario. I do not want to debug the Gradle script but the JVM that runs the actual Java/Scala application. Moreover, the recent versions of IntelliJ use the Gradle Tooling API, which does not offer the option to turn off the daemon. A native support from JetBrains is only provided using the debugging button on the run and test tasks directly, but not if they are defined as dependencies from another task (e.g., check).
According to the sources, this is the way to go then:
run { // or test, doesn't matter
jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
// xor, or both, doesn't seem to make any difference
debug true
}
In any way, Gradle (or the JVM) will then start to listen on port 5005:
Then, I have created a remote configuration with the following parameters:
But when I start the IntelliJ remote debugging task, it fails:
I have also tried using port 5006 and suspend=n without success. Before that, I tried using Gradle arguments in the IntelliJ-Gradle run task. Then, it indeed connected, but seemingly to the Gradle script and not the application JVM because it did not interrupt at the breakpoints. How to fix this?
Debugging of gradle tasks like 'test', 'run', actually all gradle tasks that implements JavaForkOptions interface, should work in IntelliJ since 2014 year
Meanwhile, I found the solution myself. If you have a similar issue, follow the approach mentioned above using the debug option.
test {
debug true
}
But make sure that external connections are accepted in the settings after IntelliJ restarted:
Then, it connects to the correct JVM and interrupts at the breakpoints using the remote task:
If you restart IntelliJ, however, with the very same option (external connections) enabled, then the debugging task might fail due to a blocked port:
So, for some reason, IntelliJ blocks that port after a restart but it is necessary to enable the setting for the debugging task to work. That's odd and I don't think it is intended to behave like that.
Anyways, if you disable the setting and restart, the port will be open again. Then, re-enable the setting, do not restart, just run the Gradle task, and the debugging task. It will work.
I hope this helps anyone else looking for an intermediate solution to debug JVM applications with Gradle and IntelliJ among the confusing and partially outdated answers out there. If anyone has a better or simpler suggestion, feel free to append your answer.

Resources