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 25, 2022
1 parent 97683ab commit 2c5e26e
Show file tree
Hide file tree
Showing 38 changed files with 203 additions and 190 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() != null )
{
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() != null )
{
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 @@ -39,7 +39,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 @@ -160,13 +159,11 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
@Inject
protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, artifactResolver,
changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, artifactResolver, changeRecorders );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.resolver.ArtifactResolver;
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 @@ -87,13 +86,11 @@ public abstract class AbstractVersionsDisplayMojo
@Inject
protected AbstractVersionsDisplayMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, artifactResolver,
changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, artifactResolver, changeRecorders );
}

@SuppressWarnings( "unchecked" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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.apache.maven.settings.Settings;
import org.codehaus.mojo.versions.api.ArtifactVersions;
Expand Down Expand Up @@ -96,14 +95,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 @@ -243,14 +234,12 @@ public abstract class AbstractVersionsUpdaterMojo
@Inject
protected AbstractVersionsUpdaterMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
Map<String, ChangeRecorder> changeRecorders )
{
this.repositorySystem = repositorySystem;
this.aetherRepositorySystem = aetherRepositorySystem;
this.projectBuilder = projectBuilder;
this.wagonManager = wagonManager;
this.artifactResolver = artifactResolver;
this.changeRecorders = changeRecorders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,12 @@ public class CompareDependenciesMojo
@Inject
public CompareDependenciesMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
MavenProjectBuilder mavenProjectBuilder,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, artifactResolver,
super( repositorySystem, aetherRepositorySystem, wagonManager, artifactResolver,
changeRecorders );
this.mavenProjectBuilder = mavenProjectBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
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.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.Segment;
Expand Down Expand Up @@ -348,13 +347,11 @@ public class DisplayDependencyUpdatesMojo
@Inject
public DisplayDependencyUpdatesMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, artifactResolver,
changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, artifactResolver, changeRecorders );
}

private static Set<Dependency> extractPluginDependenciesFromPluginsInPluginManagement( Build build )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
Expand All @@ -55,13 +54,11 @@ public class DisplayParentUpdatesMojo
@Inject
public DisplayParentUpdatesMojo( RepositorySystem repositorySystem,
org.eclipse.aether.RepositorySystem aetherRepositorySystem,
MavenProjectBuilder projectBuilder,
WagonManager wagonManager,
ArtifactResolver artifactResolver,
Map<String, ChangeRecorder> changeRecorders )
{
super( repositorySystem, aetherRepositorySystem, projectBuilder, wagonManager, artifactResolver,
changeRecorders );
super( repositorySystem, aetherRepositorySystem, wagonManager, artifactResolver, changeRecorders );
}

@Override
Expand Down
Loading

0 comments on commit 2c5e26e

Please sign in to comment.