1

I'm using maven to build a jar containing generated code from an schema file using jibx. To do this I'm using the jibx-maven-plugin with the schema-codegen goal. I want to include the generated binding.xml file as part of the resulting maven jar. Is there any way of directing the jar creation to include the generated binding.xml

Currently using:

<plugin>
    <groupId>org.jibx</groupId>
    <artifactId>jibx-maven-plugin</artifactId>
    <version>1.2.3</version>
    <configuration>
        <schemaLocation>src/main/jibx</schemaLocation>
        <includeSchemas>
            <includeSchema>dataoneTypes.xsd</includeSchema>
        </includeSchemas>
        <options>
            <package>org.dataone.ns.service.types.v1</package>
        </options>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>schema-codegen</goal>
            </goals>
        </execution>

    </executions>
</plugin>
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
David Loy
  • 963
  • 1
  • 8
  • 14

4 Answers4

1

David,

Good! While including the binding.xml file is not required, it is good practice. The new jibx-maven-plugin can use this file later when creating a new binding that is based on the original schema. There are plenty of examples in the JiBX source repository.

Since JiBX is OSGi enabled, it is also good practice to add an OSGi manifest when creating your jar file. This also simplifies including the binding.xml file. Even if you don't use OSGi, your jar will work fine. Here is what your project file should look like:

<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.dataone.ns.service</groupId>
    <artifactId>org.dataone.ns.service.types.v1</artifactId>
    <version>0.0.1</version>
    <packaging>bundle</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jibx</groupId>
                <artifactId>jibx-maven-plugin</artifactId>
                <version>1.2.3</version>

                <executions>
                    <execution>
                        <id>generate-java-code-from-schema</id>
                        <goals>
                            <goal>schema-codegen</goal>
                        </goals>
                        <configuration>
                            <schemaLocation>src/main/jibx</schemaLocation>
                            <includeSchemas>
                                <includeSchema>dataoneTypes.xsd</includeSchema>
                            </includeSchemas>
                            <options>
                                <package>org.dataone.ns.service.types.v1</package>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-binding</id>
                        <goals>
                            <goal>bind</goal>
                        </goals>
                        <configuration>
                            <schemaBindingDirectory>target/generated-sources</schemaBindingDirectory>
                            <includes>
                                <include>binding.xml</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Include-Resource>META-INF/binding.xml=${basedir}/target/generated-sources/binding.xml</Include-Resource>
                        <Export-Package>org.dataone.ns.service.types.v1.*;version=${project.version}</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-run</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-extras</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

</project>

Take a look at your jar file. Your classes, the binding.xml file, and OSGi manifest entries are there!

Don Corley jibx-maven-plugin author

Don Corley
  • 496
  • 2
  • 7
0

You can always use maven-antrun-plugin to copy your file(set) to target/classes.

Make sure that:

  • you attach the jibx plugin to a phase before package - best is generate-resources
  • you attach the antrun execution to the same or later, but again, before package - best is generate-resources or process-resources
  • the jibx plugin declaration precedes antrun declaration

Then you can use something like this:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
         <phase>generate-resources</phase>
         <goals>
           <goal>run</goal>
         </goals>
         <configuration>
           <tasks>
             <copy file="${project.build.directory}/PATH/TO/binding.xml" todir="${project.build.outputDirectory}/PATH/IN/JAR/"/>
           </tasks>
         </configuration>
       </execution>
     </executions>
</plugin>
...
Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
0

You can create your binding.xml in the target directory you want it to be placed in the jar like this:

...
<goals>
     <goal>schema-codegen</goal>
</goals>
<configuration>
...
     <targetDirectory>target/resources</targetDirectory>
...
</configuration>
...

When binding the code, you can use the refer to this directory with the <bindingDirectory> tag

bolidor
  • 39
  • 1
  • 6
0

You can do it using the add-resource goal of the build-helper-maven-plugin.

Example:

<build>
    <plugins>
        <plugin>
            <groupId>org.jibx</groupId>
            <artifactId>jibx-maven-plugin</artifactId>
            <version>1.2.3</version>
            <executions>
                <execution>
                    <id>generate-java-code-from-schema</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>schema-codegen</goal>
                    </goals>
                    <configuration>
                        <schemaLocation>src/main/resources</schemaLocation>
                        <includeSchemas>
                            <includeSchema>foobar.xsd</includeSchema>
                        </includeSchemas>
                    </configuration>
                </execution>
                <execution>
                    <id>compile-binding</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>bind</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>${project.build.directory}/generated-sources</directory>
                                <includes>
                                    <include>binding.xml</include>
                                </includes>
                                <targetPath>JiBX</targetPath>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

You will find the binding.xml file in your jar at:

JiBX/binding.xml
FrVaBe
  • 47,963
  • 16
  • 124
  • 157