I have a java project which I want to convert into Typescript (about 150 Files).
As per http://www.jsweet.org/getting-started/
...I checked out the jsweet Github project.
...I ran mvn generated-src
...I loaded index.html and it gave me 'It works!'
Now what? It is very unclear what happens next.
Ideally I would assume there is some runnable file eg. converter.java, where you would specify an input and an output directory. I can't see this being explained anywhere.
I think that this question should be "How to transpile a Java project to TypeScript with JSweet".
DISCLAMER: JSweet's main purpose is not to transform existing Java programs to TypeScript. However, since JSweet produces intermediate TypeScript code, it could be used as a helper to migrate Java programs to TypeScript. Of course, this migration will fully work only if the Java libraries are also available in JavaScript, otherwise, you will have to provide an implementation or manually change the used libraries to existing JavaScript ones.
You can indeed start from the the QuickStart project on Github. When you run the mvn generate-sources
, what happens is that the Java sources in src/main/java
are transpiled to TypeScript. The JSweet generation process is configured in the pom.xml
file with the Maven JSweet plugin. Here you can tell JSweet what to generate and where. See the full option list of the plugin 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, you will find the generated TypeScript code in the target/ts
directory.
So, in order to translate a full Java program from there, you need to copy-paste your Java source code within the src/main/java
. Then, run again mvn generate-sources
.
Please note that for this command to succeed, your Java files first need to compile from a Java standpoint. It means that if your Java source files use other Java libraries, these must be available in your classpath. So, like any Java project under Maven, you need to set up a <dependencies>
section in your pom.xml
.
Using external Java libraries can be a problem, because these Java libraries are probably not available in JSweet/TypeScript. So, it likely that you will get the TypeScript files generated in your target/ts
directory, but that the transpilation will report many errors because of use of non-existing APIs in TypeScript.
From there, you can either:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With