From XML to annotations: converting a Scipio ERP component

One real service definition from the manufacturing application, before and after the conversion, line by line, and the four steps to convert your own component with convertXmlToAnnotation and removeMigratedXml.

AI-generated illustration: a tangle of navy lines that straightens into parallel navy bars, one of them crimson
Image generated by AI

In Scipio ERP 4.0 a service, an entity, a controller and a screen can be a Java annotation instead of an XML file, and every application we ship was converted that way. Your own XML keeps loading, so there is no deadline. This post shows what the conversion does to one real definition, and how to run it on a component of your own.

One service, before and after#

The example is createBOMAssoc from the manufacturing application, the service that adds a component to a bill of materials. In 3.x it was defined in servicedef/services_bom.xml:

xml
<service name="createBOMAssoc" default-entity-name="ProductAssoc" engine="simple"
        location="component://manufacturing/script/org/ofbiz/manufacturing/bom/BomSimpleMethods.xml" invoke="createBOMAssoc" auth="true">
    <description>Add Product to Product Association</description>
    <auto-attributes include="pk" mode="IN" optional="false"/>
    <auto-attributes include="nonpk" mode="IN" optional="true"/>
    <attribute name="errorMessage" type="String" optional="true" mode="OUT"/>
    <override name="fromDate" optional="true"/>
</service>

In 4.0 the same definition is an annotation in BomServices.java:

java
public class BomServices {

    @Service(
        name = "createBOMAssoc",
        engine = "java",
        location = "com.ilscipio.scipio.manufacturing.event.BomSimpleMethods",
        invoke = "createBOMAssoc",
        description = "Add Product to Product Association",
        defaultEntityName = "ProductAssoc",
        auth = "true",
        entityAttributes = {
            @EntityAttributes(mode = "IN", include = "pk"),
            @EntityAttributes(mode = "IN", include = "nonpk", optional = "true")
        },
        attributes = {
            @Attribute(name = "errorMessage", type = "String", mode = "OUT", optional = "true")
        },
        overrideAttributes = {
            @OverrideAttribute(name = "fromDate", optional = "true")
        }
    )
    public interface CreateBOMAssoc {}
}

What maps to what#

  • The attributes of <service> become the attributes of @Service: default-entity-name is defaultEntityName, and so on.
  • <description> becomes description.
  • Each <auto-attributes> becomes an @EntityAttributes, each <attribute> an @Attribute, each <override> an @OverrideAttribute.
  • The annotation sits on an empty nested interface, CreateBOMAssoc, inside a holder class, BomServices. The interface has no members. It only carries the definition, and one holder class keeps the services of one area together, as one XML file did.

One line changed for another reason. In 3.x the service ran a Minilang simple method (engine="simple"). In 4.0 that logic is a Java class (engine="java"). That was a port of the implementation, done in the same release. The converter moves definitions, and it leaves your implementation where it is.

What you gain#

  • The attribute names are checked by the compiler. A misspelt defaultEntityNmae does not build, where a misspelt XML attribute only showed up at runtime, if at all.
  • Your IDE completes the attribute names and shows their documentation.
  • The definition lives in Java next to the code, in one language, and your refactoring tools and code search see it.

The values are still strings: a wrong entity name in defaultEntityName is still found at runtime, as before.

Convert your own component#

  1. Commit first. The conversion writes new Java files and, if you ask it to, deletes XML. A clean working tree gives you a diff you can review.

  2. Convert.

    bash
    ./gradlew convertXmlToAnnotation -Pcomponent=mycomponent

    To convert one file, pass -PxmlFile=path/to/file.xml instead. Add -PoutputPackage=com.example.mycomponent.service to choose the Java package of the new classes.

  3. Build, start and test. The XML is still there at this point, so you can compare.

  4. Remove the XML that was converted. See the list first:

    bash
    ./gradlew removeMigratedXml -Pcomponent=mycomponent -PdryRun=true
    ./gradlew removeMigratedXml -Pcomponent=mycomponent

    Or do steps 2 and 4 in one go with convertXmlToAnnotation -Pcomponent=mycomponent -PremoveXml=true.

Every annotation, with every attribute, is in the reference: Services , Entities , Controllers and Widgets .

More news

See it run on your own data.

A walk through Scipio ERP with the people who build it, on your catalogue and your processes. Or bring your agent and try the tools yourself.