Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -73,8 +74,8 @@
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.repository.WorkspaceReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -214,9 +215,9 @@ private MavenExecutionResult doExecute(MavenExecutionRequest request) {
// so that @SessionScoped components can be @Injected into AbstractLifecycleParticipants.
//
sessionScope.enter();
try {
DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) newRepositorySession(request);
MavenSession session = new MavenSession(container, repoSession, request, result);
try (CloseableSession closeableSession = newCloseableSession(request)) {
AtomicReference<CloseableSession> closeableSessionRef = new AtomicReference<>(closeableSession);
MavenSession session = new MavenSession(closeableSessionRef::get, request, result);
session.setSession(defaultSessionFactory.getSession(session));

sessionScope.seed(MavenSession.class, session);
Expand All @@ -225,7 +226,7 @@ private MavenExecutionResult doExecute(MavenExecutionRequest request) {

legacySupport.setSession(session);

return doExecute(request, session, result, repoSession);
return doExecute(request, session, result, closeableSessionRef);
} finally {
sessionScope.exit();
}
Expand All @@ -235,7 +236,7 @@ private MavenExecutionResult doExecute(
MavenExecutionRequest request,
MavenSession session,
MavenExecutionResult result,
DefaultRepositorySystemSession repoSession) {
AtomicReference<CloseableSession> closeableSessionRef) {
try {
afterSessionStart(session);
} catch (MavenExecutionException e) {
Expand All @@ -244,7 +245,11 @@ private MavenExecutionResult doExecute(

try {
WorkspaceReader reactorReader = container.lookup(WorkspaceReader.class, ReactorReader.HINT);
repoSession.setWorkspaceReader(reactorReader);
closeableSessionRef.set(closeableSessionRef
.get()
.copy()
.setWorkspaceReader(reactorReader)
.build());
} catch (ComponentLookupException e) {
return addExceptionToResult(result, e);
}
Expand All @@ -265,11 +270,10 @@ private MavenExecutionResult doExecute(
}

try {
setupWorkspaceReader(session, repoSession);
closeableSessionRef.set(setupWorkspaceReader(session, closeableSessionRef.get()));
} catch (ComponentLookupException e) {
return addExceptionToResult(result, e);
}
repoSession.setReadOnly();
try {
afterProjectsRead(session);
} catch (MavenExecutionException e) {
Expand Down Expand Up @@ -336,7 +340,7 @@ private MavenExecutionResult doExecute(
return result;
}

private void setupWorkspaceReader(MavenSession session, DefaultRepositorySystemSession repoSession)
private CloseableSession setupWorkspaceReader(MavenSession session, CloseableSession repoSession)
throws ComponentLookupException {
// Desired order of precedence for workspace readers before querying the local artifact repositories
Set<WorkspaceReader> workspaceReaders = new LinkedHashSet<>();
Expand All @@ -350,7 +354,10 @@ private void setupWorkspaceReader(MavenSession session, DefaultRepositorySystemS
}
// 3) .. n) Project-scoped workspace readers
workspaceReaders.addAll(getProjectScopedExtensionComponents(session.getProjects(), WorkspaceReader.class));
repoSession.setWorkspaceReader(MavenChainedWorkspaceReader.of(workspaceReaders));
return repoSession
.copy()
.setWorkspaceReader(MavenChainedWorkspaceReader.of(workspaceReaders))
.build();
}

private void afterSessionStart(MavenSession session) throws MavenExecutionException {
Expand Down Expand Up @@ -404,8 +411,18 @@ private void persistResumptionData(MavenExecutionResult result, MavenSession ses
}
}

/**
* Nobody should ever use this method.
*
* @deprecated If you use this method and your code is not in Maven Core, stop doing this.
*/
@Deprecated
public RepositorySystemSession newRepositorySession(MavenExecutionRequest request) {
return repositorySessionFactory.newRepositorySession(request);
return newCloseableSession(request);
}

private CloseableSession newCloseableSession(MavenExecutionRequest request) {
return repositorySessionFactory.newRepositorySessionBuilder(request).build();
}

private void validateLocalRepository(MavenExecutionRequest request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class DefaultMavenExecutionRequest implements MavenExecutionRequest {

private boolean cacheNotFound = false;

private boolean ignoreMissingArtifactDescriptor = true;

private boolean ignoreInvalidArtifactDescriptor = true;

private List<Proxy> proxies;

private List<Server> servers;
Expand Down Expand Up @@ -174,6 +178,8 @@ public static MavenExecutionRequest copy(MavenExecutionRequest original) {
copy.setInteractiveMode(original.isInteractiveMode());
copy.setCacheNotFound(original.isCacheNotFound());
copy.setCacheTransferError(original.isCacheTransferError());
copy.setIgnoreMissingArtifactDescriptor(original.isIgnoreMissingArtifactDescriptor());
copy.setIgnoreInvalidArtifactDescriptor(original.isIgnoreInvalidArtifactDescriptor());
copy.setProxies(original.getProxies());
copy.setServers(original.getServers());
copy.setMirrors(original.getMirrors());
Expand Down Expand Up @@ -1034,6 +1040,28 @@ public MavenExecutionRequest setCacheNotFound(boolean cacheNotFound) {
return this;
}

@Override
public boolean isIgnoreMissingArtifactDescriptor() {
return ignoreMissingArtifactDescriptor;
}

@Override
public MavenExecutionRequest setIgnoreMissingArtifactDescriptor(boolean ignoreMissing) {
this.ignoreMissingArtifactDescriptor = ignoreMissing;
return this;
}

@Override
public boolean isIgnoreInvalidArtifactDescriptor() {
return ignoreInvalidArtifactDescriptor;
}

@Override
public MavenExecutionRequest setIgnoreInvalidArtifactDescriptor(boolean ignoreInvalid) {
this.ignoreInvalidArtifactDescriptor = ignoreInvalid;
return this;
}

@Override
public boolean isUseLegacyLocalRepository() {
return this.useLegacyLocalRepositoryManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ public interface MavenExecutionRequest {

MavenExecutionRequest setCacheNotFound(boolean cacheNotFound);

/**
* @since 4.0.0
*/
boolean isIgnoreMissingArtifactDescriptor();

/**
* @since 4.0.0
*/
MavenExecutionRequest setIgnoreMissingArtifactDescriptor(boolean ignoreMissing);

/**
* @since 4.0.0
*/
boolean isIgnoreInvalidArtifactDescriptor();

/**
* @since 4.0.0
*/
MavenExecutionRequest setIgnoreInvalidArtifactDescriptor(boolean ignoreInvalid);

// Profiles
List<Profile> getProfiles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.maven.api.Session;
Expand All @@ -46,18 +47,20 @@
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.RepositorySystemSession;

import static java.util.Objects.requireNonNull;

/**
* A Maven execution session.
*
*/
public class MavenSession implements Cloneable {
private MavenExecutionRequest request;
private final MavenExecutionRequest request;

private MavenExecutionResult result;
private final MavenExecutionResult result;

private RepositorySystemSession repositorySession;
private final Supplier<RepositorySystemSession> repositorySystemSessionSupplier;

private Properties executionProperties;
private final Properties executionProperties;

private ThreadLocal<MavenProject> currentProject = new ThreadLocal<>();

Expand Down Expand Up @@ -257,7 +260,7 @@ public void setParallel(boolean parallel) {
}

public RepositorySystemSession getRepositorySession() {
return repositorySession;
return repositorySystemSessionSupplier.get();
}

private Map<String, MavenProject> projectMap;
Expand All @@ -282,7 +285,7 @@ public void setAllProjects(List<MavenProject> allProjects) {
// Deprecated
//

private PlexusContainer container;
private final PlexusContainer container;

private final Settings settings;

Expand All @@ -294,6 +297,21 @@ public Map<String, MavenProject> getProjectMap() {
return projectMap;
}

public MavenSession(
Supplier<RepositorySystemSession> repositorySystemSessionSupplier,
MavenExecutionRequest request,
MavenExecutionResult result) {
this.container = null;
this.request = requireNonNull(request);
this.result = requireNonNull(result);
this.settings = adaptSettings(request);
this.repositorySystemSessionSupplier = requireNonNull(repositorySystemSessionSupplier);
Properties executionProperties = new Properties();
executionProperties.putAll(request.getSystemProperties());
executionProperties.putAll(request.getUserProperties());
this.executionProperties = executionProperties;
}

@Deprecated
public MavenSession(
PlexusContainer container,
Expand All @@ -304,7 +322,11 @@ public MavenSession(
this.request = request;
this.result = result;
this.settings = adaptSettings(request);
this.repositorySession = repositorySession;
this.repositorySystemSessionSupplier = () -> repositorySession;
Properties executionProperties = new Properties();
executionProperties.putAll(request.getSystemProperties());
executionProperties.putAll(request.getUserProperties());
this.executionProperties = executionProperties;
}

@Deprecated
Expand Down Expand Up @@ -363,6 +385,8 @@ public MavenSession(
this.request.setGoals(goals);
this.request.setBaseDirectory((executionRootDir != null) ? new File(executionRootDir) : null);
this.request.setStartTime(startTime);
this.result = null;
this.repositorySystemSessionSupplier = () -> null;
}

@Deprecated
Expand All @@ -375,7 +399,12 @@ public MavenSession(
this.request = request;
this.result = result;
this.settings = adaptSettings(request);
Properties executionProperties = new Properties();
executionProperties.putAll(request.getSystemProperties());
executionProperties.putAll(request.getUserProperties());
this.executionProperties = executionProperties;
setProjects(projects);
this.repositorySystemSessionSupplier = () -> null;
}

/**
Expand Down Expand Up @@ -431,12 +460,6 @@ public boolean isUsingPOMsFromFilesystem() {
*/
@Deprecated
public Properties getExecutionProperties() {
if (executionProperties == null) {
executionProperties = new Properties();
executionProperties.putAll(request.getSystemProperties());
executionProperties.putAll(request.getUserProperties());
}

return executionProperties;
}

Expand Down
Loading