Templates features

New target languages

2 new predefined target languages have been added : C++ and Scala
To set these languages as the default target languages in a template file :
1
#set( $env.language = 'C++' )
Copied!
1
#set( $env.language = 'Scala' )
Copied!

Files usage

It's now possible to use files located in a bundle folder, in a model folder or anywhere on the filesystem.
Examples :
1
## Get a file :
2
#set( $file = $fn.fileFromBundle("foo.txt") )
3
#set( $file = $fn.fileFromModel("foo.txt") )
4
#set( $file = $fn.file("/foo/bar/foo.txt") )
5
6
## File information :
7
file.name : $file.name
8
file.parent : $file.parent
9
file.path : $file.path
10
file.absolutePath : $file.absolutePath
11
file.canonicalPath : $file.canonicalPath
12
file.exists() : $file.exists()
13
file.isFile() : $file.isFile()
14
file.isDirectory() : $file.isDirectory()
15
file.isHidden() : $file.isHidden()
16
file.length : $file.length
17
18
## Load file content :
19
#set($content = $file.loadContent() )
20
#set($lines = $file.loadLines() )
21
22
#if( $file.exists() && $file.isFile() )
23
$file.loadContent(2)## NO EOL
24
#end
25
26
#foreach ( $line in $file.loadLines(3) )
27
. $line
28
#end
29
30
## CSV file
31
#set( $file = $fn.fileFromBundle("enum-csv.txt") )
32
#set( $lines = $file.loadValues(",", 1) )
33
#foreach ( $line in $lines )
34
$line.get(0) : $line.get(1)
35
#else
Copied!

Java JPA improvements

In addition to the new JPA annotations in the model some JPA customizations have been added to define global default values in order to facilitate JPA entities generation and make it more flexible.
Examples :
Define default "fetch type" ("LAZY" or "EAGER") for all links cardinality ("ManyToMany", "OneToMany", "ManyToOne", "OneToOne" ) :
1
#set( $jpa.manyToManyFetchType = 'EAGER' )
2
#set( $jpa.manyToManyFetchType = 'LAZY' )
3
4
#set( $jpa.manyToOneFetchType = 'LAZY' )
5
6
#set( $jpa.oneToManyFetchType = 'EAGER' )
7
8
#set( $jpa.oneToOneFetchType = 'LAZY' )
Copied!
Define the value for "insertable" and "updatable" attribute in "@JoinColumn" annotation
1
#set( $jpa.joinColumnInsertable = true )
2
#set( $jpa.joinColumnInsertable = false)
3
4
#set( $jpa.joinColumnUpdatable = true )
5
#set( $jpa.joinColumnUpdatable = false)
Copied!
Define if "targetEntity" must be generated in JPA annotation (@ManyToMany, @OneToMany, etc)
1
#set( $jpa.genTargetEntity = true )
Copied!
The default collection type to be used can defined in the current environement object (default is "java.util.List" )
1
#set($env.collectionType = "java.util.Set")
2
#set($env.collectionType = "java.util.Collection")
Copied!

#cancel directive

It's now possible to cancel the current file generation in a template. The cancel directive just stop the generation without writing the output file (with a message explaining why the generation has been canceled).
Example to avoid overwriting an existing file :
1
#if( $target.outputFileExists() )
2
#cancel("File already exists")
3
#end
Copied!
Last modified 2mo ago