/** * adapted from {@link DefaultMaven#doExecute(MavenExecutionRequest)} */ private void loadExtensions(MavenProject project, List<Exception> exceptions) { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); Collection<AbstractMavenLifecycleParticipant> lifecycleParticipants = getLifecycleParticipants(Arrays.asList(project)); if (!lifecycleParticipants.isEmpty()) { LegacySupport legacySupport = getComponent(LegacySupport.class); MavenSession session = legacySupport.getSession(); session.setCurrentProject(project); session.setProjects(Arrays.asList(project)); for (AbstractMavenLifecycleParticipant listener : lifecycleParticipants) { Thread.currentThread().setContextClassLoader(listener.getClass().getClassLoader()); try { listener.afterProjectsRead(session); } catch (MavenExecutionException e) { exceptions.add(e); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } } } }
/** * maven-plugin-testing-harness can not inject local repositories (though the real runtime supports it). * and the default stubs have no sufficient implementation of getPath(): * @see "http://maven.apache.org/plugin-testing/maven-plugin-testing-harness/examples/repositories.html" */ private ArtifactRepository provideLocalRepository() throws IllegalAccessException { DefaultArtifactRepositoryFactory factory = new DefaultArtifactRepositoryFactory(); setVariableValueToObject(factory, "factory", new org.apache.maven.repository.legacy.repository.DefaultArtifactRepositoryFactory()); LegacySupport legacySupport = new DefaultLegacySupport(); setVariableValueToObject(factory, "legacySupport", legacySupport); ArtifactRepository localRepository = factory.createArtifactRepository("local", "http://localhost", new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy()); setVariableValueToObject(localRepository, "basedir", LOCAL_REPOSITORY); return localRepository; }
private void loadExtensions(MavenProject project, List<Exception> exceptions) { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); Collection<AbstractMavenLifecycleParticipant> participants = getLifecycleParticipants(Collections.singletonList(project)); if (!participants.isEmpty()) { LegacySupport legacySupport = getMavenComponent(LegacySupport.class); MavenSession session = legacySupport.getSession(); session.setCurrentProject(project); session.setProjects(Collections.singletonList(project)); for (AbstractMavenLifecycleParticipant participant : participants) { Thread.currentThread().setContextClassLoader(participant.getClass().getClassLoader()); try { participant.afterProjectsRead(session); } catch (MavenExecutionException e) { exceptions.add(e); } finally { Thread.currentThread().setContextClassLoader(currentClassLoader); } } } }
public void runMavenRequest(MavenExecutionRequest request, Runnable runnable) { DefaultMaven maven = (DefaultMaven) getMavenComponent(Maven.class); RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request); request.getProjectBuildingRequest().setRepositorySession(repositorySystemSession); MavenSession mavenSession = new MavenSession( container, repositorySystemSession, request, new DefaultMavenExecutionResult()); LegacySupport legacySupport = getMavenComponent(LegacySupport.class); MavenSession previousSession = legacySupport.getSession(); legacySupport.setSession(mavenSession); try { for (AbstractMavenLifecycleParticipant participant : getLifecycleParticipants(Collections.emptyList())) { participant.afterSessionStart(mavenSession); } runnable.run(); } catch (MavenExecutionException e) { throw new RuntimeException(e); } finally { legacySupport.setSession(previousSession); } }
@Override protected MavenSession newMavenSession(MavenProject project) { MavenExecutionResult result = new DefaultMavenExecutionResult(); MavenSession session = new MavenSession(getContainer(), project.getProjectBuildingRequest().getRepositorySession(), currentMavenExecutionRequest, result); session.setCurrentProject(project); session.setProjects(Arrays.asList(project)); try { LegacySupport legacySupport = this.lookup(LegacySupport.class); legacySupport.setSession(session); } catch (Exception e) { e.printStackTrace(); } return session; }
public void executeWithMavenSession(MavenExecutionRequest request, Runnable runnable) { DefaultMaven maven = (DefaultMaven)getComponent(Maven.class); RepositorySystemSession repositorySession = maven.newRepositorySession(request); request.getProjectBuildingRequest().setRepositorySession(repositorySession); MavenSession mavenSession = new MavenSession(myContainer, repositorySession, request, new DefaultMavenExecutionResult()); LegacySupport legacySupport = getComponent(LegacySupport.class); MavenSession oldSession = legacySupport.getSession(); legacySupport.setSession(mavenSession); try { runnable.run(); } finally { legacySupport.setSession(oldSession); } }
@Override protected void setUp() throws Exception { super.setUp(); artifactFactory = lookup( ArtifactFactory.class); artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class ); RepositorySystemSession repoSession = initRepoSession(); MavenSession session = new MavenSession( getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult() ); LegacySupport legacySupport = lookup(LegacySupport.class); legacySupport.setSession( session ); }
public static SystemProperties from(LegacySupport legacySupport, MavenRuntime mavenRuntime) { SystemProperties systemProperties = new SystemProperties(); try { MavenProjectProperties projectProperties = mavenRuntime.getProjectProperties(SystemProperties.class); systemProperties.values.put("deadcode4j.version", projectProperties.getVersion()); } catch (MavenRuntimeException e) { LoggerFactory.getLogger(SystemProperties.class).debug("Failed to determine MavenRuntime.", e); } Properties properties = legacySupport.getSession().getRequest().getSystemProperties(); for (String key : KEYS.keySet()) { String property = emptyToNull(properties.getProperty(key)); if (property != null) { systemProperties.values.put(key, property); } } return systemProperties; }
public void executeWithMavenSession(MavenExecutionRequest request, Runnable runnable) { DefaultMaven maven = (DefaultMaven)getComponent(Maven.class); RepositorySystemSession repositorySession = maven.newRepositorySession(request); request.getProjectBuildingRequest().setRepositorySession(repositorySession); MavenSession mavenSession = new MavenSession(myContainer, repositorySession, request, new DefaultMavenExecutionResult()); LegacySupport legacySupport = getComponent(LegacySupport.class); MavenSession oldSession = legacySupport.getSession(); legacySupport.setSession(mavenSession); /** adapted from {@link DefaultMaven#doExecute(MavenExecutionRequest)} */ try { for (AbstractMavenLifecycleParticipant listener : getLifecycleParticipants(Collections.<MavenProject>emptyList())) { listener.afterSessionStart(mavenSession); } } catch (MavenExecutionException e) { throw new RuntimeException(e); } try { runnable.run(); } finally { legacySupport.setSession(oldSession); } }
private static RepositorySystemSession rss( PlexusContainer c ) { try { LegacySupport legacySupport = c.lookup( LegacySupport.class ); return legacySupport.getRepositorySession(); } catch ( ComponentLookupException e ) { throw new IllegalStateException( e ); } }
@Before public void setUp() throws Exception { objectUnderTest = new UpdateChecker(); repositoryMetadataManager = mock(RepositoryMetadataManager.class); mavenSession = mock(MavenSession.class); when(mavenSession.getCurrentProject()).thenReturn(new MavenProject()); LegacySupport legacySupport = mock(LegacySupport.class); when(legacySupport.getSession()).thenReturn(mavenSession); ReflectionUtils.setVariableValueInObject(objectUnderTest, "legacySupport", legacySupport); ReflectionUtils.setVariableValueInObject(objectUnderTest, "repositoryMetadataManager", repositoryMetadataManager); }
@SuppressWarnings("deprecation") // there's no non-deprecated constructor for MavenSession :| private void givenModes(NetworkModes networkMode, InteractivityModes interactivity) throws IllegalAccessException { DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); mavenExecutionRequest.setOffline(NetworkModes.OFFLINE == networkMode); mavenExecutionRequest.setInteractiveMode(InteractivityModes.INTERACTIVE == interactivity); mavenExecutionRequest.setSystemProperties(System.getProperties()); LegacySupport legacySupport = mock(LegacySupport.class); when(legacySupport.getSession()).thenReturn(new MavenSession(null, null, mavenExecutionRequest, null)); setVariableValueInObject(objectUnderTest, "legacySupport", legacySupport); }
private MavenResult internalResolveProject( File pom, List<String> activeProfiles, List<String> inactiveProfiles, List<ResolutionListener> dependencyTreeResolutionListeners) { MavenExecutionRequest request = newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList()); request.setUpdateSnapshots(updateSnapshots); AtomicReference<MavenResult> reference = new AtomicReference<>(); runMavenRequest( request, () -> { try { ProjectBuilder builder = getMavenComponent(ProjectBuilder.class); List<ProjectBuildingResult> resultList = builder.build( Collections.singletonList(pom), false, request.getProjectBuildingRequest()); ProjectBuildingResult result = resultList.get(0); MavenProject mavenProject = result.getProject(); RepositorySystemSession repositorySession = getMavenComponent(LegacySupport.class).getRepositorySession(); if (repositorySession instanceof DefaultRepositorySystemSession) { ((DefaultRepositorySystemSession) repositorySession) .setTransferListener(new ArtifactTransferListener(mavenProgressNotifier)); if (workspaceCache != null) { ((DefaultRepositorySystemSession) repositorySession) .setWorkspaceReader(new MavenWorkspaceReader(workspaceCache)); } } List<Exception> exceptions = new ArrayList<>(); loadExtensions(mavenProject, exceptions); mavenProject.setDependencyArtifacts( mavenProject.createArtifacts(getMavenComponent(ArtifactFactory.class), null, null)); ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest(); resolutionRequest.setArtifact(mavenProject.getArtifact()); resolutionRequest.setRemoteRepositories(mavenProject.getRemoteArtifactRepositories()); resolutionRequest.setArtifactDependencies(mavenProject.getDependencyArtifacts()); resolutionRequest.setListeners(dependencyTreeResolutionListeners); resolutionRequest.setLocalRepository(localRepo); resolutionRequest.setManagedVersionMap(mavenProject.getManagedVersionMap()); resolutionRequest.setResolveTransitively(true); resolutionRequest.setResolveRoot(false); ArtifactResolver resolver = getMavenComponent(ArtifactResolver.class); ArtifactResolutionResult resolve = resolver.resolve(resolutionRequest); mavenProject.setArtifacts(resolve.getArtifacts()); reference.set(new MavenResult(mavenProject, exceptions)); } catch (Exception e) { reference.set(new MavenResult(null, null, Collections.singletonList(e))); } }); return reference.get(); }
@NotNull public MavenExecutionResult doResolveProject(@NotNull final File file, @NotNull final List<String> activeProfiles, final List<ResolutionListener> listeners) throws RemoteException { final MavenExecutionRequest request = createRequest(file, activeProfiles, Collections.<String>emptyList(), Collections.<String>emptyList()); final AtomicReference<MavenExecutionResult> ref = new AtomicReference<MavenExecutionResult>(); executeWithMavenSession(request, new Runnable() { @Override public void run() { try { // copied from DefaultMavenProjectBuilder.buildWithDependencies ProjectBuilder builder = getComponent(ProjectBuilder.class); ProjectBuildingResult buildingResult = builder.build(new File(file.getPath()), request.getProjectBuildingRequest()); //builder.calculateConcreteState(project, config, false); MavenProject project = buildingResult.getProject(); // 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); ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest(); resolutionRequest.setArtifactDependencies(project.getDependencyArtifacts()); resolutionRequest.setArtifact(project.getArtifact()); resolutionRequest.setManagedVersionMap(project.getManagedVersionMap()); resolutionRequest.setLocalRepository(myLocalRepository); resolutionRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); resolutionRequest.setListeners(listeners); resolutionRequest.setResolveRoot(false); resolutionRequest.setResolveTransitively(true); RepositorySystemSession repositorySession = getComponent(LegacySupport.class).getRepositorySession(); if (repositorySession instanceof DefaultRepositorySystemSession) { ((DefaultRepositorySystemSession)repositorySession).setTransferListener(new TransferListenerAdapter(myCurrentIndicator)); if (myWorkspaceMap != null) { ((DefaultRepositorySystemSession)repositorySession).setWorkspaceReader(new Maven3WorkspaceReader(myWorkspaceMap)); } } ArtifactResolutionResult result = resolver.resolve(resolutionRequest); project.setArtifacts(result.getArtifacts()); // end copied from DefaultMavenProjectBuilder.buildWithDependencies ref.set(new MavenExecutionResult(project, new ArrayList<Exception>())); } catch (Exception e) { ref.set(handleException(e)); } } }); return ref.get(); }
public void testThatASystemScopedDependencyIsNotResolvedFromRepositories() throws Exception { // // We should get a whole slew of dependencies resolving this artifact transitively // Dependency d = new Dependency(); d.setGroupId( "org.apache.maven.its" ); d.setArtifactId( "b" ); d.setVersion( "0.1" ); d.setScope( Artifact.SCOPE_COMPILE ); Artifact artifact = repositorySystem.createDependencyArtifact( d ); ArtifactResolutionRequest request = new ArtifactResolutionRequest() .setArtifact( artifact ) .setResolveRoot( true ) .setResolveTransitively( true ) .setRemoteRepositories( getRemoteRepositories() ) .setLocalRepository( getLocalRepository() ); DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(); session.setLocalRepositoryManager( new SimpleLocalRepositoryManager( request.getLocalRepository().getBasedir() ) ); LegacySupport legacySupport = lookup( LegacySupport.class ); legacySupport.setSession( new MavenSession( getContainer(), session, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult() ) ); ArtifactResolutionResult result = repositorySystem.resolve( request ); resolutionErrorHandler.throwErrors( request, result ); assertEquals( 2, result.getArtifacts().size() ); // // System scoped version which should // d.setScope( Artifact.SCOPE_SYSTEM ); File file = new File( getBasedir(), "src/test/repository-system/maven-core-2.1.0.jar" ); assertTrue( file.exists() ); d.setSystemPath( file.getCanonicalPath() ); artifact = repositorySystem.createDependencyArtifact( d ); // // The request has not set any local or remote repositories as the system scoped dependency being resolved should only // give us the dependency off the disk and nothing more. // request = new ArtifactResolutionRequest() .setArtifact( artifact ) .setResolveRoot( true ) .setResolveTransitively( true ); result = repositorySystem.resolve( request ); resolutionErrorHandler.throwErrors( request, result ); assertEquals( 1, result.getArtifacts().size() ); // // Put in a bogus file to make sure missing files cause the resolution to fail. // file = new File( getBasedir(), "src/test/repository-system/maven-monkey-2.1.0.jar" ); assertFalse( file.exists() ); d.setSystemPath( file.getCanonicalPath() ); artifact = repositorySystem.createDependencyArtifact( d ); // // The request has not set any local or remote repositories as the system scoped dependency being resolved should only // give us the dependency off the disk and nothing more. // request = new ArtifactResolutionRequest() .setArtifact( artifact ) .setResolveRoot( true ) .setResolveTransitively( true ); try { result = repositorySystem.resolve( request ); resolutionErrorHandler.throwErrors( request, result ); } catch( Exception e ) { assertTrue( result.hasMissingArtifacts() ); } }