HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[haxe.org/manual] Getting started with Haxe/Java #146

Open utterances-bot opened 1 month ago

utterances-bot commented 1 month ago

Getting started with Haxe/Java - Haxe - The Cross-platform Toolkit

Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

https://haxe.org/manual/target-java-getting-started.html

migmit commented 1 month ago

If you need external dependencies, it gets mildly complicated.

First of all, you don't want Haxe to attempt compiling the *.java files. It can technically use external dependencies with --java-lib file, but not track dependencies recursively, which means you need a lot of --java-lib arguments, you need to track them manually, and they are not obvious. There are better tools for that. So, let's add -D no-compilation.

Then you use a tool like Maven to compile those *.java files AND external dependencies. Here is how (I'm using Amazon STS as a dependency):

<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>ID.GROUP.OF.CHOICE.YOUR</groupId>
  <artifactId>APPNAME</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.release>11</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <sourceDirectory>BUILD_DIR/src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>haxe.root.Main</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>sts</artifactId>
      <version>2.26.29</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.13</version>
    </dependency>
  </dependencies>
</project>

I added slf4j-simple here, because otherwise SLF4J, which is the de-facto default logging solution for Java would complain about not having a provider.

And then you actually need to make externs for those packages. Yes, yourself. So, the source file might look something like

import java.lang.System;

@:native("software.amazon.awssdk.services.sts.StsClient") extern class StsClient {
    public static function create():StsClient;
    public function getCallerIdentity():GetCallerIdentityResponse;
}

@:native("software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse") extern class GetCallerIdentityResponse {
    public function toString():String;
}

class Main {
    static public function main():Void {
        System.setProperty("slf4j.internal.verbosity", "WARN");
        final stsClient = StsClient.create();
        trace(stsClient.getCallerIdentity().toString());
    }
}

The setProperty line is there to shut up SLF4J happily reporting that it actually found a way to log.

You compile it with Maven like this: mvn clean compile assembly:single. It produces a *.jar file in the target directory, which you can run pretty much the same way.

You can also use --cmd argument to haxe compiler to automatically call Maven; I'd advise to add --color always to the Maven arguments, otherwise you'd lose colored output:

--java BUILD_DIR
--main Main
--dce full
-D no-compilation
--cmd mvn clean compile assembly:single --color always