A simple and more readable tool generate Java Source code
- #insert. String can be inserted in this
#insert imports_line;- #comment. Comment will be ignroed all together.
#comment this is just comment.
#comment this lines will not be available in our resulted source code- ##VARIABLE##. All variables in the current template file will be replaced, except repeat block. Every variable name starts with ## and ends with ##
#comment example
String fileName = "##FILE_NAME##";- #import. Can be imported multiple time. After improting variables can be updated.
#comment example
#import resource_path- import_once. Can bee imported only once. Variables can be updated after improting.
#comment example
#import_once resource_path- Code block that can be injected more than once. Code block start with #start followed by a name. Block will be end with #end followed by the exact same name. Can be loaded only once.
#comment example
int i;
#start file_process_block
i = ##VALUE_TO_SET##;
fileOpen(##FILE_NAME##, ##VALUE_TO_SET##);
#end file_process_block- template file
// main.template
package ##package_name##;
#import_once imports
public class ##class_name## {
public static void main(String []args) {
List<String> strs = new ArrayList<String>();
#start value_set
strs.add("##string##");
#end value_set
for(String s: strs) {
System.out.print(s);
}
System.out.println("##final_message##");
}
}- imports.template
import java.util.List;
import java.util.ArrayList;- Processing the block
import org.junit.jupiter.api.Test;
public class SimpleExampleTest {
@Test
public void execute() {
TemplateProcessor tp = new TemplateProcessor(this.getClass(), "main");
tp.setValue("package_name", "hello.world");
tp.setValue("class_name", "HelloWorld");
tp.setValue("final_message", "");
tp.addImportBlock("imports");
tp.addRepeatBlock("value_set", "n1").setValue("string", "Hello");
tp.addRepeatBlock("value_set", "n2").setValue("string", " World");
System.out.println(tp.toString());
}
}- Generated Code
package hello.world;
// Importing imports
import java.util.List;
import java.util.ArrayList;
public class HelloWorld {
public static void main(String []args) {
List<String> strs = new ArrayList<String>();
// Repeat block: value_set starts
strs.add("Hello");
strs.add(" World");
// Repeat block: value_set ends
for(String s: strs) {
System.out.print(s);
}
System.out.println("");
}
}- Clone this repository
- execute install.sh
bash install.sh- add the following lines in your pom.xml
<dependency>
<groupId>simple.mind</groupId>
<artifactId>java-template</artifactId>
<version>0.0.1</version>
</dependency>note: Use at your own risk