[MNG-7391] add execution strategy+runner required by Maven Build Cache (#661)

* [MNG-7391] add execution strategy+runner required by Maven Build Cache
* Fix previous commit
* Fix javadoc
* Fix checkstyle
Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
This commit is contained in:
Hervé Boutemy 2022-03-09 11:35:05 +01:00 committed by GitHub
parent e8c35ae4ab
commit 8e5676c84a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 181 additions and 37 deletions

View File

@ -45,7 +45,7 @@ import org.slf4j.LoggerFactory;
@Singleton
public class DefaultLifecycles
{
public static final String[] STANDARD_LIFECYCLES = { "default", "clean", "site", "wrapper" };
public static final String[] STANDARD_LIFECYCLES = { "clean", "default", "site", "wrapper" };
private final Logger logger = LoggerFactory.getLogger( getClass() );
@ -117,6 +117,9 @@ public class DefaultLifecycles
return phaseToLifecycleMap;
}
/**
* Returns an ordered list of lifecycles
*/
public List<Lifecycle> getLifeCycles()
{
List<String> lifecycleIds = Arrays.asList( STANDARD_LIFECYCLES );

View File

@ -19,8 +19,6 @@ package org.apache.maven.lifecycle.internal;
* under the License.
*/
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@ -110,7 +108,7 @@ public class DefaultLifecyclePluginAnalyzer
Map<Plugin, Plugin> plugins = new LinkedHashMap<>();
for ( Lifecycle lifecycle : getOrderedLifecycles() )
for ( Lifecycle lifecycle : defaultLifeCycles.getLifeCycles() )
{
org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
lifecycleMappingForPackaging.getLifecycles().get( lifecycle.getId() );
@ -163,17 +161,6 @@ public class DefaultLifecyclePluginAnalyzer
}
}
private List<Lifecycle> getOrderedLifecycles()
{
// NOTE: The lifecycle order can affect implied execution ids so we better be deterministic.
List<Lifecycle> lifecycles = new ArrayList<>( defaultLifeCycles.getLifeCycles() );
lifecycles.sort( Comparator.comparing( Lifecycle::getId ) );
return lifecycles;
}
private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, LifecyclePhase goals )
{
InputSource inputSource = new InputSource();

View File

@ -48,6 +48,8 @@ import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoExecutionRunner;
import org.apache.maven.plugin.MojosExecutionStrategy;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginIncompatibleException;
@ -80,17 +82,21 @@ public class MojoExecutor
private final ReadWriteLock aggregatorLock = new ReentrantReadWriteLock();
private final MojosExecutionStrategy mojosExecutionStrategy;
@Inject
public MojoExecutor(
BuildPluginManager pluginManager,
MavenPluginManager mavenPluginManager,
LifecycleDependencyResolver lifeCycleDependencyResolver,
ExecutionEventCatapult eventCatapult )
ExecutionEventCatapult eventCatapult,
MojosExecutionStrategy mojosExecutionStrategy )
{
this.pluginManager = pluginManager;
this.mavenPluginManager = mavenPluginManager;
this.lifeCycleDependencyResolver = lifeCycleDependencyResolver;
this.eventCatapult = eventCatapult;
this.mojosExecutionStrategy = mojosExecutionStrategy;
}
public DependencyContext newDependencyContext( MavenSession session, List<MojoExecution> mojoExecutions )
@ -148,21 +154,27 @@ public class MojoExecutor
return Collections.unmodifiableCollection( scopes );
}
public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
public void execute( final MavenSession session,
final List<MojoExecution> mojoExecutions,
final ProjectIndex projectIndex )
throws LifecycleExecutionException
{
DependencyContext dependencyContext = newDependencyContext( session, mojoExecutions );
final DependencyContext dependencyContext = newDependencyContext( session, mojoExecutions );
PhaseRecorder phaseRecorder = new PhaseRecorder( session.getCurrentProject() );
final PhaseRecorder phaseRecorder = new PhaseRecorder( session.getCurrentProject() );
for ( MojoExecution mojoExecution : mojoExecutions )
mojosExecutionStrategy.execute( mojoExecutions, session, new MojoExecutionRunner()
{
execute( session, mojoExecution, projectIndex, dependencyContext, phaseRecorder );
}
@Override
public void run( MojoExecution mojoExecution ) throws LifecycleExecutionException
{
MojoExecutor.this.execute( session, mojoExecution, projectIndex, dependencyContext, phaseRecorder );
}
} );
}
public void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
private void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
DependencyContext dependencyContext, PhaseRecorder phaseRecorder )
throws LifecycleExecutionException
{

View File

@ -0,0 +1,46 @@
package org.apache.maven.plugin;
/*
* 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 org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.List;
/**
* Default mojo execution strategy. It just iterates over mojo executions and runs one by one
*/
@Named
@Singleton
public class DefaultMojosExecutionStrategy implements MojosExecutionStrategy
{
@Override
public void execute( List<MojoExecution> mojos, MavenSession session, MojoExecutionRunner mojoRunner )
throws LifecycleExecutionException
{
for ( MojoExecution mojoExecution : mojos )
{
mojoRunner.run( mojoExecution );
}
}
}

View File

@ -0,0 +1,36 @@
package org.apache.maven.plugin;
/*
* 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 org.apache.maven.lifecycle.LifecycleExecutionException;
/**
* Provides context for mojo execution. Invocation of {@link #run(MojoExecution)} will result in actual execution
*/
public interface MojoExecutionRunner
{
/**
* Runs mojo execution
*
* @param execution mojo execution
* @throws LifecycleExecutionException
*/
void run( MojoExecution execution ) throws LifecycleExecutionException;
}

View File

@ -0,0 +1,45 @@
package org.apache.maven.plugin;
/*
* 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 org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import java.util.List;
/**
* Interface allows overriding default mojo execution strategy For example it is possible wrap some mojo execution to
* decorate default functionality or skip some executions
*/
public interface MojosExecutionStrategy
{
/**
* Entry point to the execution strategy
*
* @param mojos list of mojos representing a project build
* @param session current session
* @param mojoExecutionRunner mojo execution task which must be invoked by a strategy to actually run it
* @throws LifecycleExecutionException
*/
void execute( List<MojoExecution> mojos, MavenSession session, MojoExecutionRunner mojoExecutionRunner )
throws LifecycleExecutionException;
}

View File

@ -492,6 +492,21 @@ public class DefaultMavenPluginManager
ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
if ( pluginRealm == null )
{
try
{
setupPluginRealm( pluginDescriptor, session, null, null, null );
}
catch ( PluginResolutionException e )
{
String msg = "Cannot setup plugin realm [mojoDescriptor=" + mojoDescriptor.getId()
+ ", pluginDescriptor=" + pluginDescriptor.getId() + "]";
throw new PluginConfigurationException( pluginDescriptor, msg, e );
}
pluginRealm = pluginDescriptor.getClassRealm();
}
if ( logger.isDebugEnabled() )
{
logger.debug( "Configuring mojo " + mojoDescriptor.getId() + " from plugin realm " + pluginRealm );

View File

@ -93,8 +93,8 @@ public class DefaultLifecyclesTest
DefaultLifecycles dl = new DefaultLifecycles( myLifecycles.stream()
.collect( Collectors.toMap( l -> l.getId(), l -> l ) ) );
assertThat( dl.getLifeCycles().get( 0 ).getId(), is( "default" ) );
assertThat( dl.getLifeCycles().get( 1 ).getId(), is( "clean" ) );
assertThat( dl.getLifeCycles().get( 0 ).getId(), is( "clean" ) );
assertThat( dl.getLifeCycles().get( 1 ).getId(), is( "default" ) );
assertThat( dl.getLifeCycles().get( 2 ).getId(), is( "site" ) );
assertThat( dl.getLifeCycles().get( 3 ).getId(), is( "wrapper" ) );
assertThat( dl.getLifeCycles().get( 4 ).getId(), is( "etl" ) );

View File

@ -21,14 +21,15 @@ import org.apache.maven.lifecycle.internal.DependencyContext;
import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
import org.apache.maven.lifecycle.internal.LifecycleDependencyResolver;
import org.apache.maven.lifecycle.internal.MojoExecutor;
import org.apache.maven.lifecycle.internal.PhaseRecorder;
import org.apache.maven.lifecycle.internal.ProjectIndex;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojosExecutionStrategy;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import java.util.ArrayList;
import java.util.Collections;
@ -47,24 +48,23 @@ public class MojoExecutorStub
BuildPluginManager pluginManager,
MavenPluginManager mavenPluginManager,
LifecycleDependencyResolver lifeCycleDependencyResolver,
ExecutionEventCatapult eventCatapult )
ExecutionEventCatapult eventCatapult,
MojosExecutionStrategy mojosExecutionStrategy )
{
super( pluginManager, mavenPluginManager, lifeCycleDependencyResolver, eventCatapult );
}
@Override
public void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
DependencyContext dependencyContext, PhaseRecorder phaseRecorder )
throws LifecycleExecutionException
{
executions.add( mojoExecution );
super( pluginManager, mavenPluginManager, lifeCycleDependencyResolver, eventCatapult, mojosExecutionStrategy );
}
@Override
public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
throws LifecycleExecutionException
{
executions.addAll(mojoExecutions);
executions.addAll( mojoExecutions );
}
@Override
public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session, ProjectIndex projectIndex ) throws LifecycleExecutionException
{
return null;
}