Codegen

Generate code

License

License

GroupId

GroupId

io.github.h4j4x.codegen
ArtifactId

ArtifactId

lib
Last Version

Last Version

0.0.6
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Codegen
Generate code
Project URL

Project URL

https://github.com/h4j4x/codegen
Source Code Management

Source Code Management

https://github.com/h4j4x/codegen

Download lib

Dependencies

runtime (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.12.2
org.freemarker : freemarker jar 2.3.31

Project Modules

There are no modules declared in this project.

CodeGen

Gradle build Pre release Maven Central javadoc Codacy Badge

Generate code from Freemarker templates and JSON data with CSV support.

How to use it?

From terminal:

Download cli latest version and unzip it. From the root folder of application, you should see a folder structure similar to the following:

├── bin
│   └── cli
│   └── cli.bat
├── lib
│   └── ...

Execute according your operating system.

$ bin/cli \
    --verbose \
    --overwrite \
    --read-recursive \
    -d DATA_FOLDER \
    -t TEMPLATES_FOLDER \
    -o OUTPUT_FOLDER

From gradle script:

Create configuration:

configurations {
    codegen
}

Declare dependencies:

dependencies {
    codegen (
        "io.github.h4j4x.codegen:cli:${latestVersion}",
        "io.github.h4j4x.codegen:lib:${latestVersion}",
    )
}

Create gradle task:

task codegen {
    description = 'Generate code'
    javaexec {
        classpath = configurations.codegen
        main = 'io.github.h4j4x.codegen.cli.CliApp'
        args = [
            '-d', "$projectDir/src/main/resources/data",
            '-t', "$projectDir/src/main/resources/template",
            '-o', "$projectDir/src/main/java",
        ]
    }
}

Configuration

--verbose

Verbose mode (info, warning and error logs).

--overwrite

Overwrite mode (output files will replace existing ones).

--read-recursive

Data folder and sub-folders will be read for JSON files.

-d

Data folder for generation.

How to structure data?

Data must be specified in JSON files (.json) with following structure:

  • With CSV (test.csv inside data folder).
{
  "templates": [{
    "template": "test.ftl",
    "file": "folder/TestClass.java"
  }],
  "csvData": {
    "csvKey": "items",
    "dataKey": "data",
    "fields": ["key", "description"],
    "filePath": "test.csv",
    "data": {
      "generalConfig": "Some config"
    }
  }
}
  • Without CSV.
{
  "templates": [{
    "template": "test.ftl",
    "file": "folder/TestClass.java"
  }],
  "data": {
    "key": "value"
  }
}

-t

Templates folder for generation. Templates must be specified in FTL files (.ftl).

-o

Output folder for generation. Must be writable.

License

License

Versions

Version
0.0.6
0.0.5