Skip to content

VboxNick/yaml-codegen-maven

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Basic usage

Plugin configuration in pom.xml
<build>
   <plugins>
       <plugin>
           <groupId>com.github.vboxnick</groupId>
           <artifactId>yaml-codegen-maven</artifactId>
           <version><!-- current version --></version>
           <configuration>
              <models>
                 <model>
                    <file>${project.basedir}/src/resources/schema/company.yml</file>
                    <outputs>
                       <output>
                          <tmpl>${project.basedir}/src/resources/templates/java-pojo.ftl</tmpl>
                          <dst>${project.basedir}/target/generated-sources/java/com/example/Company.java</dst>
                       </output>
                       <output>
                           <tmpl>${project.basedir}/src/resources/templates/ts-pojo.ftl</tmpl>
                           <dst>${project.basedir}/target/generated-sources/ts/company.ts</dst>
                       </output>
                    </outputs>
                 </model>
              </models>
           </configuration>
           <executions>
              <execution>
                 <goals>
                    <goal>generate</goal>
                 </goals>
                <phase>generate-sources</phase>
              </execution>
           </executions>
       </plugin>
   </plugins>
</build>
Model definition: company.yml
fields:
  - name: id
    type: long

  - name: shortName
    type: string
Template: java-pojo.ftl
<#assign pojoClassName = output.file.name?keep_before(".")>
<#assign fields = model.content.fields>

public final class ${pojoClassName} {

<#list fields as f>
    private final ${f.type?cap_first} ${f.name};
</#list>

    public ${pojoClassName}(<#list fields as f>final ${f.type?cap_first} ${f.name}<#sep>, </#sep></#list>) {
    <#list fields as f>
        this.${f.name} = ${f.name};
    </#list>
    }

    <#list fields as f>
    public ${f.type?cap_first} get${f.name?cap_first}() {
        return ${f.name};
    }

    </#list>
}
Output: Company.java
public final class Company {

    private final Long id;
    private final String shortName;

    public Company(final Long id, final String shortName) {
        this.id = id;
        this.shortName = shortName;
    }

    public Long getId() {
        return id;
    }

    public String getShortName() {
        return shortName;
    }

}

Built-in variables

Variable Type

model

Map<String, Object>

model.file

java.io.File

model.content

Map<String, Object>

output

Map<String, Object>

output.file

java.io.File

tmpl.file

java.io.File

tmpl.vars

Map<String, Object>

Packages

No packages published

Contributors 2

  •  
  •