Fork me on GitHub

Tutorial 4

E04: Reuse a subprojection to different Maven POM elements

A Maven project has a group id, artifact id and a version. So does a Maven dependency. Now we reuse the same sub projection for unrelated, but similar parts of the document. This is possible because the structure of our projection does not need to follow the structure of the document.

Second we define a simple setter in the projection interface to show how element values can be modified.

Projection API

@XBDocURL("resource://pom.xml")
public interface MavenPOM {
 
   /**
    * When I see an artifact with id and group and version, I call it an artifact.
    * (adapted from James Whitcomb Riley)
    */
    public interface Artifact {
        @XBRead("child::groupId")
        String getGroupId();
 
        @XBRead("child::artifactId")
        String getArtifactId();
 
        @XBRead("child::version")
        String getVersion();
    }
     
    @XBRead("/project/name")
    String getName();
 
    @XBWrite("/project/name")
    void setName(String name);
 
    @XBRead("/project")
    Artifact getProjectId();
 
    @XBRead("/project/dependencies/dependency")
    Artifact[] getDependencies();
}

Example Code

MavenPOM pom = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().fromURLAnnotation(MavenPOM.class);
pom.setName("New name");
assertTrue(pom.getDependencies().length>0);
for (Artifact artifact:pom.getDependencies()) {
    if (artifact.equals(pom.getProjectId())) {
        System.out.println("Hmm... your project depends on itself!");
        throw new AssertionError();
    }
}