Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7433] Warn if in parallel build aggregator Mojo found #730

Closed
wants to merge 3 commits into from
Closed
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
101 changes: 101 additions & 0 deletions maven-core/src/main/java/org/apache/maven/internal/MessageHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.apache.maven.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.util.ArrayList;
import java.util.List;

/**
* Helper class to format warning messages to the console
*/
public class MessageHelper
{

public static final int DEFAULT_MAX_SIZE = 65;

private static final char BOX_CHAR = '*';

public static String separatorLine()
{
return separatorLine( DEFAULT_MAX_SIZE );
}

public static String separatorLine( int length )
{
StringBuilder sb = new StringBuilder( length );
repeat( sb, '*', length );
return sb.toString();
}

public static List<String> messageBox( String... lines )
{
return messageBox( DEFAULT_MAX_SIZE, lines );
}

public static List<String> messageBox( int size, String... lines )
{
int rem = size - 4;
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder( size );
// first line
sb.setLength( 0 );
repeat( sb, BOX_CHAR, size );
result.add( sb.toString() );
// lines
for ( String line : lines )
{
sb.setLength( 0 );
String[] words = line.split( " " );
for ( String word : words )
{
if ( sb.length() >= rem - word.length() - ( sb.length() > 0 ? 1 : 0 ) )
{
repeat( sb, ' ', rem - sb.length() );
result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
sb.setLength( 0 );
}
if ( sb.length() > 0 )
{
sb.append( ' ' );
}
sb.append( word );
}

while ( sb.length() < rem )
{
sb.append( ' ' );
}
result.add( BOX_CHAR + " " + sb + " " + BOX_CHAR );
}
// last line
sb.setLength( 0 );
repeat( sb, BOX_CHAR, size );
result.add( sb.toString() );
return result;
}

private static void repeat( StringBuilder sb, char c, int nb )
{
for ( int i = 0; i < nb; i++ )
{
sb.append( c );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.internal.MessageHelper;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.lifecycle.MissingProjectException;
import org.apache.maven.plugin.BuildPluginManager;
Expand All @@ -36,10 +37,13 @@
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.SessionData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -71,6 +75,8 @@
public class MojoExecutor
{

private static final Logger LOGGER = LoggerFactory.getLogger( MojoExecutor.class );

@Requirement
private BuildPluginManager pluginManager;

Expand Down Expand Up @@ -232,7 +238,18 @@ private static class ProjectLock implements AutoCloseable
boolean aggregator = mojoDescriptor.isAggregator();
acquiredAggregatorLock = aggregator ? aggregatorLock.writeLock() : aggregatorLock.readLock();
acquiredProjectLock = getProjectLock( session );
acquiredAggregatorLock.lock();
if ( !acquiredAggregatorLock.tryLock() )
{
int size = Math.max( MessageUtils.getTerminalWidth() - 15, MessageHelper.DEFAULT_MAX_SIZE );
for ( String s : MessageHelper.messageBox( size,
"An aggregator Mojo is already executing in parallel build, but aggregator "
+ "Mojos require exclusive access to reactor to prevent race conditions. This "
+ "mojo execution will be blocked until the aggregator work is done." ) )
{
LOGGER.warn( s );
}
acquiredAggregatorLock.lock();
}
acquiredProjectLock.lock();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.internal.MessageHelper;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.lifecycle.LifecycleNotFoundException;
import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
Expand All @@ -43,6 +44,7 @@
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand Down Expand Up @@ -103,34 +105,35 @@ public MavenExecutionPlan resolveBuildPlan( MavenSession session, MavenProject p
final Set<Plugin> unsafePlugins = executionPlan.getNonThreadSafePlugins();
if ( !unsafePlugins.isEmpty() )
{
logger.warn( "*****************************************************************" );
logger.warn( "* Your build is requesting parallel execution, but project *" );
logger.warn( "* contains the following plugin(s) that have goals not marked *" );
logger.warn( "* as @threadSafe to support parallel building. *" );
logger.warn( "* While this /may/ work fine, please look for plugin updates *" );
logger.warn( "* and/or request plugins be made thread-safe. *" );
logger.warn( "* If reporting an issue, report it against the plugin in *" );
logger.warn( "* question, not against maven-core *" );
logger.warn( "*****************************************************************" );
int size = Math.max( MessageUtils.getTerminalWidth() - 15, MessageHelper.DEFAULT_MAX_SIZE );
for ( String s : MessageHelper.messageBox( size,
"Your build is requesting parallel execution, but project contains the following "
+ "plugin(s) that have goals not marked as @threadSafe to support parallel building.",
"While this /may/ work fine, please look for plugin updates and/or "
+ "request plugins be made thread-safe.",
"If reporting an issue, report it against the plugin in question, not against maven-core." ) )
{
logger.warn( s );
}
if ( logger.isDebugEnabled() )
{
final Set<MojoDescriptor> unsafeGoals = executionPlan.getNonThreadSafeMojos();
logger.warn( "The following goals are not marked @threadSafe in " + project.getName() + ":" );
for ( MojoDescriptor unsafeGoal : unsafeGoals )
{
logger.warn( unsafeGoal.getId() );
logger.warn( " - " + unsafeGoal.getId() );
}
}
else
{
logger.warn( "The following plugins are not marked @threadSafe in " + project.getName() + ":" );
for ( Plugin unsafePlugin : unsafePlugins )
{
logger.warn( unsafePlugin.getId() );
logger.warn( " - " + unsafePlugin.getId() );
}
logger.warn( "Enable debug to see more precisely which goals are not marked @threadSafe." );
}
logger.warn( "*****************************************************************" );
logger.warn( MessageHelper.separatorLine( size ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.apache.maven.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.util.ArrayList;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MessageHelperTest
{

@Test
public void testBuilderCommon()
{
List<String> msgs = new ArrayList<>();
msgs.add( "*****************************************************************" );
msgs.add( "* Your build is requesting parallel execution, but project *" );
msgs.add( "* contains the following plugin(s) that have goals not marked *" );
msgs.add( "* as @threadSafe to support parallel building. *" );
msgs.add( "* While this /may/ work fine, please look for plugin updates *" );
msgs.add( "* and/or request plugins be made thread-safe. *" );
msgs.add( "* If reporting an issue, report it against the plugin in *" );
msgs.add( "* question, not against maven-core *" );
msgs.add( "*****************************************************************" );

assertEquals( msgs, MessageHelper.messageBox(
"Your build is requesting parallel execution, but project contains the following "
+ "plugin(s) that have goals not marked as @threadSafe to support parallel building.",
"While this /may/ work fine, please look for plugin updates and/or "
+ "request plugins be made thread-safe.",
"If reporting an issue, report it against the plugin in question, not against maven-core"
) );
}

@Test
public void testMojoExecutor()
{
List<String> msgs = new ArrayList<>();
msgs.add( "*****************************************************************" );
msgs.add( "* An aggregator Mojo is already executing in parallel build, *" );
msgs.add( "* but aggregator Mojos require exclusive access to reactor to *" );
msgs.add( "* prevent race conditions. This mojo execution will be blocked *" );
msgs.add( "* until the aggregator work is done. *" );
msgs.add( "*****************************************************************" );

assertEquals( msgs, MessageHelper.messageBox(
"An aggregator Mojo is already executing in parallel build, but aggregator "
+ "Mojos require exclusive access to reactor to prevent race conditions. This "
+ "mojo execution will be blocked until the aggregator work is done." ) );
}
}