How to debug a Maven application in Eclipse - java

Let me start by clarifying I have check every possible resource, tutorial, video, other stackoverflow questions that I could get my hands on that are related in order to try and find an answer to this. I'm using java 8 and Eclipse Luna Service Release 2 (4.4.2) with m2e plugin, It's quite hard to believe but it seems there isn't a single example that clearly explains how you actually debug a Maven Java application USING ECLIPSE. There are no less than 40 different sources here are some
Debugging with exec-maven-plugin
https://blog.jooq.org/how-to-debug-your-maven-build-with-eclipse/
Eclipse not stopping at java breakpoints during remote debug
https://github.com/howlger/Eclipse-IDE-improvements-videos/tree/2022-03/sample_code
IntelliJ IDEA Maven project StackOverflowError when running debug configuration
Debugging in Maven?
What I'm trying to do is click the debug button in eclipse and run a debug configuration that allows me to hit my breakpoints.
I have the following configuration in Eclipse
this runs the exec:exec goal from my pom.xml which looks like this
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>core.app.server</argument>
<argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument>
</arguments>
<workingDirectory>${project.build.outputDirectory}</workingDirectory>
</configuration>
</plugin>
Ok, so far so good. At this point if I run the debug configuration the app launches and then hangs which I'm assuming it is waiting for the debugger to remotely connect based on the arguments in my pom.xml. So the app starts hangs and in Eclipse at this point I'm looking at this
At this point I've tried everything I can imagine. I notice the exec plugin launcher starts on a random port which in the picture is 51661, when in my exec arguments in the pom.xml I have it set to 49875 so this seems off. Another thing I noticed if I removed the <argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument> line from my pom.xml the app launches completely , runs fine but this gets me no where. I have tried to connect using a "Remote Java Application" configuration after the app launches this also does not work. I normally use IntelliJ which makes it an absolute breeze since everything is handled OOTB, unfortunately I have to get this working in Eclipse as well in a similar fashion.
How do I configure the Debug Configuration in a way that will allow me to launch the app and hit my breakpoints?
EDIT 1
When setting suspend=n the app still hangs when launching with the argument agentlib:jdwp
EDIT 2
In an effort to get this figured out without wasting another day on something that should be effortless, I tested running a clean install from and debug configuration and then tested using the run as commands provided by m2e shown below and the m2e commands work perfectly where as the same exact commands ran with a debug configuration fail for missing references.
EDIT 3
Reading the documentation on the exec maven plugin here https://www.mojohaus.org/exec-maven-plugin/index.html it says the difference between exec:exec and exec:java is that the ladder execute programs and Java programs in a separate process and the former execute Java programs in the same VM. I think this might be somewhat related to the issue I'm having. This should be really easy to test for someone familiar with MAven/Eclipse I would think, is anyone able to create a super basic hello world app maven project and see if they can set and hit a break point in the main method, should take but 5-10 min?

