# Running MyBatis Generator With Ant

MyBatis Generator (MBG) includes a simple Ant task. The task must be defined in your build.xml file, and the task accepts several parameters. Here is an example build.xml file:

       <project default="genfiles" basedir=".">
         <property name="generated.source.dir" value="${basedir}" />

         <target name="genfiles" description="Generate the files">
           <taskdef name="mbgenerator"
                    classname="org.mybatis.generator.ant.GeneratorAntTask"
                    classpath="mybatis-generator-core-x.x.x.jar" />
           <mbgenerator overwrite="true" configfile="generatorConfig.xml" verbose="false" >
             <propertyset>
               <propertyref name="generated.source.dir"/>
             </propertyset>
           </mbgenerator>
         </target>
       </project>

MyBatis Generator task attributes are as follows:

Attribute | Value
--- | ---
configfile (required) | Specifies the name of the configuration file.
overwrite (optional) | If "true", "yes", etc., then MBG will overwrite existing Java files if an existing Java file if found with the same name as a generated file. If "false", "no", etc., and a Java file already exists with the same name as a generated file, then MBG will write the newly generated Java file to the proper directory with a unique name (e.g. MyClass.java.1, MyClass.java.2, etc.). Important: MBG will always merge and overwrite XML files.
contextids (optional) | If specified, then this is a comma delimited list of contexts to use in the current run. Any id specified in the list must exactly match the value of the `id` attribute of an <context> configuration element. Only ids specified in this list will be active for this run. If this argument is not specified, then all contexts will be active.
tables (optional) | If specified, then this is a comma delimited list of tables to use in the current run. Any table specified in the list must exactly match the fully qualified table name specified in a <table> configuration element. Only tables specified in this list will be active for this run. If this argument is not specified, then all tables will be active. Specify table names as: `table` `schema.table` `catalog..table` etc.
verbose (optional) | If "true", "yes", etc., then MBG will log progress messages to the ant console (if Ant is running in verbose mode). The default is "false".

Notes:

- The classpath on the <taskdef> is used to tell Ant where the MBG JAR file is. This is optional if you add MBG to the Ant classpath in one of the other ways described in the Ant manual
- The name of the task can be anything you desire, "mbgenerator" is simply an example
- The task supports an optional nested `<propertyset>` element which is the standard Ant property set type. This can be used to pass parameters into a configuration file. For example, the above property `generated.source.dir` can be accessed in the configuration file with the escape sequence `${generated.source.dir}`
- If a property is specified in the configuration file and is not resolved, then the escaped property string will be passed "as is" into the generated code.