Skip to content

Commit

Permalink
mojohaus#704: Removing MavenProjectBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Nov 30, 2022
1 parent d420a62 commit fec361a
Show file tree
Hide file tree
Showing 38 changed files with 245 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,35 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.UrlModelSource;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
Expand Down Expand Up @@ -1462,37 +1468,43 @@ public static String getGroupId( Model model )
}

/**
* Finds the local root of the specified project.
* Finds the local root of the current project of the {@link MavenSession} instance.
*
* @param project The project to find the local root for.
* @param builder {@linkplain MavenProjectBuilder} object
* @param localRepository the local repo.
* @param globalProfileManager the global profile manager.
* @param projectBuilder {@link ProjectBuilder} instance
* @param mavenSession {@link MavenSession} instance
* @param logger The logger to log tog
* @return The local root (note this may be the project passed as an argument).
*/
public static MavenProject getLocalRoot( MavenProjectBuilder builder, MavenProject project,
ArtifactRepository localRepository, ProfileManager globalProfileManager,
public static MavenProject getLocalRoot( ProjectBuilder projectBuilder,
MavenSession mavenSession,
Log logger )
{
logger.info( "Searching for local aggregator root..." );
MavenProject project = mavenSession.getCurrentProject();
while ( true )
{
final File parentDir = project.getBasedir().getParentFile();
if ( parentDir != null && parentDir.isDirectory() )
{
logger.debug( "Checking to see if " + parentDir + " is an aggregator parent" );
File parent = new File( parentDir, "pom.xml" );
if ( parent.isFile() )
File parentFile = new File( parentDir, "pom.xml" );
if ( parentFile.isFile() )
{
try
{
final MavenProject parentProject =
builder.build( parent, localRepository, globalProfileManager );
if ( getAllChildModules( parentProject, logger ).contains( project.getBasedir().getName() ) )
ProjectBuildingResult result = projectBuilder.build( parentFile,
createProjectBuilderRequest( mavenSession ) );
if ( !result.getProblems().isEmpty() )
{
logger.warn( "Problems encountered during the computation of the local aggregation root." );
result.getProblems().forEach( p ->
logger.warn( "\t" + p.getMessage() ) );
}
if ( getAllChildModules( result.getProject(), logger )
.contains( project.getBasedir().getName() ) )
{
logger.debug( parentDir + " is an aggregator parent" );
project = parentProject;
project = result.getProject();
continue;
}
else
Expand All @@ -1511,6 +1523,60 @@ public static MavenProject getLocalRoot( MavenProjectBuilder builder, MavenProje
}
}

/**
* Retrieves the standalone superproject
*
* @param projectBuilder {@link ProjectBuilder} instance
* @param mavenSession {@link MavenSession} instance
* @param logger The logger to log tog
*
* @return superproject retrieved
* @throws ProjectBuildingException if the retrieval fails
*/
public static MavenProject getStandaloneSuperProject( ProjectBuilder projectBuilder,
MavenSession mavenSession,
Log logger ) throws ProjectBuildingException
{
ProjectBuildingResult result = projectBuilder.build( new UrlModelSource(
Objects.requireNonNull( PomHelper.class.getResource( "standalone.xml" ) ) ),
createProjectBuilderRequest( mavenSession, r -> r.setProcessPlugins( false ) ) );
if ( !result.getProblems().isEmpty() )
{
logger.warn( "Problems encountered during building of the superproject." );
result.getProblems().forEach( p ->
logger.warn( "\t" + p.getMessage() ) );
}
return result.getProject();
}

/**
* <p>Convenience method for creating a {@link ProjectBuildingRequest} instance based on maven session.</p>
* <p><u>Note:</u> The method initializes the remote repositories with the remote artifact repositories.
* Please use the initializers if you need to override this.</p>
* @param mavenSession {@link MavenSession} instance
* @param initializers optional additional initializers, which will be executed after the object is initialized
* @return constructed builder request
*/
@SafeVarargs
public static ProjectBuildingRequest createProjectBuilderRequest( MavenSession mavenSession,
Consumer<ProjectBuildingRequest>... initializers )
{
return new DefaultProjectBuildingRequest()
{{
setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
setResolveDependencies( false );
setLocalRepository( mavenSession.getLocalRepository() );
setRemoteRepositories( mavenSession.getCurrentProject().getRemoteArtifactRepositories() );
setBuildStartTime( mavenSession.getStartTime() );
setUserProperties( mavenSession.getUserProperties() );
setSystemProperties( mavenSession.getSystemProperties() );
setActiveProfileIds( mavenSession.getRequest().getActiveProfiles() );
setInactiveProfileIds( mavenSession.getRequest().getInactiveProfiles() );
setRepositorySession( mavenSession.getRepositorySession() );
Arrays.stream( initializers ).forEach( i -> i.accept( this ) );
}};
}

/**
* Builds a map of raw models keyed by module path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
Expand Down Expand Up @@ -159,11 +158,10 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
@Inject
protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, changeRecorders );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;

Expand Down Expand Up @@ -86,11 +85,10 @@ public abstract class AbstractVersionsDisplayMojo
@Inject
protected AbstractVersionsDisplayMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, changeRecorders );
}

@SuppressWarnings( "unchecked" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
Expand Down Expand Up @@ -92,14 +91,6 @@ public abstract class AbstractVersionsUpdaterMojo
*/
protected org.eclipse.aether.RepositorySystem aetherRepositorySystem;

/**
* The (injected) {@link MavenProjectBuilder} instance.
*/
/**
* @since 1.0-alpha-1
*/
protected final MavenProjectBuilder projectBuilder;

/**
* @since 1.0-alpha-1
*/
Expand Down Expand Up @@ -214,13 +205,11 @@ public abstract class AbstractVersionsUpdaterMojo
@Inject
protected AbstractVersionsUpdaterMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
Map<String, ChangeRecorder> changeRecorders )
{
this.repositorySystem = repositorySystem;
this.aetherRepositorySystem = aetherRepositorySystem;
this.projectBuilder = projectBuilder;
this.wagonManager = wagonManager;
this.changeRecorders = changeRecorders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.PomHelper;
Expand Down Expand Up @@ -118,22 +120,23 @@ public class CompareDependenciesMojo
protected File reportOutputFile;

/**
* The project builder used to initialize the remote project.
* The (injected) instance of {@link ProjectBuilder}
*
* @since 2.14.0
*/
protected MavenProjectBuilder mavenProjectBuilder;
protected final ProjectBuilder projectBuilder;

// ------------------------------ METHODS --------------------------

@Inject
public CompareDependenciesMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
MavenProjectBuilder mavenProjectBuilder,
ProjectBuilder projectBuilder,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, changeRecorders );
this.mavenProjectBuilder = mavenProjectBuilder;
super( repositorySystem, aetherRepositorySystem, wagonManager, changeRecorders );
this.projectBuilder = projectBuilder;
}

/**
Expand All @@ -157,30 +160,21 @@ protected void update( ModifiedPomXMLEventReader pom )
reportMode = false;
}

String[] remotePomParts = this.remotePom.split( ":" );
if ( remotePomParts.length != 3 )
String[] remoteGAV = this.remotePom.split( ":" );
if ( remoteGAV.length != 3 )
{
throw new MojoFailureException( " Invalid format for remotePom: " + remotePom );
}
String rGroupId = remotePomParts[0];
String rArtifactId = remotePomParts[1];
String rVersion = remotePomParts[2];

Artifact remoteArtifact = this.toArtifact( DependencyBuilder.newBuilder()
.withGroupId( rGroupId )
.withArtifactId( rArtifactId )
.withVersion( rVersion ).build() );

MavenProject remoteMavenProject = null;
try
{
remoteMavenProject =
mavenProjectBuilder.buildFromRepository( remoteArtifact,
session.getCurrentProject().getRemoteArtifactRepositories(),
session.getLocalRepository() );
remoteMavenProject = getRemoteMavenProject( remoteGAV[0], remoteGAV[1],
remoteGAV[2] );
}
catch ( ProjectBuildingException e )
catch ( ArtifactResolutionException | ProjectBuildingException e )
{
throw new MojoExecutionException( "Unable to build remote project " + remoteArtifact, e );
throw new MojoFailureException( e.getMessage() );
}

Map<String, Dependency> remoteDepsMap = new HashMap<>();
Expand Down Expand Up @@ -276,6 +270,40 @@ && isProcessingParent() )

}

/**
* Builds a {@link MavenProject} instance for the dependency with a given {@code groupId},
* {@code artifactId}, and {@code version}.
* @param groupId {@code groupId} of the dependency
* @param artifactId {@code artifactId} of the dependency
* @param version {@code version} of the dependency
* @return retrieved {@link MavenProject} instance for the given dependency
* @throws MojoExecutionException thrown if the artifact for the dependency could not be constructed
* @throws ProjectBuildingException thrown if the {@link MavenProject} instance could not be constructed
*/
private MavenProject getRemoteMavenProject( String groupId, String artifactId, String version )
throws MojoExecutionException, ArtifactResolutionException, ProjectBuildingException
{
Artifact remoteArtifact = toArtifact( DependencyBuilder.newBuilder()
.withGroupId( groupId )
.withArtifactId( artifactId )
.withVersion( version )
.build() );
ProjectBuildingResult result =
projectBuilder.build( remoteArtifact, true,
PomHelper.createProjectBuilderRequest( session,
r -> r.setProcessPlugins( false ),
r -> r.setRemoteRepositories( session.getCurrentProject()
.getPluginArtifactRepositories() ) ) );
if ( !result.getProblems().isEmpty() )
{
getLog().warn( "Problems encountered during construction of the POM for "
+ remoteArtifact.toString() );
result.getProblems().forEach( p ->
getLog().warn( "\t" + p.getMessage() ) );
}
return result.getProject();
}

/**
* Compare the dependency versions of the current project with the dependency versions of a remote project
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.Segment;
Expand Down Expand Up @@ -346,11 +345,10 @@ public class DisplayDependencyUpdatesMojo
@Inject
public DisplayDependencyUpdatesMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, changeRecorders );
}

// open for tests
Expand Down
Loading

0 comments on commit fec361a

Please sign in to comment.