Skip to content
Open
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
24 changes: 13 additions & 11 deletions impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,22 @@ private MavenExecutionResult doExecute(MavenExecutionRequest request) {
// so that @SessionScoped components can be @Injected into AbstractLifecycleParticipants.
//
sessionScope.enter();
MavenChainedWorkspaceReader chainedWorkspaceReader =
new MavenChainedWorkspaceReader(request.getWorkspaceReader(), ideWorkspaceReader);
try (CloseableSession closeableSession = newCloseableSession(request, chainedWorkspaceReader)) {
MavenSession session = new MavenSession(closeableSession, request, result);
session.setSession(defaultSessionFactory.newSession(session));
try {
MavenChainedWorkspaceReader chainedWorkspaceReader =
Comment thread
elharo marked this conversation as resolved.
new MavenChainedWorkspaceReader(request.getWorkspaceReader(), ideWorkspaceReader);
try (CloseableSession closeableSession = newCloseableSession(request, chainedWorkspaceReader)) {
MavenSession session = new MavenSession(closeableSession, request, result);
session.setSession(defaultSessionFactory.newSession(session));

sessionScope.seed(MavenSession.class, session);
sessionScope.seed(RepositorySystemSession.class, closeableSession); // fixed in Maven 3.10.x
sessionScope.seed(Session.class, session.getSession());
sessionScope.seed(InternalMavenSession.class, InternalMavenSession.from(session.getSession()));
sessionScope.seed(MavenSession.class, session);
sessionScope.seed(RepositorySystemSession.class, closeableSession); // fixed in Maven 3.10.x
sessionScope.seed(Session.class, session.getSession());
sessionScope.seed(InternalMavenSession.class, InternalMavenSession.from(session.getSession()));

legacySupport.setSession(session);
legacySupport.setSession(session);

return doExecute(request, session, result, chainedWorkspaceReader);
return doExecute(request, session, result, chainedWorkspaceReader);
}
} finally {
sessionScope.exit();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.List;

import org.apache.maven.api.services.Lookup;
import org.apache.maven.execution.BuildResumptionAnalyzer;
import org.apache.maven.execution.BuildResumptionDataRepository;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.graph.GraphBuilder;
import org.apache.maven.internal.impl.DefaultSessionFactory;
import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.resolver.RepositorySystemSessionFactory;
import org.apache.maven.session.scope.internal.SessionScope;
import org.eclipse.aether.repository.WorkspaceReader;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class DefaultMavenSessionScopeTest {

@Test
void testSessionScopeIsExitedOnWorkspaceReaderError() throws Exception {
WorkspaceReader badReader = mock(WorkspaceReader.class);
when(badReader.getRepository()).thenReturn(null);

File localRepoDir = Files.createTempDirectory("local-repo").toFile();
localRepoDir.deleteOnExit();
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
Comment on lines +53 to +55
.setLocalRepositoryPath(localRepoDir)
.setWorkspaceReader(badReader);

SessionScope sessionScope = new SessionScope();

DefaultMaven defaultMaven = new DefaultMaven(
mock(Lookup.class),
mock(ExecutionEventCatapult.class),
mock(LegacySupport.class),
sessionScope,
mock(RepositorySystemSessionFactory.class),
mock(GraphBuilder.class),
mock(BuildResumptionAnalyzer.class),
mock(BuildResumptionDataRepository.class),
null,
mock(DefaultSessionFactory.class),
null);

MavenExecutionResult result = defaultMaven.execute(request);

assertFalse(result.getExceptions().isEmpty());
Comment on lines +74 to +76

Field valuesField = SessionScope.class.getSuperclass().getDeclaredField("values");
valuesField.setAccessible(true);
List<?> values = (List<?>) valuesField.get(sessionScope);
assertTrue(values.isEmpty(), "Session scope must be exited even on workspace reader error");
Comment on lines +78 to +81
}
}
Loading