mvn-scalaxb: Running scalaxb from Maven
Running scalaxb from Maven
The scalaxb-maven-plugin is provided to allow scalaxb to be run as part of a maven build. These steps describe how to use scalaxb in an existing Maven project. See below for a full example, that also includes configuration of the scala plugin.
Step 1: Add the plugin to the pom.xml
Add the following plugin definition to your pom.xml file, and change the packageName from 'ipo' to something more suitable for your project.
<plugin>
<groupId>org.scalaxb</groupId>
<artifactId>scalaxb-maven-plugin</artifactId>
<configuration>
<packageName>ipo</packageName>
</configuration>
<executions>
<execution>
<id>scalaxb</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>Note: The plugin is called scalaxb-maven-plugin, and not maven-scalaxb-plugin. This is consistent with the Maven convention that only plugins from Apache are named maven-xxx-plugin, but can seem inconsistent to those accustomed to just using the standard set of plugins.
For more help on the available configuration parameters, run:
mvn org.scalaxb:scalaxb-maven-plugin:help -Dgoal=generate -Ddetail
Step 2: Add your XSD files in src/main/xsd
By default, the scalaxb maven plugin looks for XSD files in src/main/xsd, so the simplest thing is to put your XSD files in there.
If that's not where your XSD files are, then the plugin can be configured using the xsdDirectory configuration parameter:
<configuration>
...
<xsdDirectory>${basedir}/path/to/xsd</xsdDirectory>
</configuration>Step 3: Run scalaxb to generate code from your XSD
To generate the Scala sources from the XSD files, run:
mvn generate-sources
The Scala sources will be generated in target/generated-sources/scalaxb when Maven runs the generate-sources phase of the lifecycle. This might be done automatically by your IDE.
There's no need to do this step if you're running a later phase in the Maven lifecycle anyway. For example, if you run mvn compile, there's no need to invoke mvn generate-sources beforehand.
That's it! See below for a complete example project.
Full example
A full example of using the scalaxb maven plugin is included below.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.scalaxb</groupId> <artifactId>scalaxb-example</artifactId> <version>0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.9.1</scala.version> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.scalaxb</groupId> <artifactId>scalaxb-maven-plugin</artifactId> <configuration> <packageName>ipo</packageName> </configuration> <executions> <execution> <id>scalaxb</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
src/main/xsd/ipo.xsd
<xs:schema targetNamespace="http://www.example.com/IPO"
elementFormDefault="qualified"
xmlns="http://www.example.com/IPO"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ipo="http://www.example.com/IPO">
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="USAddress">
<xs:complexContent>
<xs:extension base="ipo:Address">
<xs:sequence>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:positiveInteger"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>- Login to post comments
