Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,30 @@
* under the License.
*/

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
* Comma delimited multiple goals. The goal may be specified using:
* <ul>
* <li>&lt;groupId&gt;:&lt;artifactId&gt;:&lt;goal&gt;</li>
* <li>&lt;groupId&gt;:&lt;artifactId&gt;:&lt;version&gt;:&lt;goal&gt;
* <li>&lt;groupId&gt;:&lt;artifactId&gt;:&lt;version&gt;:&lt;goal&gt;[&lt;configuration&gt;]
* </li>
*/
public class LifecyclePhase
{

private List<LifecycleMojo> mojos;

public LifecyclePhase()
Expand Down Expand Up @@ -57,12 +70,40 @@ public void set( String goals )

if ( StringUtils.isNotEmpty( goals ) )
{
String[] mojoGoals = StringUtils.split( goals, "," );

for ( String mojoGoal: mojoGoals )
// match <groupId>:<artifactId>:<version>:<goal>
String goalRegex = "(?<goal>[^:\\[,\\s]+(?::[^:\\[,\\s]+){2,3})";

// match [<configuration>]
String configurationRegex = "(?:\\[(?<configuration>(?:[^\\]]|(?:(?<=/)\\]))*)\\])?";

Pattern pattern = Pattern.compile( goalRegex + configurationRegex );
Matcher matcher = pattern.matcher( goals );

while ( matcher.find() )
{
String goal = matcher.group( "goal" );
String configuration = matcher.group( "configuration" );

LifecycleMojo lifecycleMojo = new LifecycleMojo();
lifecycleMojo.setGoal( mojoGoal.trim() );
lifecycleMojo.setGoal( goal );

if ( configuration != null )
{
// replace escaped /]
configuration = configuration.replace( "/]", "]" );

// add root configuration tag
configuration = "<configuration>" + configuration + "</configuration>";

try
{
lifecycleMojo.setConfiguration( Xpp3DomBuilder.build( new StringReader( configuration ) ) );
}
catch ( XmlPullParserException | IOException e )
{
throw new IllegalArgumentException( e );
}
}
mojos.add( lifecycleMojo );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;

import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.Test;

/**
Expand Down Expand Up @@ -55,19 +56,73 @@ public void testSet()
assertNotNull( phase.getMojos() );
assertEquals( 0, phase.getMojos().size() );

phase.set( "jar:jar, war:war" );
phase.set( "jar:jar:jar, war:war:war" );

List<LifecycleMojo> mojos = phase.getMojos();
assertNotNull( mojos );
assertEquals( 2, mojos.size() );

LifecycleMojo mojo1 = mojos.get(0);
assertNotNull( mojo1 );
assertEquals( "jar:jar", mojo1.getGoal() );
assertEquals( "jar:jar:jar", mojo1.getGoal() );

LifecycleMojo mojo2 = mojos.get(1);
assertNotNull( mojo2 );
assertEquals( "war:war", mojo2.getGoal() );
assertEquals( "war:war:war", mojo2.getGoal() );
}

@Test
public void testIncludedConfigurations()
{
String configuration = "\t<merge>true</merge>\n" +
"\t<scope>compile,deploy[production/],test</scope>\n" +
"\t<version>snapshot:1.5</version>\n";

LifecyclePhase phase = new LifecyclePhase();
assertNull( phase.getMojos() );

phase.set( "" );
assertNotNull( phase.getMojos() );
assertEquals( 0, phase.getMojos().size() );

phase.set( " org.jar:jar:3.0:jar-jar[" + configuration + "], war:war:war[" + configuration + "]" );

List<LifecycleMojo> mojos = phase.getMojos();
assertNotNull( mojos );
assertEquals( 2, mojos.size() );

LifecycleMojo mojo1 = mojos.get(0);
assertNotNull( mojo1 );
assertEquals( "org.jar:jar:3.0:jar-jar", mojo1.getGoal());
Xpp3Dom configuration1 = mojo1.getConfiguration();
assertNotNull( configuration1 );
assertEquals( "configuration", configuration1.getName() );
assertEquals( 3, configuration1.getChildCount() );

LifecycleMojo mojo2 = mojos.get(1);
assertNotNull( mojo2 );
assertEquals( "war:war:war", mojo2.getGoal() );
Xpp3Dom configuration2 = mojo2.getConfiguration();
assertNotNull( configuration2 );
assertEquals( "configuration", configuration2.getName() );
assertEquals( 3, configuration2.getChildCount() );
}

@Test
public void testEmptySet()
{
LifecyclePhase phase = new LifecyclePhase();
assertNull( phase.getMojos() );

phase.set( "" );
assertNotNull( phase.getMojos() );
assertEquals( 0, phase.getMojos().size() );

phase.set( " \\n\\t " );

List<LifecycleMojo> mojos = phase.getMojos();
assertNotNull( mojos );
assertEquals( 0, mojos.size() );
}
}