You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/07/01 19:38:00 UTC
[jira] [Commented] (MNG-7401) Make MavenSession#getCurrentProject() using a thread local
[ https://issues.apache.org/jira/browse/MNG-7401?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17561599#comment-17561599 ]
ASF GitHub Bot commented on MNG-7401:
-------------------------------------
michael-o commented on code in PR #743:
URL: https://github.com/apache/maven/pull/743#discussion_r912212544
##########
maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilderTest.java:
##########
@@ -0,0 +1,102 @@
+package org.apache.maven.lifecycle.internal;
+
+/*
+ * 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.
+ */
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.maven.execution.AbstractExecutionListener;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.DefaultMavenExecutionResult;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
+import org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder;
+import org.apache.maven.lifecycle.internal.stub.DefaultLifecyclesStub;
+import org.apache.maven.lifecycle.internal.stub.LifecycleTaskSegmentCalculatorStub;
+import org.apache.maven.lifecycle.internal.stub.LoggerStub;
+import org.apache.maven.lifecycle.internal.stub.MojoExecutorStub;
+import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.ContainerConfiguration;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.logging.Logger;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+public class LifecycleModuleBuilderTest extends PlexusTestCase
+{
+ @Override
+ protected void customizeContainerConfiguration( ContainerConfiguration configuration )
+ {
+ configuration.setAutoWiring( true );
+ configuration.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
+
+ }
+
+ public void testCurrentProject() throws Exception
+ {
+ List<MavenProject> currentProjects = new ArrayList<>();
+ MojoExecutorStub mojoExecutor = new MojoExecutorStub()
+ {
+ @Override
+ public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
+ throws LifecycleExecutionException
+ {
+ super.execute( session, mojoExecutions, projectIndex );
+ currentProjects.add( session.getCurrentProject() );
+ }
+ };
+
+ final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
+ MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
+ mavenExecutionRequest.setExecutionListener( new AbstractExecutionListener() );
+ mavenExecutionRequest.setGoals( Arrays.asList( "clean" ) );
+ mavenExecutionRequest.setDegreeOfConcurrency( 1 );
+ final MavenSession session = new MavenSession( null, null, mavenExecutionRequest, defaultMavenExecutionResult );
+ final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
+ session.setProjectDependencyGraph( dependencyGraphStub );
+ session.setProjects( dependencyGraphStub.getSortedProjects() );
+
+ LifecycleModuleBuilder moduleBuilder = lookup( LifecycleModuleBuilder.class );
+ set( moduleBuilder, "mojoExecutor", mojoExecutor );
+
+ LifecycleStarter ls = lookup( LifecycleStarter.class );
+ ls.execute( session );
+
+ assertNull( session.getCurrentProject() );
+ assertEquals( Arrays.asList( ProjectDependencyGraphStub.A, ProjectDependencyGraphStub.B, ProjectDependencyGraphStub.C,
+ ProjectDependencyGraphStub.X, ProjectDependencyGraphStub.Y, ProjectDependencyGraphStub.Z ), currentProjects );
+ }
+
+ static void set( Object obj, String field, Object v ) throws NoSuchFieldException, IllegalAccessException
+ {
+ Field f = obj.getClass().getDeclaredField( field );
+ f.setAccessible( true );
+ f.set( obj, v );
Review Comment:
Alright, thanks!
> Make MavenSession#getCurrentProject() using a thread local
> ----------------------------------------------------------
>
> Key: MNG-7401
> URL: https://issues.apache.org/jira/browse/MNG-7401
> Project: Maven
> Issue Type: Improvement
> Reporter: Christoph Läubrich
> Assignee: Guillaume Nodet
> Priority: Major
> Fix For: 3.9.0, waiting-for-feedback
>
>
> I noticed that a session is often cloned due to change the current project for a while.
> As this works for everyone passing down the session, consumers of the "upper session" (e.g. a SessionScoped Component) would never see this if they are (indirectly) called and e.g. use Session#getCurrentProject().
> I wonder if MavenSession could simply use a ThreadLocal for the currentProject (that is shared accross all cloned sessions), that way one would always get the correct value.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)