Debug a maven plugin's execution in a maven web-project - debugging

Is there any way to actually debug a maven plugin while it is in action. What I mean is that for example we have the maven-clean-plugin. So when this plugin executes it's action can we somehow debug and check inside the source code of maven-clean-plugin?
Obviously we would have to associate the Java source for the plugin in eclipse but how can we set it for debugging?
Thanks.
EDIT: Changing the subject
I'm sorry guys maybe I should have been more precise. Actually I have my web-app which is a maven project, which makes use of 3rd party maven plugins. Now when I do a mvn clean install I need to debug my 3rd party maven plugin. Now in my maven dependency I dont get a dependency to that plugin jar, which is quite normal. Any ideas?

If you are using Eclipse with the m2eclipse plugin, simply launch your build with Debug As... instead of Run as....
If you are not using m2eclipse, run mvnDebug instead of mvn (for Maven 2.0.8+) and attach a remote debugger on port 8000. For Maven 2.0.8<, add the remote debuggin options to the start script.
Of course, you need to import the sources of the plugin in your workspace.
See also
Developing and debugging Maven plugins
Dealing with Eclipse-based IDE

If you want to debug Maven execution in eclipse, here is how I did it, with mostly command-line tools (no Eclipse plugin used) (may be off at some points, I haven't done that for 6 months):
Run, from the command line, mvndebug in place of the mvn command. Maven will begin launching and wait for an external debugger to appear on a TCP port before resuming. Note the port number.
Configure in Eclipse a custom, remote debug configuration. (See http://www.ibm.com/developerworks/library/os-ecbug/ , Remote debugging) - set the port number as the one used by mvndebug. Also put the source files that you will be using for debugging in the Debug configuration.
Launch the remote debug configuration. Maven should resume and you will catch bugs in Eclipse.

This might be slightly off, since I'm going to talk about IntelliJ IDEA. With IDEA, you can load a maven project directly, and then simply right click on one of the build lifecycle phases (clean, package, install etc...) and choose debug. The IDE then runs that phase with the correct classpath and drops you into a debugger.
I've used the debugger to debug several of my own plugins. I've never tried debugging a third party plugin, although I imagine this will be relatively painless if the jar still has debug symbols in it and you have corresponding source code.
The community edition of IDEA is available for no fee.

Just replace mvn with mvnDebug in your command:
mvnDebug clean install
As result JVM listens for debugger at port 8000. For finer control you can set MAVEN_OPTS environment variable with standard JVM debug parameters:
export MAVEN_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
mvn clean install

In NetBeans 7.4, Right-click the project > Properties > Actions. Pick the Build project action (or any other), then next to "Set Properties" click Add and Debug Maven Build.

Related

Having Maven Plugins in IntelliJ IDEA without Maven Installation in Computer

I just started to use Maven and IntelliJ IDEA.
I imported a project into IntelliJ IDEA which requires Maven. I didn't install Maven to my computer but I have 2 plugins in IntelliJ IDEA named as "Maven" and "Maven Extension". And the code I have is running without any dependency problem.
In that case, do I still need to install Maven from the web or just the plugins in the IntellJ are enough for projects with Maven?
Can we say that for every project? If someone can explain the logic behind I would be very happy.
Thanks a lot!
Intellij comes with a bundled version of Maven (see File -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven; the property 'Maven home directory' on that screen by defaults points to 'Bundled (Maven 3)').
You don't have to, but you can still install your own version of Maven, and point that property to it. It has the advantage of being able to run maven from the command-line, which is a better guarantee of build-stability (i.e. building the same project in different environments with the same result) than building directly inside of your IDE. And often is way more helpful in investigating build problems.

What does maven clean install -U do?

I have eclipse ide with m2e plugin, maven and weblogc app server running from my local box.
I have imported someone else's multiple maven projects from bitbucket to my box. I was told that one of them is main and rest are dependencies in which I never seen anything like that before. I have always dealt with single maven project. Anyhow from the instruction, it says I have to run maven command such as "clean install -U".
In the IDE so I touched run configuration for each mvn project by setting goal as "clean install -U". By reading maven guide, I kind understand what each term means but when you combine together with a passing parameter, what does it do actually? I didn't expect a jar (web app) to be deployed to an application server but it did also.
-U forces maven to check any external dependencies (third party dependencies) that might need to be updated based on your POM files.
clean install are both basic maven lifecycle phases (https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html).
install normally would simply take the artifact that is built and put it in the local repository, i.e. a directory on the box you are building on (.m2 directory most of the time). It would not do a deployment to a server - typically the deploy phase would be used to do that.
However, developers can override and add to what maven does in the various phases, so just like in the days of ant things can easily devolve into chaos no one can understand on complex projects ;-).
sometimes in the integration-test phase, developers will tell maven to start up a container temporarily to run the web app on, so that tests can be run against it, and then that container is shut down when the integration-test phase completes.

Run dynamic web project with maven in debug mode

I built a dynamic web project with tomcat7 server on maven. To test the project I run mvn tomcat7:run every time anything is changed. This seems to time consuming.
Is there any way to run this in debug mode so I don't have to run project every time I change something in files? mvn --debug doesn't seem to work.
You can use JRebel.
See the answer to this question from stackoverflow for configuring maven with JRebel.
use mvnDebug for debugging/breakpoints.
Otherwise if any class change yup you have to restart (in order to recompile)

How to stop tomcat 7 with maven in eclipse

When I run my maven web project in eclipse. I go to pom.xml, right click on the file and
1. Maven Clean
2. Build Resources
3. Maven Build ..
Then I type tomcat7:run (for tomcat 7) for tomcat 6 , tomcat:run in the tomcat goals field in eclipse . It just starts. But I want to know how
1. To stop it
2. Rebuild the source and redeploy
in elipse or any other quick easy step. Isn't there any way to do all stuff to start the tomcat instead of repeating fore said steps(maven clean.. building resource.. again type tomcat7:run ... ) in eclipse.
Can you try right click on pom.xml > Run As > Maven Build...
Define here in the Goals section all your commands, something like: clean install tomcat7:run
My recommendation is to check skip tests for faster start up time, you can add extra paramenters like -Denvironment=qa if needed or VM arguments (from the JRE tab) like -Xmx2048m -Xms256m
Also, put a name to the configuration since you will be able to Run it again with a simple click or you could copy the .launch file eclipse generates into your application and commit it.
To stop the server just click the red square from the console view.
Hope it helps.
Following worked for me:
mvn tomcat7:shutdown
http://tomcat.apache.org/maven-plugin-2.2/tomcat7-maven-plugin/shutdown-mojo.html
You can use JRebel to hot-deploy your changes, no need to stop/start the app.
In IntelliJ use Ctrl+Shift+F9 to recompile and hot-deploty current changes.
Installing JRebel takes 5 minutes, and running app under JRebel is relally easy:
mvn <your usual parameteres here> -Djrebel.
If you search around, you can get JRebel for free.
Also, nothing stops you from running your maven application in a command line. Then stopping your app is as easy as hitting Ctrl+C.
You can also try maven tomcat shutdown plugin: http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/shutdown-mojo.html.
I use JBoss Tools for managing Containers from Eclipse.
Just close the window for eclipse if the red square from the console view is not available.

IntelliJ steps through the wrong lines when debugging remote Java application

I tried to remote debug a maven plugin for a liquibase project with Intellij. IDEA is highlighting the wrong source code line.
I manually built and installed the plugin in my local maven repository from sources in my Intellij project. Intellij version is 11.1.3 and maven version is 3.0.4 running on Ubuntu 12.04.
For debugging the maven plugin I used mvnDebug comand.
If someone has any ideas please give me some advice. I'm not too used to remote debugging (in fact this is the second time I've done this).
For me, whenever IntelliJ is highlighting the wrong line, it was always because the version of the JAR/classes being used to run the application differs from my source files - i.e. different version of the sources were used to build the JAR and/or classes.
You are going to have to be sure that you are working from the exact source that was used to build the classes you are debugging.
You can verify this by looking at the classpath being used to launch the application, locating the JAR file or classes directory that contains the classes you are debugging, and verifying that they were built from the sources you are inspecting.
Note that when you are debugging third-party libraries, you often can download the "sources" jar (see IntelliJ2-IDEA get Maven-2 to download source and documentation).
If you stumbled across this post, and sure that the source and JAR are the same code, then this could be your problem.
http://youtrack.jetbrains.com/issue/IDEA-8021
Instead of doing remote debugging you can run the plugin directly from IntelliJ.
Have the liquibase plugin project loaded in IntelliJ by just pointing at the pom.xml.
Choose Edit Configurations...
Press the + button to Add New Configuration.
Select Maven.
Enter the Working directory to the project you want to run the plugin on.
Enter the Command line. Could be process-sources, compile or liquibase:status depending on what you want to do.
Press OK
Set a breakpoint in the Liquibase Mojo.
Now you can start this configuration by pressing Shift+F9.
Maven will start and finally you will see that your plugin is waiting at the breakpoint!
Make sure that you have defined the plugin in the target pom.xml with correct version and also that you build the plugin before launching it. You can ensure that by enabling Make in the Before Launch pane.

Resources