|
| 1 | +# Modello Ant Task |
| 2 | + |
| 3 | +The `modello-ant-task` is an Apache Ant task that integrates [Codehaus Modello](https://codehaus-plexus.github.io/modello/) into Ant build workflows. It allows you to generate Java classes, XML serializers/deserializers, XML Schemas (XSD), documentation, and other outputs from Modello model description (`.mdo`) files. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Installation & Definition |
| 8 | + |
| 9 | +To use the task in your Ant build file, define it using either the individual task definition or the preferred standard `antlib` resource. |
| 10 | + |
| 11 | +### Option 1: Using Antlib (Recommended) |
| 12 | + |
| 13 | +```xml |
| 14 | +<typedef resource="org/codehaus/modello/ant/antlib.xml" |
| 15 | + classpathref="modello.classpath" /> |
| 16 | +``` |
| 17 | + |
| 18 | +### Option 2: Using Taskdef |
| 19 | + |
| 20 | +```xml |
| 21 | +<taskdef name="modello" |
| 22 | + classname="org.codehaus.modello.ant.ModelloTask" |
| 23 | + classpathref="modello.classpath" /> |
| 24 | +``` |
| 25 | + |
| 26 | +*Note: `modello.classpath` must contain the `modello-ant-task` JAR, `modello-core`, and any runtime Modello generator plugins you wish to use.* |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Task Reference |
| 31 | + |
| 32 | +The `<modello>` task supports several attributes and nested elements to configure code generation. |
| 33 | + |
| 34 | +### Attributes |
| 35 | + |
| 36 | +| Attribute | Type | Description | Required | Default | |
| 37 | +|:-------------------------------|:----------|:--------------------------------------------------------------------------------------|:--------:|:---------:| |
| 38 | +| **`version`** | `String` | The model version to generate (e.g., `1.0.0`). | **Yes** | — | |
| 39 | +| **`outputDirectory`** | `File` | The directory where the generated files will be written. | **Yes** | — | |
| 40 | +| **`javaSource`** | `String` | Target Java version for generated source code. | No | `8` | |
| 41 | +| **`encoding`** | `String` | Character encoding for generated files. | No | `"utf-8"` | |
| 42 | +| **`packageWithVersion`** | `boolean` | Whether to append the version to the package name (`"true"` / `"false"`). | No | `false` | |
| 43 | +| **`packagedVersions`** | `String` | Comma-separated list of versions to generate backward compatibility for. | No | — | |
| 44 | +| **`domAsXpp3`** | `boolean` | Whether to generate DOM content as Xpp3Dom (`"true"`) or W3C DOM Element (`"false"`). | No | `true` | |
| 45 | +| **`licenseFile`** | `File` | Path to file containing license header text to prepend to generated files. | No | — | |
| 46 | +| **`licenseText`** | `String` | License header text to prepend to generated files. | No | — | |
| 47 | +| **`extendedClassnameSuffix`** | `String` | Suffix for extended class names (e.g., `Ex`). | No | — | |
| 48 | +| **`xsdFileName`** | `String` | Custom output file name for generated XSD schema (`xsd` goal). | No | — | |
| 49 | +| **`enforceMandatoryElements`** | `boolean` | Whether to enforce mandatory elements in XSD (`"true"` / `"false"`). | No | `false` | |
| 50 | +| **`jsonSchemaFileName`** | `String` | Custom output file name for generated JSON Schema (`jsonschema` goal). | No | — | |
| 51 | +| **`firstVersion`** | `String` | Earliest version to document when generating documentation (`xdoc` goal). | No | — | |
| 52 | +| **`xdocFileName`** | `String` | Custom output file name for generated XDoc documentation (`xdoc` goal). | No | — | |
| 53 | +| **`velocityBasedir`** | `File` | Base directory of template files (required only for Velocity-based goals). | No | — | |
| 54 | + |
| 55 | +### Nested Elements |
| 56 | + |
| 57 | +#### `<model>` |
| 58 | + |
| 59 | +Specifies the Modello model description file to read. At least one `<model>` element is required. |
| 60 | +* **`file`** (`File`, Required): Path to the `.mdo` model file. |
| 61 | + |
| 62 | +```xml |
| 63 | +<model file="src/main/mdo/maven.mdo" /> |
| 64 | +``` |
| 65 | + |
| 66 | +#### `<goal>` |
| 67 | + |
| 68 | +Specifies the Modello generator target to run. At least one `<goal>` element is required. |
| 69 | +* **`name`** (`String`, Required): The generator plugin name (e.g., `java`, `xdoc`, `xsd`, `xpp3-reader`, `xpp3-writer`, `velocity`). |
| 70 | + |
| 71 | +```xml |
| 72 | +<goal name="java" /> |
| 73 | +``` |
| 74 | + |
| 75 | +#### `<packagedVersion>` |
| 76 | + |
| 77 | +Specifies an additional model version to package. Can be specified multiple times. |
| 78 | +* **`name`** (`String`, Required): The version string (e.g., `1.0.0`). |
| 79 | + |
| 80 | +```xml |
| 81 | +<packagedVersion name="1.0.0" /> |
| 82 | +<packagedVersion name="1.1.0" /> |
| 83 | +``` |
| 84 | + |
| 85 | +#### `<license>` |
| 86 | + |
| 87 | +Specifies license header content to prepend to generated sources. |
| 88 | +* **`file`** (`File`, Optional): File containing license header text. |
| 89 | +* **`text`** (`String`, Optional): Direct license text string (can also be passed as nested text). |
| 90 | + |
| 91 | +```xml |
| 92 | +<license file="${basedir}/LICENSE.txt" /> |
| 93 | +``` |
| 94 | + |
| 95 | +#### `<pluralException>` |
| 96 | + |
| 97 | +Specifies an irregular plural mapping used during field naming generation. |
| 98 | +* **`name`** (`String`, Required): The plural form (e.g., `aliases`). |
| 99 | +* **`value`** (`String`, Required): The singular form (e.g., `alias`). |
| 100 | + |
| 101 | +```xml |
| 102 | +<pluralException name="aliases" value="alias" /> |
| 103 | +``` |
| 104 | + |
| 105 | +#### `<template>` |
| 106 | + |
| 107 | +Specifies a custom template file when running the Velocity-based generator (`velocity` goal). |
| 108 | +* **`name`** (`String`, Required): Name or path of the template file relative to `velocityBasedir`. |
| 109 | + |
| 110 | +```xml |
| 111 | +<template name="model.vm" /> |
| 112 | +``` |
| 113 | + |
| 114 | +#### `<param>` |
| 115 | + |
| 116 | +Specifies custom velocity parameters passed to the generator context. |
| 117 | +* **`name`** (`String`, Required): Parameter name. |
| 118 | +* **`value`** (`String`, Required): Parameter value. |
| 119 | + |
| 120 | +```xml |
| 121 | +<param name="packageModelV4" value="org.example.model" /> |
| 122 | +``` |
| 123 | + |
| 124 | +#### `<property>` |
| 125 | + |
| 126 | +Passes arbitrary Modello parameters directly (equivalent to `-Dkey=value` on the CLI). |
| 127 | +* **`name`** (`String`, Required): Parameter key. |
| 128 | +* **`value`** (`String`, Required): Parameter value. |
| 129 | + |
| 130 | +```xml |
| 131 | +<property name="modello.package.with.version" value="true" /> |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +### Example 1: Standard Code Generation (Java & XSD) |
| 139 | + |
| 140 | +This example generates Java sources and XML Schema definitions (XSD) from a `model.mdo` file into the target folder `target/generated-sources`. |
| 141 | + |
| 142 | +```xml |
| 143 | +<target name="generate-model"> |
| 144 | + <mkdir dir="${basedir}/target/generated-sources" /> |
| 145 | + |
| 146 | + <modello version="1.0.0" |
| 147 | + outputDirectory="${basedir}/target/generated-sources" |
| 148 | + javaSource="11"> |
| 149 | + |
| 150 | + <!-- Path to the Modello model --> |
| 151 | + <model file="src/main/mdo/model.mdo" /> |
| 152 | + |
| 153 | + <!-- Goals to execute --> |
| 154 | + <goal name="java" /> |
| 155 | + <goal name="xsd" /> |
| 156 | + <goal name="xpp3-reader" /> |
| 157 | + <goal name="xpp3-writer" /> |
| 158 | + </modello> |
| 159 | +</target> |
| 160 | +``` |
| 161 | + |
| 162 | +### Example 2: Velocity-Based Generation |
| 163 | + |
| 164 | +This example demonstrates how to use the Velocity generator plugin to create files using a custom `.vm` template. |
| 165 | + |
| 166 | +```xml |
| 167 | +<target name="generate-velocity"> |
| 168 | + <mkdir dir="${basedir}/target/generated-templates" /> |
| 169 | + |
| 170 | + <modello version="2.0.0" |
| 171 | + outputDirectory="${basedir}/target/generated-templates" |
| 172 | + velocityBasedir="${basedir}/src/main/templates"> |
| 173 | + |
| 174 | + <model file="src/main/mdo/model.mdo" /> |
| 175 | + |
| 176 | + <!-- Execute Velocity generator --> |
| 177 | + <goal name="velocity" /> |
| 178 | + |
| 179 | + <!-- Template specification --> |
| 180 | + <template name="custom-report.vm" /> |
| 181 | + |
| 182 | + <!-- Parameters passed to the template context --> |
| 183 | + <param name="isMavenModel" value="true" /> |
| 184 | + <param name="packageModel" value="org.example.api" /> |
| 185 | + </modello> |
| 186 | +</target> |
| 187 | +``` |
| 188 | + |
0 commit comments