Search code examples
javamavenjboss-forge

How to add Parent POM to a POM using JBOSS Forge API


I'm creating an addon that programmatically updates the existing POM, I'm able to add dependencies, repository, and various other information using DependencyFacet, MavenFacet etc., However, I'm unable to figure out how to add a parent POM. Following is the code I tried and it didn't work. Any help is appreciated as to which facet I can use and what is the syntax to use it.

 Parent p = new Parent();
 p.setGroupId("org.example");
 p.setArtifactId("application-master-pom");
 p.setVersion("1.1.0");
 MavenFacet mavenFacet = getFaceted().getFacet(MavenFacet.class);
 mavenFacet.getModel().setParent(p);

However the parent POM reference is not being added to the project pom on which the command I'm creating is executed.


Solution

  • I found the answer after some trial and errors...

    Parent p = new Parent();
    p.setGroupId("org.codehaus.griffon");
    p.setArtifactId("application-master-pom");
    p.setVersion("1.0.0");
    MavenFacet mavenFacet = getFaceted().getFacet(MavenFacet.class);
    Model model = mavenFacet.getModel();
    model.setParent(p);
    mavenFacet.setModel(model); 
    

    Instead of directly setting parent as in the question. If we get the model and set the parent and reset the model using maven facet, it is successfully updating the parent pom.