Guide to using Modello

Modello is a tool for generating resources from a simple model. From a simple model you can generate things like:

  • Java sources
  • XML serialization code for the model
  • XML deserialization code for model
  • Model documentation
  • XSD

A typical modello model looks like the following:

  1. <model xmlns="http://modello.codehaus.org/MODELLO/1.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://modello.codehaus.org/MODELLO/1.4.0 http://modello.codehaus.org/xsd/modello-1.4.0.xsd"
  3. xml.namespace="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/${version}"
  4. xml.schemaLocation="http://maven.apache.org/xsd/archetype-${version}.xsd">
  5. <id>archetype</id>
  6. <name>Archetype</name>
  7. <description>
  8. <![CDATA[Maven's model for the old archetype descriptor (ie for Archetype 1.0.x).
  9. <p>The metadata about an archetype is stored in the <code>archetype.xml</code> file located
  10. in the <code>META-INF/maven</code> directory of its jar file.</p>]]>
  11. </description>
  12.  
  13. <defaults>
  14. <default>
  15. <key>package</key>
  16. <value>org.apache.maven.archetype.model</value>
  17. </default>
  18. </defaults>
  19.  
  20. <classes>
  21. <class rootElement="true" xml.tagName="archetype">
  22. <name>ArchetypeModel</name>
  23. <description>Describes the assembly layout and packaging.</description>
  24. <version>1.0.0</version>
  25. <fields>
  26. <field>
  27. <name>id</name>
  28. <version>1.0.0</version>
  29. <required>true</required>
  30. <type>String</type>
  31. <description><![CDATA[The value should be the same as the artifactId in the archetype <code>pom.xml</code>.]]></description>
  32. </field>
  33. <field>
  34. <name>allowPartial</name>
  35. <version>1.0.0</version>
  36. <type>boolean</type>
  37. <description><![CDATA[Setting this option to <code>true</code> makes it possible to run the
  38. <code>archetype:create</code> even on existing projects.]]></description>
  39. </field>
  40.  
  41. <field xdoc.separator="blank">
  42. <name>sources</name>
  43. <version>1.0.0</version>
  44. <description><![CDATA[Files that will go into <code>src/main/java</code>.]]></description>
  45. <association>
  46. <type>Source</type>
  47. <multiplicity>*</multiplicity>
  48. </association>
  49. </field>
  50. <field>
  51. <name>resources</name>
  52. <version>1.0.0</version>
  53. <description><![CDATA[Files that will go into <code>src/main/resources</code>.]]></description>
  54. <association>
  55. <type>Resource</type>
  56. <multiplicity>*</multiplicity>
  57. </association>
  58. </field>
  59.  
  60. <field xdoc.separator="blank">
  61. <name>testSources</name>
  62. <version>1.0.0</version>
  63. <description><![CDATA[Files that will go into <code>src/test/java</code>.]]></description>
  64. <association xml.tagName="source">
  65. <type>Source</type>
  66. <multiplicity>*</multiplicity>
  67. </association>
  68. </field>
  69. <field>
  70. <name>testResources</name>
  71. <version>1.0.0</version>
  72. <description><![CDATA[Files that will go into <code>src/test/resources</code>.]]></description>
  73. <association xml.tagName="resource">
  74. <type>Resource</type>
  75. <multiplicity>*</multiplicity>
  76. </association>
  77. </field>
  78.  
  79. <field xdoc.separator="blank">
  80. <name>siteResources</name>
  81. <version>1.0.0</version>
  82. <description><![CDATA[Files that will go into <code>src/site</code>.]]></description>
  83. <association xml.tagName="resource">
  84. <type>Resource</type>
  85. <multiplicity>*</multiplicity>
  86. </association>
  87. </field>
  88. </fields>
  89. </class>
  90.  
  91. <class>
  92. <name>Source</name>
  93. <description>Describes a source file. Note that source files are always filtered, unlike resources that
  94. can be non-filtered.</description>
  95. <version>1.0.0</version>
  96. <fields>
  97. <field xml.content="true">
  98. <name>file</name>
  99. <version>1.0.0</version>
  100. <type>String</type>
  101. <description><![CDATA[The source file.]]></description>
  102. </field>
  103. <field xml.attribute="true">
  104. <name>encoding</name>
  105. <version>1.0.0</version>
  106. <type>String</type>
  107. <description><![CDATA[The encoding to be used when reading/writing this file.
  108. Platform encoding is used by default, or ISO-8859-1 when filename ends in <code>.properties</code>]]></description>
  109. </field>
  110. </fields>
  111. </class>
  112. <class>
  113. <name>Resource</name>
  114. <description>Describes a resource file.</description>
  115. <version>1.0.0</version>
  116. <fields>
  117. <field xml.content="true">
  118. <name>file</name>
  119. <version>1.0.0</version>
  120. <type>String</type>
  121. <description><![CDATA[The resource file.]]></description>
  122. </field>
  123. <field xml.attribute="true">
  124. <name>encoding</name>
  125. <version>1.0.0</version>
  126. <type>String</type>
  127. <description><![CDATA[The encoding to be used when reading/writing this file.
  128. Platform encoding is used by default, or ISO-8859-1 when filename ends in <code>.properties</code>]]></description>
  129. </field>
  130. <field xml.attribute="true">
  131. <name>filtered</name>
  132. <version>1.0.0</version>
  133. <type>boolean</type>
  134. <defaultValue>true</defaultValue>
  135. <description>A resource can be filtered, which means the file will be used as Velocity template.
  136. It can be non-filtered, which means the file will be copied without modification.</description>
  137. </field>
  138. </fields>
  139. </class>
  140. </classes>
  141. </model>
  142.  

To utilize Modello you would configure the maven-modello-plugin something like the following where you want to generate the Java sources for the model, the xpp3 serialization code and the xpp3 deserialization code:

  1. <project>
  2. ...
  3. <build>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.codehaus.modello</groupId>
  7. <artifactId>modello-maven-plugin</artifactId>
  8. <version>1.8.3</version>
  9. <executions>
  10. <execution>
  11. <goals>
  12. <!-- Generate the xpp3 reader code -->
  13. <goal>xpp3-reader</goal>
  14. <!-- Generate the xpp3 writer code -->
  15. <goal>xpp3-writer</goal>
  16. <!-- Generate the Java sources for the model itself -->
  17. <goal>java</goal>
  18. </goals>
  19. </execution>
  20. </executions>
  21. <configuration>
  22. <models>
  23. <model>src/main/mdo/descriptor.mdo</model>
  24. </models>
  25. <version>1.0.0</version>
  26. <useJava5>true</useJava5>
  27. </configuration>
  28. </plugin>
  29. </plugins>
  30. </build>
  31. ...
  32. </project>