Vert.x:从 Java 部署 JavaScript Verticle 失败
Vert.x: Deploying a JavaScript Verticle from Java fails
我想用 Java 部署一个 JavaScipt 编写的 Verticle。但是我总是得到 ClassNotFoundErrorException
- 虽然路径是正确的并且添加了 vertx-lang-js
资源。
这是我的 JS-Verticle:
declare var vertx;
var eb = new vertx.EventBus()
eb.consumer("cbweb.validation", (message:any) => {
console.log(message);
});
console.log("Validation.js ready")
这是我的 Java- 部署 Verticle 的代码:
private static final String RELATIVE_PATH = "D:/Temp/vertxFtpClient/jslibs/jsftp/verticle.js";
public static void main(String[] args) throws InterruptedException {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new BasicVerticle(), stringAsyncResult -> {
System.out.println("BasicVerticle deployment complete");
});
vertx.deployVerticle(RELATIVE_PATH);
vertx.close();
}
}
这是我的 pom.xml 的样子:
<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>de.vertex.ftp</groupId>
<artifactId>VertexAdapter</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>VertexLearning</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>
也许缺少什么?
如果您只想使用 JavaScript,则根本不需要编写 java 代码。您所需要的只是将您的 js 代码添加到 src/main/resources
(您可以将其更改为其他内容,但您需要调整 maven 以使其不使用其默认值)。
然后像这样使用 maven shade plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</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}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
并使用相对于 src/main/resource
的 js verticle 文件路径声明一个变量 main.verticle
例如:jslibs/jsftp/verticle.js
.
我想用 Java 部署一个 JavaScipt 编写的 Verticle。但是我总是得到 ClassNotFoundErrorException
- 虽然路径是正确的并且添加了 vertx-lang-js
资源。
这是我的 JS-Verticle:
declare var vertx;
var eb = new vertx.EventBus()
eb.consumer("cbweb.validation", (message:any) => {
console.log(message);
});
console.log("Validation.js ready")
这是我的 Java- 部署 Verticle 的代码:
private static final String RELATIVE_PATH = "D:/Temp/vertxFtpClient/jslibs/jsftp/verticle.js";
public static void main(String[] args) throws InterruptedException {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new BasicVerticle(), stringAsyncResult -> {
System.out.println("BasicVerticle deployment complete");
});
vertx.deployVerticle(RELATIVE_PATH);
vertx.close();
}
}
这是我的 pom.xml 的样子:
<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>de.vertex.ftp</groupId>
<artifactId>VertexAdapter</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>VertexLearning</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>
也许缺少什么?
如果您只想使用 JavaScript,则根本不需要编写 java 代码。您所需要的只是将您的 js 代码添加到 src/main/resources
(您可以将其更改为其他内容,但您需要调整 maven 以使其不使用其默认值)。
然后像这样使用 maven shade plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</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}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
并使用相对于 src/main/resource
的 js verticle 文件路径声明一个变量 main.verticle
例如:jslibs/jsftp/verticle.js
.