英文:
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<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
<vertx.version>3.8.5</vertx.version>
<junit-jupiter.version>5.4.0</junit-jupiter.version>
<main.verticle>test.test.service.Application</main.verticle>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-api-contract</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config</artifactId>
<version>${vertx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>${main.verticle}</Main-Verticle>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
</transformer>
</transformers>
<artifactSet></artifactSet>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<configuration>
<mainClass>io.vertx.core.Launcher</mainClass>
<arguments>
<argument>run</argument>
<argument>${main.verticle}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
hope it will help.
debugging settings
{
"type": "java",
"name": "Run service",
"request": "launch",
"mainClass": "io.vertx.core.Launcher",
"args": "run test.test.service.Application"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论