public MavenEnvironment(MavenSession aMavenSession, BuildPluginManager aBuildPluginManager, Log aLog, DependencyTreeBuilder aDependencyTreeBuilder, ArtifactRepository aLocalRepository, SecDispatcher aSecurityDispatcher, MavenProjectBuilder aProjectBuilder, LifecycleExecutor aLifecycleExecutor, ArtifactFactory aArtifactFactory, ArtifactMetadataSource aArtifactMetadataSource, ArtifactCollector aArtifactCollector, RuntimeInformation aRuntimeInformation, MojoExecution aExecution) { mavenSession = aMavenSession; buildPluginManager = aBuildPluginManager; log = aLog; dependencyTreeBuilder = aDependencyTreeBuilder; localRepository = aLocalRepository; securityDispatcher = aSecurityDispatcher; projectBuilder = aProjectBuilder; lifecycleExecutor = aLifecycleExecutor; artifactFactory = aArtifactFactory; artifactMetadataSource = aArtifactMetadataSource; artifactCollector = aArtifactCollector; runtimeInformation = aRuntimeInformation; mojoExecution = aExecution; }
private void setUpProject( File pomFile, AbstractMojo mojo ) throws Exception { MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); ArtifactRepositoryLayout localRepositoryLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); String path = "src/test/repository"; ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" + new File( path ).getAbsolutePath(), localRepositoryLayout ); MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null ); // this gets the classes for these tests of this mojo (exec plugin) onto the project classpath for the test project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() ); setVariableValueToObject( mojo, "project", project ); }
/** * * @param sink * @param project * @param mavenProjectBuilder * @param locale * @param listener */ public DependenciesRenderer( Sink sink, MavenProject project, MavenProjectBuilder mavenProjectBuilder, ArtifactRepository localRepository, ArtifactFactory factory, I18N i18n, Locale locale, ReportingResolutionListener listener, Log log ) { super( sink ); this.project = project; this.locale = locale; this.listener = listener; this.mavenProjectBuilder = mavenProjectBuilder; this.localRepository = localRepository; this.i18n = i18n; this.factory = factory; this.log = log; }
private void setUpProject( File pomFile, AbstractMojo mojo ) throws Exception { MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy( true, "never", "never" ); String localRepoUrl = "file://" + System.getProperty( "user.home" ) + "/.m2/repository"; ArtifactRepository localRepository = artifactRepositoryFactory.createArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout(), policy, policy ); ProfileManager profileManager = new DefaultProfileManager( getContainer() ); MavenProject project = projectBuilder.buildWithDependencies( pomFile, localRepository, profileManager ); //this gets the classes for these tests of this mojo (exec plugin) onto the project classpath for the test project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() ); setVariableValueToObject( mojo, "project", project ); }
/** * Manually constructs an list of effective reactor projects by recursively * searching parent and submodule projects. This allows the intention of the * reactor to be preserved, as long as it is fully available on disk, even * when building a submodule directly. * * @param defaultReactor Return value to use if a comprehensive list can not * be discovered. * @param baseProject {@link MavenProject} where invocation started. * @return A list of MavenProjects that can be treated as though within the * current reactor. * @throws ProjectBuildingException */ public static List<MavenProject> findEffectiveReactor( final List<MavenProject> defaultReactor, final MavenSession session, final MavenProject baseProject, final MavenProjectBuilder projectBuilder, final ArtifactRepository localRepository) throws ProjectBuildingException { final Set<MavenProject> reactor = new HashSet<MavenProject>(); final Set<MavenProject> visited = new HashSet<MavenProject>(); final ProfileManager profileManager = getProfileManager(session); findEffectiveReactor(reactor, visited, baseProject, baseProject, projectBuilder, localRepository, profileManager); if (reactor.size() <= 1 || !reactor.contains(baseProject)) return defaultReactor; return new ArrayList<MavenProject>(reactor); }
public static void initialize(MavenSession aMavenSession, BuildPluginManager aBuildPluginManager, Log aLog, DependencyTreeBuilder aDependencyTreeBuilder, ArtifactRepository aLocalRepository, SecDispatcher aSecurityDispatcher, MavenProjectBuilder aProjectBuilder, LifecycleExecutor aLifecycleExecutor, ArtifactFactory aArtifactFactory, ArtifactMetadataSource aArtifactMetadataSource, ArtifactCollector aArtifactCollector, RuntimeInformation aRuntimeInformation, MojoExecution aExecution) { ENVIRONMENT.set(new MavenEnvironment(aMavenSession, aBuildPluginManager, aLog, aDependencyTreeBuilder, aLocalRepository, aSecurityDispatcher, aProjectBuilder, aLifecycleExecutor, aArtifactFactory, aArtifactMetadataSource, aArtifactCollector, aRuntimeInformation, aExecution)); }
private void setUpProject( File pomFile, ExecMojo mojo ) throws Exception { MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); ArtifactRepositoryLayout localRepositoryLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); String path = "src/test/repository"; ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" + new File( path ).getAbsolutePath(), localRepositoryLayout ); mojo.setBasedir( File.createTempFile( "mvn-temp", "txt" ).getParentFile() ); MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null ); // this gets the classes for these tests of this mojo (exec plugin) onto the project classpath for the test project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() ); mojo.setProject( project ); mojo.setLog( new SystemStreamLog() { public boolean isDebugEnabled() { return true; } } ); }
/** * Finds the local root of the specified project. * * @param project The project to find the local root for. * @param localRepository the local repo. * @param globalProfileManager the global profile manager. * @param logger The logger to log to. * @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, Log logger ) { logger.info( "Searching for local aggregator root..." ); 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() ) { try { final MavenProject parentProject = builder.build( parent, localRepository, globalProfileManager ); if ( getAllChildModules( parentProject, logger ).contains( project.getBasedir().getName() ) ) { logger.debug( parentDir + " is an aggregator parent" ); project = parentProject; continue; } else { logger.debug( parentDir + " is not an aggregator parent" ); } } catch ( ProjectBuildingException e ) { logger.warn( e ); } } } logger.debug( "Local aggregation root is " + project.getBasedir() ); return project; } }
/** * Finds the local root of the specified project. * * @param project The project to find the local root for. * @param localRepository the local repo. * @param globalProfileManager the global profile manager. * @param logger The logger to log to. * @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, Log logger ) { logger.info( "Searching for local aggregator root..." ); while ( true ) { final File parentDir = project.getBasedir().getParentFile(); if ( parentDir.isDirectory() ) { logger.debug( "Checking to see if " + parentDir + " is an aggregator parent" ); File parent = new File( parentDir, "pom.xml" ); if ( parent.isFile() ) { try { final MavenProject parentProject = builder.build( parent, localRepository, globalProfileManager ); if ( getAllChildModules( parentProject, logger ).contains( project.getBasedir().getName() ) ) { logger.debug( parentDir + " is an aggregator parent" ); project = parentProject; continue; } else { logger.debug( parentDir + " is not an aggregator parent" ); } } catch ( ProjectBuildingException e ) { logger.warn( e ); } } } logger.debug( "Local aggregation root is " + project.getBasedir() ); return project; } }
protected void setUp() throws Exception { super.setUp(); mavenProjectBuilder = (MavenProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE); ArtifactFactory artifactFactory = (ArtifactFactory) getContainer() .lookup(ArtifactFactory.ROLE); final Map<String, Mojo> mojos = (Map<String, Mojo>) getContainer() .lookupMap(Mojo.ROLE); for (Mojo mojo : mojos.values()) { if (mojo instanceof Hyperjaxb3Mojo) { this.mojo = (Hyperjaxb3Mojo) mojo; } } MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) getContainer() .lookup(MavenSettingsBuilder.ROLE); ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) getContainer() .lookup(ArtifactRepositoryLayout.ROLE, "default"); Settings settings = settingsBuilder.buildSettings(); String url = settings.getLocalRepository(); if (!url.startsWith("file:")) { url = "file://" + url; } localRepository = new DefaultArtifactRepository("local", url, new DefaultRepositoryLayout()); }
public SnapshotFinder(final MavenProjectBuilder projectBuilder, final ArtifactRepository localRepository, @SuppressWarnings("rawtypes") final List remoteRepositories) { this.projectBuilder = projectBuilder; this.localRepository = localRepository; this.remoteRepositories = remoteRepositories; }
/** * Helper method to recursively populate a set of {@link MavenProject}s that * can be considered to be within the same reactor. */ private static void findEffectiveReactor(final Set<MavenProject> reactor, final Set<MavenProject> visited, final MavenProject currentProject, final MavenProject target, final MavenProjectBuilder projectBuilder, final ArtifactRepository localRepository, final ProfileManager profileManager) throws ProjectBuildingException { // short-circuit if already visited this project if (!visited.add(currentProject)) return; final File baseDir = currentProject.getBasedir(); // We only are interested in local projects if (baseDir != null && baseDir.exists()) { // If the current project lists any modules , then that project itself // needs to be included in the reactor if (currentProject.getModules().size() > 0) { reactor.add(currentProject); } // Recursively add each submodule to the reactor for (final Object o : currentProject.getModules()) { final File submodule = new File(baseDir.getAbsolutePath() + File.separator + o.toString() + File.separator + "pom.xml"); final MavenProject p = projectBuilder.build(submodule, localRepository, profileManager); reactor.add(p); findEffectiveReactor(reactor, visited, p, target, projectBuilder, localRepository, profileManager); } } // Recurse into parent if (currentProject.hasParent()) findEffectiveReactor(reactor, visited, currentProject.getParent(), target, projectBuilder, localRepository, profileManager); }
public MavenProjectBuilder getProjectBuilder() { return projectBuilder; }
@Test public void testInitialize() { assertNull(MavenEnvironment.get()); MavenSession aMavenSession = mock(MavenSession.class); BuildPluginManager aBuildPluginManager = mock(BuildPluginManager.class); Log aLog = mock(Log.class); DependencyTreeBuilder aDependencyTreeBuilder = mock(DependencyTreeBuilder.class); ArtifactRepository aLocalRepository = mock(ArtifactRepository.class); SecDispatcher aSecurityDispatcher = mock(SecDispatcher.class); MavenProjectBuilder aProjectBuilder = mock(MavenProjectBuilder.class); LifecycleExecutor aLifecycleExecutor = mock(LifecycleExecutor.class); ArtifactFactory aArtifactFactory = mock(ArtifactFactory.class); ArtifactMetadataSource aArtifactMetadataSource = mock(ArtifactMetadataSource.class); ArtifactCollector aArtifactCollector = mock(ArtifactCollector.class); RuntimeInformation aRuntimeInformation = mock(RuntimeInformation.class); MojoExecution theExecution = mock(MojoExecution.class); MavenEnvironment.initialize(aMavenSession, aBuildPluginManager, aLog, aDependencyTreeBuilder, aLocalRepository, aSecurityDispatcher, aProjectBuilder, aLifecycleExecutor, aArtifactFactory, aArtifactMetadataSource, aArtifactCollector, aRuntimeInformation, theExecution); MavenEnvironment theMavenEnvironment = MavenEnvironment.get(); assertNotNull(theMavenEnvironment); assertSame(aMavenSession, theMavenEnvironment.getMavenSession()); assertSame(aBuildPluginManager, theMavenEnvironment.getBuildPluginManager()); assertSame(aLog, theMavenEnvironment.getLog()); assertSame(aDependencyTreeBuilder, theMavenEnvironment.getDependencyTreeBuilder()); assertSame(aLocalRepository, theMavenEnvironment.getLocalRepository()); assertSame(aSecurityDispatcher, theMavenEnvironment.getSecurityDispatcher()); assertSame(aProjectBuilder, theMavenEnvironment.getProjectBuilder()); assertSame(aLifecycleExecutor, theMavenEnvironment.getLifecycleExecutor()); assertSame(aArtifactFactory, theMavenEnvironment.getArtifactFactory()); assertSame(aArtifactMetadataSource, theMavenEnvironment.getArtifactMetadataSource()); assertSame(aArtifactCollector, theMavenEnvironment.getArtifactCollector()); assertSame(aRuntimeInformation, theMavenEnvironment.getRuntimeInformation()); MavenEnvironment.cleanUp();; assertNull(MavenEnvironment.get()); }
@NotNull public MavenExecutionResult resolveProject(@NotNull final File file, @NotNull final List<String> activeProfiles, @NotNull final List<String> inactiveProfiles, List<ResolutionListener> listeners) { MavenExecutionRequest request = createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList()); ProjectBuilderConfiguration config = request.getProjectBuilderConfiguration(); request.getGlobalProfileManager().loadSettingsProfiles(mySettings); ProfileManager globalProfileManager = request.getGlobalProfileManager(); globalProfileManager.loadSettingsProfiles(request.getSettings()); List<Exception> exceptions = new ArrayList<Exception>(); MavenProject project = null; try { // copied from DefaultMavenProjectBuilder.buildWithDependencies MavenProjectBuilder builder = getComponent(MavenProjectBuilder.class); project = builder.build(new File(file.getPath()), config); builder.calculateConcreteState(project, config, false); // copied from DefaultLifecycleExecutor.execute findExtensions(project); // end copied from DefaultLifecycleExecutor.execute Artifact projectArtifact = project.getArtifact(); Map managedVersions = project.getManagedVersionMap(); ArtifactMetadataSource metadataSource = getComponent(ArtifactMetadataSource.class); project.setDependencyArtifacts(project.createArtifacts(getComponent(ArtifactFactory.class), null, null)); ArtifactResolver resolver = getComponent(ArtifactResolver.class); ArtifactResolutionResult result = resolver .resolveTransitively(project.getDependencyArtifacts(), projectArtifact, managedVersions, myLocalRepository, project.getRemoteArtifactRepositories(), metadataSource, null, listeners); project.setArtifacts(result.getArtifacts()); // end copied from DefaultMavenProjectBuilder.buildWithDependencies } catch (Exception e) { return handleException(e); } return new MavenExecutionResult(project, exceptions); }
/** * @param log * @param sink * @param locale * @param i18n * @param plugins * @param reports * @param project * @param mavenProjectBuilder * @param artifactFactory * @param localRepository */ public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set plugins, Set reports, MavenProject project, MavenProjectBuilder mavenProjectBuilder, ArtifactFactory artifactFactory, ArtifactRepository localRepository ) { super( sink ); this.log = log; this.locale = locale; this.plugins = new ArrayList( plugins ); this.reports = new ArrayList( reports ); this.i18n = i18n; this.project = project; this.mavenProjectBuilder = mavenProjectBuilder; this.artifactFactory = artifactFactory; this.localRepository = localRepository; }
public ProjectHelper(MavenProject project, MavenProjectBuilder projectBuilder, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository) { this.project = project; this.projectBuilder = projectBuilder; this.remoteRepositories = remoteRepositories; this.localRepository = localRepository; }
protected void setUp() throws Exception { super.setUp(); mavenProjectBuilder = (MavenProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE); }
public MavenProjectBuilder getMavenProjectBuilder() { return mavenProjectBuilder; }
public void setMavenProjectBuilder(MavenProjectBuilder mavenProjectBuilder) { this.mavenProjectBuilder = mavenProjectBuilder; }
@NotNull public MavenExecutionResult resolveProject(@NotNull final File file, @NotNull final List<String> activeProfiles, List<ResolutionListener> listeners) { MavenExecutionRequest request = createRequest(file, activeProfiles, Collections.<String>emptyList(), Collections.<String>emptyList()); ProjectBuilderConfiguration config = request.getProjectBuilderConfiguration(); request.getGlobalProfileManager().loadSettingsProfiles(mySettings); ProfileManager globalProfileManager = request.getGlobalProfileManager(); globalProfileManager.loadSettingsProfiles(request.getSettings()); List<Exception> exceptions = new ArrayList<Exception>(); MavenProject project = null; try { // copied from DefaultMavenProjectBuilder.buildWithDependencies MavenProjectBuilder builder = getComponent(MavenProjectBuilder.class); project = builder.build(new File(file.getPath()), config); builder.calculateConcreteState(project, config, false); // copied from DefaultLifecycleExecutor.execute findExtensions(project); // end copied from DefaultLifecycleExecutor.execute Artifact projectArtifact = project.getArtifact(); Map managedVersions = project.getManagedVersionMap(); ArtifactMetadataSource metadataSource = getComponent(ArtifactMetadataSource.class); project.setDependencyArtifacts(project.createArtifacts(getComponent(ArtifactFactory.class), null, null)); ArtifactResolver resolver = getComponent(ArtifactResolver.class); ArtifactResolutionResult result = resolver .resolveTransitively(project.getDependencyArtifacts(), projectArtifact, managedVersions, myLocalRepository, project.getRemoteArtifactRepositories(), metadataSource, null, listeners); project.setArtifacts(result.getArtifacts()); // end copied from DefaultMavenProjectBuilder.buildWithDependencies } catch (Exception e) { return handleException(e); } return new MavenExecutionResult(project, exceptions); }
@Override public MavenProjectBuilder getMavenProjectBuilder() { return this.mavenProjectBuilder; }
private void searchNext() { this.next = null; while (this.next == null && !this.dependencies.isEmpty()) { final Dependency dependency = this.dependencies.remove(0); if (dependency != null) { final String artifactId = dependency.getGroupId() + ":" + dependency.getArtifactId() //$NON-NLS-1$ + ":" + dependency.getVersion(); //$NON-NLS-1$ if (!this.treated.contains(artifactId)) { boolean isTreated = false; try { final Artifact dependencyArtifact = createArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(), dependency.getType()); resolveArtifact(dependencyArtifact); final MavenProjectBuilder builder = getMavenProjectBuilder(); final MavenProject dependencyProject = builder.buildFromRepository( dependencyArtifact, this.remoteRepositiories, getMavenSession().getLocalRepository()); if (dependencyProject != null) { if (this.isTransitive) { this.dependencies.addAll(dependencyProject.getDependencies()); } this.next = dependencyProject; isTreated = true; } } catch (MojoExecutionException | ProjectBuildingException e) { getBuildContext().addMessage( this.projectFile, 1, 1, "Unable to retreive the Maven plugin: " + artifactId, //$NON-NLS-1$ BuildContext.SEVERITY_WARNING, e); isTreated = true; } if (isTreated) { this.treated.add(artifactId); } } } } }
public void runTest( String generatorId, String pom, String descriptor, String outputPath ) throws Exception { File outputDir = getTestFile( outputPath ); DaemonGenerator generator = (DaemonGenerator) lookup( DaemonGenerator.ROLE, generatorId ); // ----------------------------------------------------------------------- // Build the MavenProject instance // ----------------------------------------------------------------------- MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) lookup( MavenSettingsBuilder.ROLE ); Settings settings = settingsBuilder.buildSettings(); ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); String localRepoUrl = new File( settings.getLocalRepository() ).toURL().toExternalForm(); ArtifactRepository localRepository = artifactRepositoryFactory.createDeploymentArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout(), false ); ProfileManager profileManager = new DefaultProfileManager( getContainer() ); File tempPom = createFilteredFile( pom ); MavenProject project = projectBuilder.buildWithDependencies( tempPom, localRepository, profileManager ); // ----------------------------------------------------------------------- // Clean the output directory // ----------------------------------------------------------------------- FileUtils.deleteDirectory( outputDir ); FileUtils.forceMkdir( outputDir ); // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- DaemonGeneratorService daemonGeneratorService = (DaemonGeneratorService) lookup( DaemonGeneratorService.ROLE ); Daemon model = daemonGeneratorService.loadModel( getTestFile( descriptor ) ); generator.generate( new DaemonGenerationRequest( model, project, localRepository, outputDir, "bin" ) ); }
/** * Replies the current maven session. Basically it is an internal component of Maven. * * <p>It is an attribute defined as: <pre><code> * <span>/</span>* <span>@</span>component role="org.apache.maven.project.MavenProjectBuilder" * * <span>@</span>required * * <span>@</span>readonly * <span>*</span>/ * private MavenProjectBuilder projectBuilder; * </code></pre> * * @return the maven session */ public abstract MavenProjectBuilder getMavenProjectBuilder();
/** * This method returns a {@link MavenProjectBuilder}. * * @return A {@link MavenProjectBuilder} is returned. */ protected final MavenProjectBuilder getProjectBuilder() { return mavenProjectBuilder; }