zacker330
1/14/2017 - 4:53 AM

Creating a MavenProject from an ArtifactInfo inside a maven plugin.

Creating a MavenProject from an ArtifactInfo inside a maven plugin.

protected MavenProject buildProjectModel(ArtifactInfo artifactInfo) throws Exception {
    try {
        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
        configuration.setLocalRepository( localRepository );
        configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
        configuration.setProcessPlugins( false );
        configuration.setRepositoryMerging( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT );
        Properties properties = new Properties(  );
        for ( String key : session.getSystemProperties( ).keySet() ){
            properties.put( key, session.getSystemProperties().get(key) );
        }
        configuration.setSystemProperties( properties );
        configuration.setRepositorySession( session );

        org.apache.maven.artifact.Artifact artifact = new org.apache.maven.artifact.DefaultArtifact(
                artifactInfo.groupId, artifactInfo.artifactId,artifactInfo.version,
                "compile", "", "", new DefaultArtifactHandler());

        MavenProject project = projectBuilder.build(artifact, configuration).getProject();
        return project;
    } catch (Exception ex) {
        getLog().error("error in buildProjectModel ", ex);
    }
    return null;
}