gradle:将默认端口从5005更改为 [英] gradle: change default port from 5005
问题描述
我想调试一些同时运行的JVM实例.我知道我可以使用--debug-jvm
运行gradle,以便JVM将等到我启动IDE调试器后才能连接到JVM,但默认情况下使用端口5005.这对于调试一个JVM实例很好.但是,如果我要调试多个实例,则需要定义一个与5005不同的端口.如何使用gradle实现此目的?
I want to debug some JVM instances that are running at the same time. I know that I can run gradle using --debug-jvm
so that the JVM will wait until I start the IDE debugger so that it connects to the JVM but it uses port 5005 by default. That's fine for debugging one instance of JVM... but if I want to debug more than one instance, I'll need to define a different port from 5005. How can I achieve this with gradle?
推荐答案
就我而言,我想调试特定文件,因此在build.gradle
中包含了以下代码:
In my case I wanted to debug a specific file, so I included the following code in build.gradle
:
task execFile(type: JavaExec) {
main = mainClass
classpath = sourceSets.main.runtimeClasspath
if (System.getProperty('debug', 'false') == 'true') {
jvmArgs "-Xdebug", "-agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=y"
}
systemProperties System.getProperties()
}
我可以运行:
gradle execFile -PmainClass=com.MyClass -Dmyprop=somevalue -Ddebug=true
自定义execFile
任务收到:
-
-PmainClass=com.MyClass
:具有要执行的主要方法的类(在脚本中,main = mainClass
) -
-Dmyprop=somevalue
:一个属性,该属性的值可以在调用System.getProperty("myprop")
的应用程序中进行检索(在脚本中,为此需要systemProperties System.getProperties()
) -
-Ddebug=true
:一个用于在端口8787上启用调试的标志(在脚本中,请参见if
条件,以及address=8787
,但可以更改端口,并且也可以更改此标志名称).使用suspend=y
,执行将被挂起,直到调试器连接到端口为止(如果您不希望这种行为,则可以使用suspend=n
)
-PmainClass=com.MyClass
: the class with the main method I want to execute (in the script,main = mainClass
)-Dmyprop=somevalue
: a property whose value be retrieved in the application callingSystem.getProperty("myprop")
(in the script,systemProperties System.getProperties()
was needed for that)-Ddebug=true
: a flag to enable debugging on port 8787 (in the script, see theif
condition, and alsoaddress=8787
, but the port could be changed, and this flag name also could be changed). Usingsuspend=y
the execution is suspended until the debugger is attached to the port (if you don't want this behaviour, you could usesuspend=n
)
对于您的用例,您可以尝试将jvmArgs ...
行后面的逻辑应用于特定任务(或使用tasks.withType(JavaExec) { ... }
应用于所有此类任务).
For your use case, you could try to apply the logic behind the line jvmArgs ...
to your specific task (or use tasks.withType(JavaExec) { ... }
to apply to all tasks of this type).
使用此解决方案时,请勿使用--debug-jvm
选项,因为您可能会收到有关属性jdwp
被定义两次的错误.
Using this solution, don't use the --debug-jvm
option because you may receive an error about the property jdwp
being defined twice.
这篇关于gradle:将默认端口从5005更改为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!