1: Download IJGenerator
2: Define the model. For this purpose we'll create folder 'models' where we'll place our model files.
2.1: XML approach:
Create a XML file that will contain the description of your model. Let's create a file named model.xml under models directory and place the following XML code inside:
<class>
<name>MyXmlClass</name>
<package>my.xml.package</package>
<dbTable>MY_CLASS</dbTable>
<fields>
<field>
<fieldName>id</fieldName>
<fieldType>int</fieldType>
<primary>true</primary>
<persisted>true</persisted>
<userExposed>true</userExposed>
<columnName>ID</columnName>
<constraints>
<required>true</required>
</constraints>
</field>
<field>
<fieldName>name</fieldName>
<fieldType>String</fieldType>
<persisted>true</persisted>
<userExposed>true</userExposed>
<columnName>NAME</columnName>
<constraints>
<required>true</required>
<minLength>1</minLength>
<maxLength>10</maxLength>
</constraints>
</field>
</fields>
</class>
2.2: Java POJO approach:
Create the following directory structure under models directory: models/my/test. Then create a java file MyClass.java that has the following content:
package my.test;
import ijgen.generator.annotations.Constraints;
import ijgen.generator.annotations.DBTable;
import ijgen.generator.annotations.PackageDesc;
import ijgen.generator.annotations.Persisted;
import ijgen.generator.annotations.Primary;
import ijgen.generator.annotations.UserExposed;
@PackageDesc(name="my.package")
@DBTable(name="MY_CLASS")
public class MyClass {
@Primary
@Persisted
@Constraints(required=true)
@UserExposed
long id;
@Persisted
@UserExposed
@Constraints(required=true, maxLength=30)
String name;
}
Now let's compile the models classes:
javac -cp ijgenerator/ijgenerator.jar models/my/test/*.java
3: Create some templates.
You first have to create folder templates and put some templates there.
In this case you just can copy some of the templates that are in 'samples/templates' in the binary distribution.
For example: domain.vm.jv and dto.vm.jv.
Note: I'll not explain the structure of the templates here because this is beyond the goals ot this 'Getting Started' guide.
For more information you can go to Documentation section.
4: Run the generator:
4.1: Run generator from Command Line:
4.1.1: XML approach:
java -cp ijgenerator/ijgenerator.jar:
ijgenerator/lib/velocity-1.6.2-dep.jar:ijgenerator/lib/xstream-1.3.1.jar ijgen.generator.app.IJGen -mode xml -models models
-templates templates -generator
"class:ijgen.generator.engine.generators.MultipleDefinitionsTemplateGenerator;
filter:jv;extension:java;custom://custom;output:out"
4.1.2: Java POJO approach:
java -cp ijgenerator/ijgenerator.jar:
ijgenerator/lib/velocity-1.6.2-dep.jar:models ijgen.generator.app.IJGen
-mode java -models models -templates templates -generator
"class:ijgen.generator.engine.generators.MultipleDefinitionsTemplateGenerator;
filter:jv;extension:java;custom://custom;output:out"
4.2: Run generator as an ANT task:
You must first define ant task for the generator:
<taskdef name="gen" classname="ijgen.generator.ant.GeneratorTask">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
Now it it time to execute this task in your build:
4.2.1: XML approach:
<target name="generate">
<gen sourceMode="xml">
<path
modelsDirectory="models"
templatesDirectory="templates"
/>
<generatorType
className="ijgen.generator.engine.generators.MultipleDefinitionsTemplateGenerator"
templateFileFilter = "jv"
generatedExtension = "java"
customLine = "//custom"
outputDirectory = "out"
/>
</gen>
</target>
4.2.2: Java POJO approach:
<target name="generate">
<gen sourceMode="java">
<path
modelsDirectory="models"
templatesDirectory="templates"
/>
<generatorType
className="ijgen.generator.engine.generators.MultipleDefinitionsTemplateGenerator"
templateFileFilter = "jv"
generatedExtension = "java"
customLine = "//custom"
outputDirectory = "out"
/>
</gen>
</target>
This is it, now you have running generator and if everything went well you should have generated sources for MyClass object in directory out/my/package.
This is very simple example and is covering very small part of this tool possibilities. You can go to Documentation section for detailed information and more examples.