Debug Vert.x app using Gradle and VS Code

huangapple go评论108阅读模式
英文:

Debug Vert.x app using Gradle and VS Code

问题

我正在使用一个使用 Java 编写 Vert.x 应用程序,其中包含一些顶点(verticles)。该应用程序使用一个 MainVerticle 进行启动,并使用 Gradle 进行构建。我使用 VS Code 作为集成开发环境(IDE)/代码编辑器,因为它非常简单且轻量。然而,我发现很难找到一种调试代码的方法。请问如何配置 build.gradle.vscode/launch.json 以启用调试选项?

到目前为止,这是我在 build.gradle 文件中用于启动的命令:

run {
  args = [ 'run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange" ]
}
英文:

I'm working with a Vert.x app with verticles written in Java. The app uses a MainVerticle to start and uses Gradle for its construction. I'm using VS Code as IDE/Code Editor since it is really simple and lightweight. However it has been really difficult to find a way to debug my code. How do I configure build.gradle and .vscode/launch.json to enable debug options?

So far this is my command to start in build.gradle file:

run {
  args = [ 'run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange" ]
}

答案1

得分: 1

我正在为Java开发者使用VS Code,并且我的微服务使用Eclipse Vertx.io。这在Maven中很容易配置。使用Maven创建Vertx项目并修改它以满足自己的使用需求。

我已经对它进行了修改,并获得了类似的项目(不完全相同,不能展示完整的代码):

package com.test.service;

import io.vertx.config.ConfigRetriever;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;

public class Application extends AbstractVerticle {

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    ConfigRetriever retriever = ConfigRetriever.create(vertx);
    retriever.getConfig(
        res -> {
          try {
            if (res.failed()) {
              // ...
            } else {
              startPromise.complete();
              vertx.deployVerticle(
                  new TestServiceVerticle(),
                  new DeploymentOptions().setConfig(res.result()));              
            }
          } catch (RuntimeException e) {
            // ...
            startPromise.fail(e.getMessage());
          }
        });
  }
}

Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <!-- ... 省略其他部分 ... -->

</project>

调试设置:

{
  "type": "java",
  "name": "Run service",
  "request": "launch",
  "mainClass": "io.vertx.core.Launcher",
  "args": "run test.test.service.Application"
}

希望这对您有所帮助。

英文:

I'm woking on VS Code for Java devs and my microservices uses Eclipse Vertx.io. It's is easy to configure for maven. Use maven create vertx project and modify it own usage.

I have modified it and got similar project (not eaxact cannot show full code)

package com.test.service;

import io.vertx.config.ConfigRetriever;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;

public class Application extends AbstractVerticle {

  @Override
  public void start(Promise&lt;Void&gt; startPromise) throws Exception {
    ConfigRetriever retriever = ConfigRetriever.create(vertx);
    retriever.getConfig(
        res -&gt; {
          try {
            if (res.failed()) {
              .....
            } else {
              startPromise.complete();
              vertx.deployVerticle(
                  new TestServiceVerticle(),
                  new DeploymentOptions().setConfig(res.result()));              
            }
          } catch (RuntimeException e) {
            ...
            startPromise.fail(e.getMessage());
          }
        });
  }
}

Pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;test&lt;/groupId&gt;
  &lt;artifactId&gt;test&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;

  &lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;

    &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;

    &lt;maven-compiler-plugin.version&gt;3.8.1&lt;/maven-compiler-plugin.version&gt;
    &lt;maven-shade-plugin.version&gt;2.4.3&lt;/maven-shade-plugin.version&gt;
    &lt;maven-surefire-plugin.version&gt;2.22.1&lt;/maven-surefire-plugin.version&gt;
    &lt;exec-maven-plugin.version&gt;1.5.0&lt;/exec-maven-plugin.version&gt;

    &lt;vertx.version&gt;3.8.5&lt;/vertx.version&gt;
    &lt;junit-jupiter.version&gt;5.4.0&lt;/junit-jupiter.version&gt;

    &lt;main.verticle&gt;test.test.service.Application&lt;/main.verticle&gt;
  &lt;/properties&gt;

  &lt;dependencyManagement&gt;
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;io.vertx&lt;/groupId&gt;
        &lt;artifactId&gt;vertx-stack-depchain&lt;/artifactId&gt;
        &lt;version&gt;${vertx.version}&lt;/version&gt;
        &lt;type&gt;pom&lt;/type&gt;
        &lt;scope&gt;import&lt;/scope&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;io.vertx&lt;/groupId&gt;
      &lt;artifactId&gt;vertx-web-api-contract&lt;/artifactId&gt;
      &lt;version&gt;${vertx.version}&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;io.vertx&lt;/groupId&gt;
      &lt;artifactId&gt;vertx-junit5&lt;/artifactId&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
      &lt;version&gt;${junit-jupiter.version}&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
      &lt;version&gt;${junit-jupiter.version}&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;io.vertx&lt;/groupId&gt;
      &lt;artifactId&gt;vertx-config&lt;/artifactId&gt;
      &lt;version&gt;${vertx.version}&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;${maven-compiler-plugin.version}&lt;/version&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
        &lt;version&gt;${maven-shade-plugin.version}&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;phase&gt;package&lt;/phase&gt;
            &lt;goals&gt;
              &lt;goal&gt;shade&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;transformers&gt;
                &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
                  &lt;manifestEntries&gt;
                    &lt;Main-Class&gt;io.vertx.core.Launcher&lt;/Main-Class&gt;
                    &lt;Main-Verticle&gt;${main.verticle}&lt;/Main-Verticle&gt;
                  &lt;/manifestEntries&gt;
                &lt;/transformer&gt;
                &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.AppendingTransformer&quot;&gt;
                  &lt;resource&gt;META-INF/services/io.vertx.core.spi.VerticleFactory&lt;/resource&gt;
                &lt;/transformer&gt;
              &lt;/transformers&gt;
              &lt;artifactSet&gt;&lt;/artifactSet&gt;
              &lt;outputFile&gt;${project.build.directory}/${project.artifactId}.jar
              &lt;/outputFile&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
        &lt;version&gt;${maven-surefire-plugin.version}&lt;/version&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
        &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;${exec-maven-plugin.version}&lt;/version&gt;
        &lt;configuration&gt;
          &lt;mainClass&gt;io.vertx.core.Launcher&lt;/mainClass&gt;
          &lt;arguments&gt;
            &lt;argument&gt;run&lt;/argument&gt;
            &lt;argument&gt;${main.verticle}&lt;/argument&gt;
          &lt;/arguments&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;


&lt;/project&gt;

hope it will help.

debugging settings

     {
				&quot;type&quot;: &quot;java&quot;,
				&quot;name&quot;: &quot;Run service&quot;,
				&quot;request&quot;: &quot;launch&quot;,
				&quot;mainClass&quot;: &quot;io.vertx.core.Launcher&quot;,
				&quot;args&quot;: &quot;run test.test.service.Application&quot;
      }

huangapple
  • 本文由 发表于 2020年4月7日 07:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定