ant-javacard

repository·next·Indexed 42 days ago

https://github.com/martinpaljak/ant-javacard

An Apache Ant task for building JavaCard CAP files in a declarative way. It simplifies compiling and converting JavaCard source code into deployable CAP files across various SDK versions, providing tags for CAP and applet definition, support for 32-bit int types, and a CLI utility for metadata inspection and off-card verification.

Tokens
1.5K
Snippets
3
Records
7
Agent score
5%

What's inside ant-javacard

  1. Install and use ant-javacard

    next

    To use ant-javacard in your Apache Ant project, follow these steps:

    1. Download the JAR: Download ant-javacard.jar from the latest releases or use the Ant <get> task:
    <get src="https://github.com/martinpaljak/ant-javacard/releases/latest/download/ant-javacard.jar" dest="." skipexisting="true"/>
    1. Define the task: In your build.xml, load the task using <taskdef>:
    <taskdef name="javacard" classname="pro.javacard.ant.JavaCard" classpath="ant-javacard.jar"/>
    1. Build an applet: Use the <javacard> and <cap> tags to define your build process.
    <taskdef name="javacard" classname="pro.javacard.ant.JavaCard" classpath="ant-javacard.jar"/>
  2. Recommended JDK and SDK setup for JavaCard

    next

    To ensure compatibility, match your JDK and JavaCard SDK versions according to these guidelines:

    Target PlatformRecommended JDKRecommended SDK
    Modern (JC 3.0.4+)JDK 25 (set $JAVA_HOME)Latest SDK (e.g., jc320v26.0_kit) with targetsdk="3.x.y"
    Older (JC 2.2.x / 3.0.1)JDK 17 (set $JAVA_HOME)JavaCard SDK 3.0.5u4 (jc305u4_kit) with correct targetsdk
    Legacy (JC 2.1.x)JDK 8 (set $JAVA_HOME)Matching legacy SDK (e.g., jc212_kit). Note: off-card verification is disabled automatically.
  3. Add ant-javacard as a Maven dependency

    next

    To use ant-javacard in a Maven project, add the following repository to your pom.xml. Note that Maven Central updates are manual and limited to selected versions.

    <repositories>
        <repository>
            <id>javacard-pro</id>
            <url>https://mvn.javacard.pro/public/</url>
        </repository>
    </repositories>
    <repositories>
        <repository>
            <id>javacard-pro</id>
            <url>https://mvn.javacard.pro/public/</url>
        </repository>
    </repositories>
  4. Configure the javacard task and cap tag

    next

    The ant-javacard task uses a hierarchical XML structure to define how CAP files are generated.

    <javacard> tag

    This is the generic task wrapper.

    • jckit: Path to the JavaCard SDK used if individual <cap> elements do not specify one. This has high precedence (overrides JC_HOME).

    <cap> tag

    This construct defines a specific CAP file to be built.

    • jckit: Path to the JavaCard SDK to be used. Required if not defined in <javacard>.
    • targetsdk: Path to the target JavaCard SDK, or a version name (e.g., "3.0.4", "3.1.0", "3.2.0", or "preview"). This allows using a newer converter to target older platforms.
    • sources: Path(s) to Java source code. Required if classes is not provided (unless using default paths like src/main/javacard).
    • classes: Path to pre-compiled class files. If both sources and classes are provided, compiled classes are placed in the classes folder.
    • package: Name of the package. Required for libraries; optional for applets (defaults to parent package).
    • aid: Hex AID of the package. Recommended.
    • output: Path where to save the generated CAP file.
    • export: Path (folder) to place the generated JAR and EXP files.
    • verify: If false, disables off-card verification.
    • debug: If true, generates debug CAP components.
    • strip: If true, removes class files from the target CAP.
    • ints: If true, enables support for 32-bit int type.

    <applet> tag

    Used inside <cap> to define an applet within the package.

    • class: The class where the install() method is defined. Required.
    • aid: Hex AID of the applet. Recommended.

    <import> tag

    Used for linking external libraries (e.g., GPSystem).

    • exps: Path to the folder containing .exp files. Required if the imported JAR doesn't include them.
    • jar: Path to the JAR file for compilation.
    <javacard jckit="/path/to/jckit_dir1">
      <cap targetsdk="/path/to/jckit_dir2" aid="0102030405" package="package.name" version="0.1" output="MyApplet.cap" sources="src/myapplet" classes="path/to/classes" export="mylib">
        <applet class="myapplet.MyApplet" aid="0102030405060708"/>
        <import exps="path/to/exps" jar="/path/to/lib.jar"/>
      </cap>
    </javacard>
  5. Use output file name variables in CAP generation

    next

    When specifying the output attribute in the <cap> tag, you can use variables to create structured filenames. The default template is %n_%a_%h_%j_%J.cap.

    Available substitutions:

    • %h: 8-character hex prefix of the SHA-256 Load File Data Block hash.
    • %H: Full SHA-256 Load File Data Block hash (hex).
    • %n: Common name (applet class name if single applet, otherwise package name).
    • %p: Package name.
    • %a: Package AID (hex).
    • %j: JavaCard version required by imports (e.g., 3.0.5).
    • %J: Used JDK version (e.g., jdk11).
    • %v: Applet package version (from the version attribute).
  6. Configure environment variables for ant-javacard

    next

    The following environment variables affect the build behavior:

    • JAVA_HOME: Path to the JDK to be used.
    • JC_HOME: Path to the JavaCard SDK to be used if not specified in the build file.
    • ANT_JAVACARD_TMP: Path to the temporary folder used for building. This folder is not cleaned after use.
    • ANT_JAVACARD_DEBUG: If set, enables debug output.
    • SOURCE_DATE_EPOCH: Used for reproducible builds. Set this to the seconds since the Unix epoch (use with TZ=UTC).
  7. Use the ant-javacard command line utility

    next

    The ant-javacard.jar can be run directly via CLI for metadata inspection and verification.

    Dump CAP file metadata:

    java -jar ant-javacard.jar <capfile>

    Run off-card verifier:

    java -jar ant-javacard.jar <sdk> [<targetsdk>] <capfile> <expfiles>