es4x 使用了graalvm 做爲運行時環境,因此即擁有vertx 的強大,又擁有了與java 代碼便捷的通訊能力
如下是一個簡單的測試,同時也簡單說明下es4x 的es4x-launcher.jar 的生成java
使用maven 構建,沒有什麼特殊的node
<?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>com.dalong.nodeapi</groupId>
<artifactId>nodejsapi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/rongfengliang/es4x-userlogin-deploy</url>
</repository>
</distributionManagement>
</project>
package com.dalong;
public class UserLogin {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
public String token(String name,String password) {
return String.format("%s--%s",name,password);
}
}
mvn clean package
使用cli 工具建立的簡單web 項目react
├── README.md
├── app.sh
├── index.js
├── index.ts
├── nodejsapi-1.0-SNAPSHOT.jar
├── package-lock.json
├── package.json
├── tsconfig.json
└── yarn.lock
/// <reference path="node_modules/@types/es4x.d.ts" />
import { Router } from '@vertx/web';
// call jvm build in typee
const BigInteger =Java.type("java.math.BigInteger")
const app = Router.router(vertx);
// call custom type
const UserLogin = Java.type("com.dalong.UserLogin")
app.route('/').handler((ctx) => {
let msg = BigInteger.valueOf(10).pow(100)
// create new instance
let userlogin = new UserLogin();
// call instance method
let token = userlogin.token("dalong","appdemo")
ctx.response().end(`Hello from Vert.x Web! ${msg} \r\n---${token}`);
});
vertx.createHttpServer()
.requestHandler(app.handle)
.listen(8090);
{
"version": "1.0.0",
"description": "This is a ES4X empty project.",
"main": "index.js",
"scripts": {
"test": "es4x test index.test.js",
"postinstall": "es4x install",
"build": "tsc -w",
"start": "es4x -cp=:./"
},
"keywords": [],
"author": "",
"license": "ISC",
"name": "es4x-app",
"devDependencies": {
"@vertx/unit": "^3.8.3",
"typescript": "^3.7.2"
},
"dependencies": {
"@vertx/core": "^3.8.3",
"@vertx/web": "^3.8.3"
}
}
yarn
rm -rf node_modules/.bin/es4x-launcher.jar node_modules/.bin/es4x-launcher
// -v 表明有本身的jar
es4x install -v nodejsapi-1.0-SNAPSHOT.jar
yarn start
yarn start
yarn run v1.19.1
$ es4x
Succeeded in deploying verticle
curl -i http://localhost:8090
es4x-launcher.jar 文件是調用代碼,生成的,默認生成的jar清單文件以下
git
若是須要三方依賴主要就是claa path, 目錄文件.lib 放本身的jar 包 也沒有用,因此打包帶入可使用-v 參數
以下爲帶-v 生成的jar 清單文件
github
artifacts 獲取方法
web
從以上代碼也能夠看出來主要仍是npm包暴露pacakge.json 中定義的mvn 依賴,而後調用maven 的依賴解析,處理依賴的構件
實際上咱們也能夠本身基於此模式,開發符合標準的npm 包,這樣就能夠不用使用自定義參數依賴jar了,這樣能夠無縫的與nodejs
環境集成typescript
https://sourcegraph.com/github.com/reactiverse/es4x/-/blob/pm/src/main/java/io/reactiverse/es4x/commands/Install.java#L174:5
https://github.com/reactiverse/es4xapache