I got it working like so:
Download, extract (latest!?) Luna SR2 JavaEE Edition
set (correct) JAVA_HOME/vm in eclipse.ini (oracle-8 latest)
Welcome Screen, create new Maven project, simple, skip achetype...
(So with fresh defaults, simple project):
With the following pom section :
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>C:\Program Files\Java\jdk1.8.0_333\bin\java.exe</executable>
<arguments>
<argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=49875</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.Demo</argument>
</arguments>
<workingDirectory>${project.build.outputDirectory</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
(very similar to yours, only significant difference: address, server is crucial in this approach, suspend either)
With the same (Maven) Run configuration, as you show:
We now additionally need:
"Debug Configurations..."
Remote Java Application(! ..because we chose "server"..)
we select the (sources) project as "Project"
Connection Type: attach
Host: localhost
Port: the one configured in jdwp
Looks like:
Set "my" breakpoint in:
package com.example;
public class Demo {
public static void main(String[] args) {
int x = 0;
x++; // just to set breakpoint somewhere
System.out.println(x);
}
}
"Run" "Exec_Java" (suspend, and server flags (and all) have effect!)
Console(Exec_Java) should report:
...
[INFO] --- exec-maven-plugin:3.0.0:exec (default-cli) # luna-res ---
Listening for transport dt_socket at address: 49875 //!!
"Debug" "New_Configuration".
..and the breakpoint halts(, and we get the "switch perspective dialog";)

Related

How can I add setup goal to clover in Jenkins?

I have a maven project, and I can run coverage on it on my own machine, no problem after following instructions here: https://confluence.atlassian.com/clover/clover-for-maven-2-and-3-quick-start-guide-160399608.html
When I use Jenkins to run clean clover:setup test clover:aggregate clover:clover, it complains that it Could not find goal 'setup' in plugin org.apache.maven.plugins:maven-clover-plugin:2.4 among available goals aggregate, check, instrumentInternal, instrument, log, clover, save-history
Obviously there is no settings file on the Jenkins box. I'm not sure how it even knows those available goals offhand. And I'm not sure how to add that last goal.
I tried this to no avail:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-jira-plugin</artifactId>
<version>5.0.18</version>
</plugin>
</plugins>
</pluginManagement>
The second plugin is what I added, in the hopes to mirror what the settings.xml file was doing on my local machine.
I figure another option is to edit the settings file on Jenkins itself. That is probably the best one...
My Jenkins is in Docker so I just ran docker exec -it abc /bin/bash and got in. When I go to .m2 I don't see a settings.xml. So not sure what Jenkins is saying it's going to reference. I'm going to try out just putting settings.xml here and see how that goes
I made sure that it was using my global settings file, and made that file match what was on my machine. Didn't need to touch my pom.xml at all.

How can I run my Selenium tests with Maven? [duplicate]

This question already has answers here:
How do I run a selenium test using maven from the command line?
(4 answers)
Closed 7 years ago.
I'm trying to figure out how to run Selenium WebDriver tests without having to use Eclipse or IntelliJ or any other IDE. I do all my java development using a plain text editor, and don't want to have to install (and learn) an IDE just for the sake of compiling and running tests.
I've tried following the Selenium documentation, but it stops short of actually telling you how to run the tests from the command line.
My brief experience with maven amounts to the following:
$ mvn compile
<snip>
No sources to compile
$ mvn test
<snip>
No tests to run
$ mvn run
<snip>
Invalid task 'run'
The only other one I know is mvn jetty:run but that doesn't seem right as I'm not wanting to run a new web server.
I suspect I just need to set the correct targets etc in my pom.xml, but I don't know what they should be, and surprisingly can't find any online.
Can anyone help please?
In short:
mvn integration-test or mvn verify is the thing you're looking for.
Explanation
The goals, you're invoking, are lifecycle phases of maven (see Maven Lifecycle Reference). mvn test is intended for standalone unit tests, mvn integration-test runs after compiling, testing and packaging. That would be also the phase, where you invoke Selenium tests. If you need to start and stop Jetty, Tomcat, JBoss, etc., you would bind start/stop of these to pre-integration-test and post-integration-test.
I usually run my integration-tests using Failsafe and perform there invocations to Selenium and other integrative tests.
OK, I finally figured it out after realising that this is actually a Maven-specific question rather than Eclipse or Selenium.
Maven can be made to run the code it compiles by using the exec-maven-plugin and adding the following to the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Selenium2Example</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
As you can probably gather from the snippet, arguments can be passed in by listing them in the pom.xml. Also, be sure to use the proper package name in the mainClass element.
You can then run mvn compile followed by mvn test to compile and run your code.
Credit has to go to http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ for listing several ways to do this.

Log statements in pom.xml or Tracking flow of Maven

I am new to Maven and I am trying to get strong hold of it. I have gone through the beginner's tutorials and I am able to understand the basic stuff, but I have a complex pom.xml file in my project and my next goal is to understand that.
With the limited knowledge acquired from beginner's guide it is not possible for me to understand that complex pom.xml, so i was thinking of adding some sort of log statements in pom.xml or looking for an alternative via which i could track the flow of execution in pom.xml, since that would help me in knowing how things are moving.
You could printout some log messages to maven console using the maven-antrun-plugin, you can also specify the phase you want to print the message in, see the code below (it goes to the plugins section)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>print-log</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="Your log message for project ${project.artifactId}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
I believe that output generated by debug option in maven can be quite huge and time consuming for reading. I use it only for some specific problem analysis.
For me it allways worked to go through complicated pom.xml files by reading and try to understand intent of the author of the particular section (I think good reference for the pom.xml elements can be found on: http://maven.apache.org/pom.html).
Once you run build afterwards, you can see what and when is it executed and you can find mapping between what you've learned before and how it really works :)
You can start by issuing
mvn -X install
That will give you the flow of execution.
According to mvn --help:
-X,--debug Produce execution debug output
An alternative if you want to see all your dependencies, properties, plugins, directories etc. the try this:
mvn help:effective-pom
I would recommend to read more about Maven build lifecycle. Maven is not a script-like build automation tool (like ant or make). It's a different approach. Logs should be less important than output artifacts produced in ./target directory.

Maven 3.0.4 - execute annotation processors after compile

I would like to run the following basic procedure within my Maven 3.0.4 project. I have all the basics in place and haven't had any issues but am running into problems on step #3. For some reason the basic solution is eluding me, since it seems like something that should be very obvious.
Run a basic clean/install (without annotation processing)
Request that a site build be run
Before the site build kicks off, run annotation processing on the compiled classes using an annotation processor class that was compiled in the initial steps
I tried setting up the annotation processing goal as follows:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>pre-site</phase>
<configuration>
<outputDirectory>${basedir}/target/generated-documentation</outputDirectory>
<processors>
<processor>com.mydomain.MyFancyAnnotationProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
For some reason this doesn't seem to be working.
I feel like I'm doing something very, very silly that is preventing it from working.
I am using the Maven Annotation Plugin instead of the basic, Mojo Apt Plugin. I don't mind switching if someone has a working solution with that one. I tried both without any immediate signs of success. Again, it feels like it's just something obvious that I'm overlooking.
Error received:
[INFO] diagnostic error: Annotation processor 'com.mydomain.MyFancyAnnotationProcessor' not found
[ERROR] error on execute: error during compilation
My guess would be that the plugin is not including the current project itself in its classpath. The best solution would be to separate the annotation processor into its own (sub-)module if possible. If you can't do that, you may be able to just add this project itself as a dependency of the plugin (using a <dependencies> section under the plugin node).
As a diagnostic note, you can run maven with the '-X' argument to see detailed info about the build. This should show you exactly what is on the classpath when the plugin is executed.

maven-assembly-plugin holds lock on file

I'm using the maven-assembly-plugin to produce a zip file for some database scripts. I am having an intermittent problem in that if my build fails for whatever reason, when I come to rebuild it often (but not always) fails immediately when trying to delete the target directory. The reason for this is that the zip assembly file (used by maven-assembly-plugin) that it has copied there is locked.
If I repeatedly try to build, or leave a long enough period between the failed build and a rebuild, the lock is eventually dropped and the build proceeds as normal.
This is proving very frustrating - any ideas what the problem might be? I am using Windows XP Pro.
My pom.xml snippet is :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/sql/zip-assembly.xml</descriptor>
</descriptors>
<finalName>db-deployment</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-database-scripts</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
Thanks!
Using 'process explorer' I can see that there is a java process that still holds onto this file handle even though the maven build has failed. This java process is actually owned by eclipse which is interesting as my build is running from the command line. I suspect that eclipse has seen a file change in the workspace and decided to update its project. After eclipse has finished doing the update, the handle is dropped and my build is able to continue without problem.
Looking further I can see that the assembly file is being built under the main/ directory, which would confirm why eclipse would get involved. I'll move it!

Categories