5

I'm trying to port Codename One client builds which are very customized ant scripts to work with gradle. When you run a Codename One app on the simulator you aren't running the apps main class but rather something like:

java -classpath CodenameOneJarList com.codename1.impl.javase.Simulator nameOfTheMainClassForTheApp

To do this in gradle I edited the basic build script as such:

apply plugin: 'java'
apply plugin: 'application'
mainClassName = "com.codename1.impl.javase.Simulator"

// for netbeans
ext.mainClass = 'com.codename1.impl.javase.Simulator'

Then at the bottom I did this:

run {
    args 'com.mycompany.myapp.Main'
}

This worked as expected and launched the simulator when pressing run in the IDE (NetBeans). I'm not sure if this is the "right thing" to do though and if it will work in other IDE's.

Then when I tried launching in the debugger the arguments weren't passed since I'm guessing the run target wasn't invoked?

I tried doing this:

debug {
    args 'com.mycompany.myapp.Main'
}

Which obviously failed. I'm not exactly sure where to pass arguments to the debugger?

Is this standardized by gradle?

Am I in the right direction regarding argument passing?

What does the "run" declarative syntax map to? How do I find the other potential declarative block types?

Unfortunately googling for basics like run/debug doesn't result in something useful.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65

1 Answers1

2

The run section in your build.gradle is DSL added to your project from the application plugin you applied. Here is more information about the plugin. As you will have noticed, the application plugin is really geared towards producing a runnable jar, distributing and executing it - not so much for debugging and has no support for that.

To start a java process from Gradle, you can use a JavaExec task, and any arbitrary JVM arguments using the args field, including but not limited to debug options.

You could do something like:

task runApp(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath
  main = 'package.Main' //class to run

  // add debug arguments as required, for example:
  args '-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8888,suspend=n'
}

run gradle runApp to start your application in debug mode and then attach your IDE. Not being familiar enough with Netbeans, I can't comment on how to attach Netbeans to a debug java process.

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • Thanks. So is there a plugin that will allow me to do that? Define a target for debugging and generally all the tasks the IDE needs or do I need to do something specific per-IDE to get this working? If the latter where do I start looking for hacks like this? – Shai Almog Feb 04 '16 at 05:38
  • FYI I obviously have the netbeans gradle plugin installed ;-) – Shai Almog Feb 04 '16 at 05:39
  • How would you debug a specific class in a plain java (non gradle) project in netbeans? – RaGe Feb 04 '16 at 14:22
  • We can use ant so I can invoke the ant support to do that for netbeans only. The problem is that the main reason I'm using gradle is for cross IDE support with a single project type. Maybe my expectations aren't realistic here... – Shai Almog Feb 04 '16 at 14:35
  • My lack of Netbeans familiarity is showing again, but in Eclipse or IntelliJ, you can right click on any (executable) class and select debug. Unless if you're starting an embedded server of some sort, the build system does not usually get involved. That said, you can define **tasks** in gradle that execute classes and call the task directly from Eclipse/IntelliJ (and I would imagine Netbeans as well) – RaGe Feb 04 '16 at 14:41
  • In our case the main class isn't in the project. Its in an external library so that won't work. – Shai Almog Feb 04 '16 at 14:46
  • ah. You can run an external class/lib with debug enabled using gradle, will update my answer. It is then IDE specific, how to attached your IDE to a running process - can't help you there! – RaGe Feb 04 '16 at 14:59
  • But that would mean that run would act as debug always whereas I want the IDE to run when I press the run button and attach a debugger when I press debug. I'm guessing that the answer is that this isn't something gradle covers in a cross IDE way? Right? – Shai Almog Feb 04 '16 at 16:47
  • Try adding two javaExec tasks called run and debug and check if netbeans picks those up for run and debug buttons? This is definitely not cross IDE. – RaGe Feb 04 '16 at 17:46