How to debug Java code when using ANT script in Eclipse

后端 未结 8 1862
南笙
南笙 2020-12-04 12:43

I have a java class and I need to debug it (put breakpoints and continue using F6). I am using ANT script to init, build, deploy and run the code. I am using:



        
相关标签:
8条回答
  • 2020-12-04 12:45

    (Wasn't able to comment on the given answer, so have to make another answer)

    I realized that when launching Ant from Eclipse, you'll have to add fork="true" to the <java> task. Also, it was first not clear to me how to write nested jvmargs, so here goes an example:

    <java classname="..." fork="true">
      <jvmarg value="-Xdebug" />
      <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
      ...
    </java>
    
    0 讨论(0)
  • 2020-12-04 12:54

    In the <java> ant task you should add two jvm parameters (<jvmarg> IIRC) to turn on debugging:

     -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432
    

    This will launch the java program with debugging turned on and the program will be ready to accept debugger connections on port 5432. Then you should use your IDE's remote debugging facility and direct it to connect to port 5432.

    0 讨论(0)
  • 2020-12-04 12:57

    In Eclipse:

    Toolbar > External Tool Configurations... > (select your existing ANT build or create new) > JRE tab

    In "VM Arguments" add:

    -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y

    Again Toolbar > Debug > Debug Configurations... > Remote Java Application > New

    Name: Debug Ant
    Project: <Select your project where debug files are kept>
    Host: localhost
    Port: 8787
    

    Now in "External Tool Configurations" launch "ANT Task" (which waits for the Remote Java Application debugger to connect), then launch the "Debug Ant" from the "Debug" toolbar icon.

    0 讨论(0)
  • 2020-12-04 12:57

    This is to help the people who are wondering how to debug the web application that use ant to build and deploy. This is quite frequent in legacy applications. If the project was started as "Dynamic Web Project" as the beginning, following steps and even Ant is not necessary.

    Set the break point in your code.

    Window -> Show View -> Others -> Servers
    

    Add your server JBoss or Tomcat for example.

    Right click on the server and choose 'Debug'.

    Make sure that debug="true" is set in ant build file.

    0 讨论(0)
  • 2020-12-04 12:58

    I too faced this problem, I did following steps to resolve.

    1. Put the below lines in ANT file

    2. Go to the debugging configurations->Remote java application-> Create new configuration file with project name,port=5432 and host is localhost and save it.

    3. Now run your build.xml using debugging mode, then you should see in console that "Listening for transport dt_socket at address 5432"

    4. Now run debug configuration file which is you configured. Now your selenium code will run using Debug mode.

    Hope this helps.

    If you still facing issues, please let me know so that i can help you on that.

    Thanks

    0 讨论(0)
  • 2020-12-04 13:00

    This is how I got it working for me (Just commenting for future reference).

    Link dump ahead :


    Debugging ant tasks is not as simple as plain old java debugging. While you can debug an Ant file adding breakpoints, digging inside the code of specific custom task will require you to add a remote debugger in order to be able to "catch" the running process.

    I will explain how to do this in Eclipse, altough I recon it can be achieved with all major java IDEs. First thing is to create a new run configuration for the ant file where you plan to use your customized new task. To do so, go to:

    Run -> External Tools -> External Tools configuration...

    Right click in Ant Build -> New and in the Main tab select your ant script in Buildfile field. Then go to JRE tab and insert the following JVM arguments:

    -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

    If you wonder what these arguments mean check this, although a bit updated (Java 1.5) still works.

    Once this is done, you must create a new Debug configuration for a remote Java application. To do so, navigate to:

    Run-> Debug configurations

    Drop down the list in the left column and right click in Remote Java Application -> New. Select the project name in the Project field. Default values for host and port are okay as long as you used the same ones for the Ant configuration (JVM arguments).

    Everything is ready for the test run! Add breakpoints wherever you consider necessary. In my case, I added one both in the ant script that uses the custom ant task as well as in the custom ant task, in the execute method.

    Right click in your ant script or task -> Debug As...-> Ant >Build first

    Now BEFORE calling your custom ant task code, go to Run-> Debug Configurations and debug your previously created Java Remote Application config. This will start a separate thread that will debug your custom ant task code, provided that you included some breakpoints :) You can see in the following image how in my case, thread stopped in the execute method of my custom ant task. After this point, it is up to you to decide what to do next...

    0 讨论(0)
提交回复
热议问题