Let's get started with Maven and Bck2Brwsr VM!
The way to create new projects in Maven is to generate them from an archetype. Let's use maven quickstart and execute:
mvn archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 -DgroupId=org.yourorg -DartifactId=demo -Dversion=1.0-SNAPSHOT
A project in the demo
directory is created with its pom.xml
configuration file.
In addition to that it also contains two source files App.java
and AppTest.java
in its directory tree. By default the main
application method just prints
Hello World!
. Let's run it in a browser! Apply the following change to the
pom.xml
file:
diff -r 37410ddd908f pom.xml
--- a/pom.xml Tue May 29 07:37:34 2018 +0200
+++ b/pom.xml Tue May 29 07:39:32 2018 +0200
@@ -28,6 +28,14 @@
</dependencies>
<build>
+ <plugins>
+ <plugin>
+ <groupId>org.apidesign.bck2brwsr</groupId>
+ <artifactId>bck2brwsr-maven-plugin</artifactId>
+ <version>0.51</version>
+ </plugin>
+ </plugins>
+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
and your project is ready to be executed in the browser:
demo$ mvn clean install bck2brwsr:aot bck2brwsr:show
A browser is opened and a Hello World! message gets printed into its console and also console of your terminal window.
While in browser console (press F12 to open the developer tools with the console tab), you can interact with the Java virtual machine by loading public Java class and invoking its public static methods:
vm.loadClass("java.lang.System", function(System) {
System.exit(0)
});
Your Java code is in src/main/java/org/yourorg/App.java
open it in your
editor, change it and run again
demo$ mvn clean install bck2brwsr:aot bck2brwsr:show
Well done!
The next step is to interact with JavaScript. For that purpose Bck2Brwsr VM
uses Apache HTML/Java
@JavaScriptBody
annotation. See the package tutorial
for list of examples.
There are some ready to use libraries built around this annotation:
Charts,
canvas,
maps.
Include their coordinates in the dependency in your pom.xml
and you are
ready to use them all!