我想用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")
这是我用于部署Verticle的Java代码:
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>
也许缺少某些东西?
答案 0 :(得分:1)
如果您只想使用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>
并使用相对于main.verticle
的js垂直文件路径声明变量src/main/resource
,例如:jslibs/jsftp/verticle.js
。