简体   繁体   English

Vert.x:从Java部署JavaScript Verticle失败

[英]Vert.x: Deploying a JavaScript Verticle from Java fails

I want to deploy a JavaScipt-written Verticle with Java. 我想用Java部署JavaScipt编写的Verticle。 But I always get a ClassNotFoundErrorException - although the Path is correct and the vertx-lang-js resource is added. 但是我总是会收到ClassNotFoundErrorException尽管路径正确并且添加了vertx-lang-js资源。

This is my JS-Verticle: 这是我的JS-Verticle:

declare var vertx;    
var eb = new vertx.EventBus()
eb.consumer("cbweb.validation", (message:any) => {
    console.log(message);
});
console.log("Validation.js ready")

And here is my Java-Code for deploying the Verticle: 这是我用于部署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();

}

} }

and here is how my pom.xml looks like: 这是我的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>

Maybe something is missing? 也许缺少什么?

If you want to use just JavaScript you don't need to write java code at all. 如果您只想使用JavaScript,则完全不需要编写Java代码。 All you need is to add your js code to src/main/resources (you could change this to something else but you will need to tweak maven not to use its defaults). 您需要做的就是将js代码添加到src/main/resources (您可以将其更改为其他内容,但是需要调整Maven使其不使用默认值)。

And the use the maven shade plugin as this: 并使用maven shade插件,如下所示:

<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>

And declare a variable main.verticle with your js verticle file path relative from src/main/resource eg: jslibs/jsftp/verticle.js . 并使用src/main/resource相对的js main.verticle文件路径声明一个变量main.verticle ,例如: jslibs/jsftp/verticle.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM