Home

Awesome

JSweet maven plugin

Unleash the power of JSweet into your maven project

Table of Contents

  1. Basic Configuration
  2. Run JSweet
  3. Hot transpilation
  4. Advanced configuration

Basic Configuration

Add the JSweet's plugin repositories to your project's pom.xml:

<pluginRepositories>
	<pluginRepository>
		<id>jsweet-plugins-release</id>
		<name>plugins-release</name>
		<url>http://repository.jsweet.org/artifactory/plugins-release-local</url>
	</pluginRepository>
	<pluginRepository>
		<snapshots />
		<id>jsweet-plugins-snapshots</id>
		<name>plugins-snapshot</name>
		<url>http://repository.jsweet.org/artifactory/plugins-snapshot-local</url>
	</pluginRepository>
</pluginRepositories>

Configure your pom's sourceDirectory, as usual:

<build>
	<sourceDirectory>src</sourceDirectory>

Add your JSweet dependencies (candies):

<dependencies>
	<dependency>
		<groupId>org.jsweet.candies</groupId>
		<artifactId>angular</artifactId>
		<version>1.4.1-SNAPSHOT</version>
	</dependency>

Enable the JSweet transpiler plugin for the preferred phase (here, generate-sources):

<plugin>
	<groupId>org.jsweet</groupId>
	<artifactId>jsweet-maven-plugin</artifactId>
	<version>3.0.0</version>
	<configuration>
		<outDir>javascript</outDir>
		<targetVersion>ES6</targetVersion>
	</configuration>
	<executions>
		<execution>
			<id>generate-js</id>
			<phase>generate-sources</phase>
			<goals>
				<goal>jsweet</goal>
			</goals>
		</execution>
	</executions>
</plugin>

The configuration options of the plugin:

NameTypeValuesDefault
targetVersionenumES3, ES5, ES6ES3
moduleenumAny of the ModuleKind enum's values (e.g. none, commonjs, umd, es2015).none
moduleResolutionenumThe module resolution strategy (classic, node).classic
outDirstringJS files output directorytarget/js
tsOutstringSpecify where to place generated TypeScript files.target/ts
tsOnlybooleanDo not compile the TypeScript output (let an external TypeScript compiler do so).false
tsserverbooleanFaster ts2js transpilation using tsserverfalse
includesstring[]Java source files to be included-
excludesstring[]Source files to be excluded-
bundlebooleanBundle up all the generated code in a single file, which can be used in the browser. The bundle files are called 'bundle.ts', 'bundle.d.ts', or 'bundle.js' depending on the kind of generated code. NOTE: bundles are not compatible with any module kind other than 'none'.false
sourceMapbooleanGenerate source map files for the Java files, so that it is possible to debug Java files directly with a debugger that supports source maps (most JavaScript debuggers).true
sourceRootstringSpecify the location where debugger should locate Java files instead of source locations. Use this flag if the sources will be located at run-time in a different location than that at design-time. The location specified will be embedded in the sourceMap to direct the debugger where the source files will be located.-
compileSourceRootsOverridestringSpecify other source location/s instead of default source locations. Use this flag if the sources will be generated/preprocessed at build-time to a non standard location.default compile sources
encodingstringForce the Java compiler to use a specific encoding (UTF-8, UTF-16, ...).UTF-8
noRootDirectoriesbooleanSkip the root directories (i.e. packages annotated with @jsweet.lang.Root) so that the generated file hierarchy starts at the root directories rather than including the entire directory structure.false
enableAssertionsbooleanJava 'assert' statements are transpiled as runtime JavaScript checks.false
verbosebooleanTurn on general information logging (INFO LEVEL).false
veryVerbosebooleanTurn on all levels of logging.false
jdkHomestringSet the JDK home directory to be used to find the Java compiler. If not set, the transpiler will try to use the JAVA_HOME environment variable. Note that the expected JDK version is greater or equals to version 8.${java.home}
declarationbooleanGenerate the d.ts files along with the js files, so that other programs can use them to compile.false
dtsOutstringSpecify where to place generated d.ts files when the declaration option is set (by default, d.ts files are generated in the JavaScript output directory - next to the corresponding js files).outDir
candiesJsOutstringSpecify where to place extracted JavaScript files from candies.-
ingoreDefinitionsbooleanIgnore definitions from def.* packages, so that they are not generated in d.ts definition files. If this option is not set, the transpiler generates d.ts definition files in the directory given by the tsout option.false
factoryClassNamestringUse the given factory to tune the default transpiler behavior.-
headerfileA file that contains a header to be written at the beginning of each generated file. If left unspecified, JSweet will generate a default header.-
workingDirdirectoryThe directory JSweet uses to store temporary files such as extracted candies. JSweet uses '.jsweet' if left unspecified.-
disableSinglePrecisionFloatsbooleanBy default, for a target version >=ES5, JSweet will force Java floats to be mapped to JavaScript numbers that will be constrained with ES5 Math.fround function. If this option is true, then the calls to Math.fround are erased and the generated program will use the JavaScript default precision (double precision).false
extraSystemPathstringAllow an extra path to be added to the system path.-
ignoredProblemsstring[]Array of JSweetProblems which won't be reported or cause failure.-

Run JSweet

Then, just run the maven command line as usual:

$ mvn generate-sources -P client

Hot transpilation

JSweet maven plugin is now able to watch changes in your JSweet files and transpile them on the fly. Try it with

mvn jsweet:watch

Advanced configuration

You can use the plugin with profiles in order to transpile differently several parts of your application. For instance, a node server and a HTML5 client app:

<profiles>
		<profile>
			<id>client</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.jsweet</groupId>
						<artifactId>jsweet-maven-plugin</artifactId>
						<version>3.0.0</version>
						<configuration>
							<outFile>client/out.js</outFile>
							<targetVersion>ES6</targetVersion>
							<includes>
								<include>**/*.java</include>
							</includes>
							<excludes>
								<exclude>**/server/**</exclude>
							</excludes>
						</configuration>
						<executions>
							<execution>
								<id>generate-js</id>
								<phase>generate-sources</phase>
								<goals>
									<goal>jsweet</goal>
								</goals>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
		<profile>
			<id>server</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.jsweet</groupId>
						<artifactId>jsweet-maven-plugin</artifactId>
						<version>3.0.0</version>
						<configuration>
							<outFile>server/full.js</outFile>
							<module>commonjs</module>
							<targetVersion>ES5</targetVersion>
							<includes>
								<include>**/*.java</include>
							</includes>
							<excludes>
								<exclude>**/app/**</exclude>
							</excludes>
						</configuration>
						<executions>
							<execution>
								<id>generate-js</id>
								<phase>generate-sources</phase>
								<goals>
									<goal>jsweet</goal>
								</goals>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
</profiles>

then run the desired profile:

$ mvn generate-sources -P client

Use with code generators / preprocessors

When using some code generators/preprocessors (eg: Project Lombok) the code is not ready for JSweet fully yet (eg: there are custom annotations to handle beforehand).

To let JSweet use a non default source directory, the compileSourceRootsOverride config element can be used, for instance with Lombok:

<build>                          
    <plugins>
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.18.12.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                    <configuration>
                        <addOutputDirectory>false</addOutputDirectory>
                        <sourceDirectory>src/main/java</sourceDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.jsweet</groupId>
            <artifactId>jsweet-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <compileSourceRootsOverride>
                    <compileSourceRoot>target/generated-sources/delombok</compileSourceRoot>
                </compileSourceRootsOverride>
                <sourceRoot>target/generated-sources/delombok</sourceRoot>
                <outDir>javascript</outDir>
                <targetVersion>ES5</targetVersion>
            </configuration>
            <executions>
                <execution>
                    <id>generate-js</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>jsweet</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>