How to manually create a MavenProject to get built-in properties

I am aware that this is a rather strange request so I'll be expecting a barrage of questions to elaborate.

NOTE: I'm executing this from a "main" or "test" and not within the context of a Maven plugin.

In short, I'm wanting to take a well known pom.xml and local/remote repositories and resolve all of the project's properties. Not only the properties located in the pom and all of its parent poms, but also the built-in properties such as ${project.build.target} and ${project.version} which aren't available when I use the following code:

private Properties loadMavenProperties(Properties originalProperties)
{
    final File pomFile = new File("pom.xml");
    final MavenProject mavenProject = loadProject(pomFile);
    if (mavenProject != null)
    {
        final Properties mavenProperties = loadMavenProperties(mavenProject);
        if (mavenProperties != null)
        {
            originalProperties.putAll(mavenProperties);
        }
    }

    return originalProperties;
}

private Properties loadMavenProperties(MavenProject mavenProject) throws IOException, XmlPullParserException
{
    if (mavenProject.getModel().getParent() == null)
    {
        return mavenProject.getProperties();
    }

    mavenProject.setRemoteArtifactRepositories(Arrays.asList(new ArtifactRepository[0]));

    final String userHome = System.getProperty("user.home");
    final String localRepoPath = "/.m2/repository";
    final File localRepo = new File(userHome, localRepoPath);

    final Aether aether = new Aether(mavenProject, localRepo);

    final Parent parent = mavenProject.getModel().getParent();
    try
    {
        final Artifact parentArtifact = aether.resolve(new DefaultArtifact(parent.toString()), "runtime").get(0);

        final Properties parentProperties = loadMavenProperties(loadProject(parentArtifact.getFile()));
        if (parentProperties != null)
        {
            parentProperties.putAll(mavenProject.getProperties());
        }

        return parentProperties;
    }
    catch (DependencyResolutionException e)
    {
        throw new DowngradedException(e);
    }
}

private MavenProject loadProject(File pomFile) throws IOException, XmlPullParserException
{
    if (pomFile != null && pomFile.exists())
    {
        FileReader reader = null;

        try
        {
            final MavenXpp3Reader mavenReader = new MavenXpp3Reader();
            reader = new FileReader(pomFile);
            Model model = mavenReader.read(reader);
            model.setPomFile(pomFile);

            return new MavenProject(model);
        }
        finally
        {
            reader.close();
        }
    }

    return null;
}

The works for all explicitly defined properties but none of the built-in properties.

Is this a lost cause?

Thanks for any insight.

Answers


Things like the version and build directories aren't exposed as properties, but come from the MavenProject class itself. See the JavaDocs.

In your specific examples, you'd get the version by calling getVersion() and build directory info by calling getBuild() which returns a Build object with the various directories in it.


Need Your Help

Manage partials with handlebarsjs

javascript handlebars.js

i want use a partials with array but also a main template with arrays.