Search code examples
mavenmaven-3xmlbeansxmlbeans-maven-plugin

Maven Xmlbeans Plugin - Generate & Retain files for specified schemas


I'm trying to develop XML-based software. I'm using Maven & XmlBeans.

I have a base-project which provides base-xml-schemas which define some elements, abstract types, etc,.

All other sub-projects also provides xml-schemas. But, these sub-projects imports/refers the base-xml-schemas to use & extend the elements / types defined by base-schemas.

Even though my sub-projects depends on base-project, xmlbeans plugin requires the base-xml-schemas to be present in schemaDirectory for its usage. As an implication, all my sub-projects now carry the duplicate content generated/compiled due to base-xml-schemas.

I want my sub-projects to have only those sub-project-specific schemas & content in their jar, as My base-project will be available to all applications in my Jboss & the types can be resolved in runtime.

Having understood that the xmlbean needs all the schemas referred to be present for code generation, now I want to delete all xmlbean-generated code due to the present of base-xml-schema and retain files related to sub-project specific schemas.

I have manually done this and my project compilation & runtime works fine, as my maven has dependency & classes are available in runtime.

My question is - How to automate this using maven so the files generated due to base-schema is deleted just after generation.


Solution

  • Maven xmlbeans plugin binds by default to the lifecycle phase: generate-sources.

    You will need to bind clean goal of the clean plugin to the above lifecycle phase.

    Refer to the link - Remove or delete resource files from target directory using pom file

    Configuration will be like this:

          <execution>
            <id>clean-autogenerated-code</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>clean</goal>
            </goals>
            <configuration>
             <filesets>
                <fileset>
                  <directory>some/relative/path/location/of/autogeneratd/code</directory>
                </fileset>
              </filesets>
            </configuration>
        </execution>
    

    directory location - provide exact directory location of the auto-generated classes.

    This you will need to configure in your all sub-projects from where you need to delete your auto-generated xml beans files. If you have any parent-pom all of your sub-projects, configure in that pom.xml file.