Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:01:33+00:00 2026-06-12T07:01:33+00:00

How do I obtain a fully resolved Model of a pom file? This is

  • 0

How do I obtain a fully resolved Model of a pom file?

This is basically a rephrasing of How can i programmaticaly build the effective model of a pom file?

I’m building a maven plugin that performs some validation rules against a set of modules.
Those modules’ pom files are available but they’re not part of the reactor when the plugin is executed.

I can read a pom file and obtain the corresponding Model object using this method (removed exception handling for simplicity):

private Model pomToModel(String pathToPom) throws Exception {
    BufferedReader in = new BufferedReader(new FileReader(pathToPom));
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = reader.read(in);
    return model;
}

And it works but the Model object has only the same information that the pom file has.

How can I improve that method so that I can obtain a “fully resolved” Model object?
By fully resolved, I mean: with all the transitive dependencies and everything else from the parent poms.

Cheers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:01:34+00:00Added an answer on June 12, 2026 at 7:01 am

    I did it 🙂

    help:effective-pom and dependency:tree really did not help at all.

    I had to look at how maven builds the Model for the MavenProject that gets injected in the mojo.
    help:effective-pom already receives the resolved Model, and dependency:tree only builds a DependencyGraph, but it doesn’t load the whole model for a pom into memory.

    By using the code below I was able to get a Model object with all the information from the parent, with resolved ${property} expressions, and expanded transitive dependencies.

    Here’s how:

    1) Get a ModelResolver

    You will need an instance of interface org.apache.maven.model.resolution.ModelResolver.
    Unfortunately, maven doesn’t provide one easily via dependency injection (at least I couldn’t find one), so we’ll have to build one.
    To make things even better, the only two implementations of that interface are package protected, so you need to use some reflection magic to instantiate it.
    The concrete classes that implement it are DefaultModelResolver and ProjectModelResolver.
    I was able to build a DefaultModelResolver like this

    /**
     * The Maven Project Object
     * 
     * @parameter expression="${project}"
     * @required2.0
     * @readonly
     */
    protected MavenProject project;
    
    /**
     * @component
     */
    protected ArtifactResolver artifactResolver;
    
    /**
     * @component
     */
    protected RemoteRepositoryManager remoteRepositoryManager;
    
    private Object invoke( Object object, String method )
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        return object.getClass().getMethod( method ).invoke( object );
    }
    
    private org.apache.maven.model.resolution.ModelResolver makeModelResolver() throws MojoExecutionException {
        try {
            ProjectBuildingRequest projectBuildingRequest =
            (ProjectBuildingRequest) invoke( project, "getProjectBuildingRequest" );
    
            Class c = Class.forName("org.apache.maven.repository.internal.DefaultModelResolver");
            Constructor ct = c.getConstructor(new Class[]{RepositorySystemSession.class, 
                    RequestTrace.class, String.class,
                    ArtifactResolver.class, RemoteRepositoryManager.class,
                    List.class});
            ct.setAccessible(true);
            return (org.apache.maven.model.resolution.ModelResolver) ct.newInstance(new Object[]{
                    projectBuildingRequest.getRepositorySession(), 
                    null, null, artifactResolver, remoteRepositoryManager, 
                    project.getRemoteProjectRepositories()});
        } catch (Exception e) {
            throw new MojoExecutionException("Error instantiating DefaultModelResolver", e);
        }
    }
    

    2) Build the Model

    When you have a modelResolver, you can build the Model from a pom file like this:

    public Model resolveEffectiveModel(File pomfile) {
        try {
            return modelBuilder.build(makeModelBuildRequest(pomfile)).getEffectiveModel();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }   
    }
    
    private ModelBuildingRequest makeModelBuildRequest(File artifactFile) {
        DefaultModelBuildingRequest mbr = new DefaultModelBuildingRequest();
        mbr.setPomFile(artifactFile);
        mbr.setModelResolver(modelResolver); // <-- the hard-to-get modelResolver
        return mbr;
    }
    

    Doesn’t look pretty, but it worked for me.. 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I obtain the fully qualified path of an isolated storage file for
I want obtain full URL adres in php script, tell please, this code always
How can I obtain the count of faulted messages from a Windows Azure Storage
I need to obtain a Date object from this String:This is a example) M-27\nJUN-2012
I think this is a relatively simple problem. I wish to obtain some functionality
Building a file open dialog replacement. Much of it works now, but I would
As in question, i have form with file = forms.FileFIeld(widget = forms.FIleInput()) but this
How can you obtain a list of files (as a stringcollection or some other
I can't seem to find the answer anywhere, I'm trying to obtain a socket
I have the following function in my .emacs file to obtain the path of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.