I think this question should be "How to port a Java project to TypeScript using JSweet .
DISCLAMER . JSweet's main goal is not to convert existing Java programs to TypeScript. However, since JSweet creates TypeScript middleware, it can be used as an assistant to port Java programs to TypeScript. Of course, this migration will only work fully if the Java libraries are also available in JavaScript, otherwise you will have to provide an implementation or manually change the libraries used to existing JavaScript.
You can really start with the QuickStart project on Github. When you run mvn generate-sources , what happens is that the Java sources in src/main/java are passed to TypeScript. The JSweet generation process is configured in the pom.xml file using the Maven JSweet plugin. Here you can tell JSweet what to generate and where. A complete list of plugin options is here . So, according to the pom.xml file, JSweet is configured as:
<plugin> <groupId>org.jsweet</groupId> <artifactId>jsweet-maven-plugin</artifactId> <version>1.1.1</version> <configuration> <verbose>true</verbose> <tsOut>target/ts</tsOut> <outDir>target/js</outDir> <candiesJsOut>webapp</candiesJsOut> <targetVersion>ES3</targetVersion> </configuration> [...]
Because of the tsOut option tsOut you will find the generated TypeScript code in the target/ts directory.
So, in order to translate the full Java program from there, you need to copy-paste the Java source code into src/main/java . Then run mvn generate-sources again.
Please note that for this command to succeed, your Java files must first be compiled from a Java perspective. This means that if your Java source files use other Java libraries, they should be available in your class path. So, like any Java project under Maven, you need to configure the <dependencies> section in pom.xml .
Using external Java libraries can be a problem, as these Java libraries are probably not available in JSweet / TypeScript. This way, you will probably get the TypeScript files generated in your target/ts directory, but that the transpilation will report many errors due to the use of non-existent APIs in TypeScript.
From there you can:
- Take the TypeScript code as it is and finish the conversion manually (this means that you need to reorganize your code to use the valid TypeScript APIs, not Java). This is probably the preferred option if you want to switch to TypeScript and remove the Java source code base.
- Provide the JSweet Java API implementation that you use, similar to the J4TS project. This video demonstrates the conceptual concept of implementing the applet API in JSweet. This may be a good option if you want to continue working in Java with JSweet and use Java tools to safely and gradually move to the Internet.
Renaud Pawlak Aug 31 '16 at 16:50 2016-08-31 16:50
source share