Skip to content

Commit

Permalink
[MINVOKER-174] add a property to configure build order (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: olivier lamy <olamy@apache.org>
  • Loading branch information
olamy committed Sep 5, 2019
1 parent 9efcc6e commit a9bcc8d
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/it/project-setup/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ under the License.
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<setupIncludes>
<setupInclude>project3</setupInclude>
<setupInclude>project6</setupInclude>
</setupIncludes>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
Expand Down
18 changes: 18 additions & 0 deletions src/it/project-setup/src/it/project3/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.ordinal=1
18 changes: 18 additions & 0 deletions src/it/project-setup/src/it/project6/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.ordinal=2
32 changes: 32 additions & 0 deletions src/it/project-setup/src/it/project6/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>project6</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
8 changes: 8 additions & 0 deletions src/it/project-setup/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ try
return false;
}

int indexProject3 = log.indexOf( "Building: project3" );
int indexProject6 = log.indexOf( "Building: project6" );
if(indexProject3 > indexProject6)
{
System.out.println( "FAILED! project3 should be build before project6" );
return false;
}

File reportFile = new File( basedir, "target/invoker-reports/BUILD-project1.xml" );
if ( !reportFile.exists() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -566,6 +567,12 @@ public abstract class AbstractInvokerMojo
* # Path to an alternate <code>settings.xml</code> to use for Maven invocation with this IT.
* # Since plugin version 3.0.1
* invoker.settingsFile = ../
*
* # An integer value to control run order of projects. sorted in the ascending order of the ordinal.
* In other words, the BuildJobs with the slowest numbers will be executed first
* # Since plugin version 3.2.1
* invoker.ordinal = 3
* invoker.ordinal = 1
* </pre>
*
* @since 1.2
Expand Down Expand Up @@ -691,7 +698,7 @@ public void execute()
setupReportsFolder();
}

BuildJob[] buildJobs;
List<BuildJob> buildJobs;
if ( pom == null )
{
try
Expand All @@ -716,10 +723,10 @@ public void execute()
+ "pom File parameter. Reason: " + e.getMessage(), e );
}

buildJobs = new BuildJob[] { new BuildJob( pom.getName(), BuildJob.Type.NORMAL ) };
buildJobs = Collections.singletonList( new BuildJob( pom.getName(), BuildJob.Type.NORMAL ) );
}

if ( ( buildJobs == null ) || ( buildJobs.length < 1 ) )
if ( buildJobs.isEmpty() )
{
doFailIfNoProjects();

Expand Down Expand Up @@ -754,7 +761,7 @@ else if ( cloneProjectsTo == null && "maven-plugin".equals( project.getPackaging
}

// First run setup jobs.
BuildJob[] setupBuildJobs = null;
List<BuildJob> setupBuildJobs = null;
try
{
setupBuildJobs = getSetupBuildJobsFromFolders();
Expand All @@ -765,20 +772,20 @@ else if ( cloneProjectsTo == null && "maven-plugin".equals( project.getPackaging
// TODO: Check shouldn't we fail in case of problems?
}

if ( ( setupBuildJobs != null ) && ( setupBuildJobs.length > 0 ) )
if ( !setupBuildJobs.isEmpty() )
{
// Run setup jobs in single thread
// mode.
//
// Some Idea about ordering?
getLog().info( "Running " + setupBuildJobs.length + " setup job"
+ ( ( setupBuildJobs.length < 2 ) ? "" : "s" ) + ":" );
getLog().info( "Running " + setupBuildJobs.size() + " setup job"
+ ( ( setupBuildJobs.size() < 2 ) ? "" : "s" ) + ":" );
runBuilds( projectsDir, setupBuildJobs, 1 );
getLog().info( "Setup done." );
}

// Afterwards run all other jobs.
BuildJob[] nonSetupBuildJobs = getNonSetupJobs( buildJobs );
List<BuildJob> nonSetupBuildJobs = getNonSetupJobs( buildJobs );
// We will run the non setup jobs with the configured
// parallelThreads number.
runBuilds( projectsDir, nonSetupBuildJobs, parallelThreads );
Expand Down Expand Up @@ -816,7 +823,7 @@ private void setupReportsFolder()
}
}

private BuildJob[] getNonSetupJobs( BuildJob[] buildJobs )
private List<BuildJob> getNonSetupJobs( List<BuildJob> buildJobs )
{
List<BuildJob> result = new LinkedList<>();
for ( BuildJob buildJob : buildJobs )
Expand All @@ -826,8 +833,7 @@ private BuildJob[] getNonSetupJobs( BuildJob[] buildJobs )
result.add( buildJob );
}
}
BuildJob[] buildNonSetupJobs = result.toArray( new BuildJob[result.size()] );
return buildNonSetupJobs;
return result;
}

private void handleScriptRunnerWithScriptClassPath()
Expand Down Expand Up @@ -858,7 +864,7 @@ private void handleScriptRunnerWithScriptClassPath()
scriptRunner.setClassPath( scriptClassPath );
}

private void writeSummaryFile( BuildJob[] buildJobs )
private void writeSummaryFile( List<BuildJob> buildJobs )
throws MojoExecutionException
{

Expand Down Expand Up @@ -1216,7 +1222,7 @@ static boolean alreadyCloned( String subpath, List<String> clonedSubpaths )
* @param buildJobs The build jobs to run must not be <code>null</code> nor contain <code>null</code> elements.
* @throws org.apache.maven.plugin.MojoExecutionException If any build could not be launched.
*/
private void runBuilds( final File projectsDir, BuildJob[] buildJobs, int runWithParallelThreads )
private void runBuilds( final File projectsDir, List<BuildJob> buildJobs, int runWithParallelThreads )
throws MojoExecutionException
{
if ( !localRepositoryPath.exists() )
Expand Down Expand Up @@ -2381,42 +2387,53 @@ private List<String> calculateExcludes()
* @throws IOException
* @see {@link #setupIncludes}
*/
private BuildJob[] getSetupBuildJobsFromFolders()
throws IOException
private List<BuildJob> getSetupBuildJobsFromFolders()
throws IOException, MojoExecutionException
{
List<String> excludes = calculateExcludes();

BuildJob[] setupPoms = scanProjectsDirectory( setupIncludes, excludes, BuildJob.Type.SETUP );
List<BuildJob> setupPoms = scanProjectsDirectory( setupIncludes, excludes, BuildJob.Type.SETUP );
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Setup projects: " + Arrays.asList( setupPoms ) );
getLog().debug( "Setup projects: " + setupPoms );
}

return setupPoms;
}

private static class OrdinalComparator implements Comparator
{
private static final OrdinalComparator INSTANCE = new OrdinalComparator();

@Override
public int compare( Object o1, Object o2 )
{
return Integer.compare( ( ( BuildJob ) o1 ).getOrdinal(), ( ( BuildJob ) o2 ).getOrdinal() );
}
}

/**
* Gets the build jobs that should be processed. Note that the order of the returned build jobs is significant.
*
* @return The build jobs to process, may be empty but never <code>null</code>.
* @throws java.io.IOException If the projects directory could not be scanned.
*/
BuildJob[] getBuildJobs()
throws IOException
List<BuildJob> getBuildJobs()
throws IOException, MojoExecutionException
{
BuildJob[] buildJobs;
List<BuildJob> buildJobs;

if ( invokerTest == null )
{
List<String> excludes = calculateExcludes();

BuildJob[] setupPoms = scanProjectsDirectory( setupIncludes, excludes, BuildJob.Type.SETUP );
List<BuildJob> setupPoms = scanProjectsDirectory( setupIncludes, excludes, BuildJob.Type.SETUP );
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Setup projects: " + Arrays.asList( setupPoms ) );
}

BuildJob[] normalPoms = scanProjectsDirectory( pomIncludes, excludes, BuildJob.Type.NORMAL );
List<BuildJob> normalPoms = scanProjectsDirectory( pomIncludes, excludes, BuildJob.Type.NORMAL );

Map<String, BuildJob> uniquePoms = new LinkedHashMap<>();
for ( BuildJob setupPom : setupPoms )
Expand All @@ -2431,7 +2448,7 @@ BuildJob[] getBuildJobs()
}
}

buildJobs = uniquePoms.values().toArray( new BuildJob[uniquePoms.size()] );
buildJobs = new ArrayList<>( uniquePoms.values() );
}
else
{
Expand Down Expand Up @@ -2474,12 +2491,12 @@ BuildJob[] getBuildJobs()
* @return The build jobs matching the patterns, never <code>null</code>.
* @throws java.io.IOException If the project directory could not be scanned.
*/
private BuildJob[] scanProjectsDirectory( List<String> includes, List<String> excludes, String type )
throws IOException
private List<BuildJob> scanProjectsDirectory( List<String> includes, List<String> excludes, String type )
throws IOException, MojoExecutionException
{
if ( !projectsDirectory.isDirectory() )
{
return new BuildJob[0];
return Collections.emptyList();
}

DirectoryScanner scanner = new DirectoryScanner();
Expand Down Expand Up @@ -2516,7 +2533,19 @@ private BuildJob[] scanProjectsDirectory( List<String> includes, List<String> ex
}
}

return matches.values().toArray( new BuildJob[matches.size()] );
List<BuildJob> projects = new ArrayList<>( matches.size() );

// setup ordinal values to have an order here
for ( BuildJob buildJob : matches.values() )
{
InvokerProperties invokerProperties =
getInvokerProperties( new File( projectsDirectory, buildJob.getProject() ).getParentFile(),
null );
buildJob.setOrdinal( invokerProperties.getOrdinal() );
projects.add( buildJob );
}
Collections.sort( projects, OrdinalComparator.INSTANCE );
return projects;
}

/**
Expand All @@ -2528,7 +2557,7 @@ private BuildJob[] scanProjectsDirectory( List<String> includes, List<String> ex
* contain <code>null</code> elements.
* @throws java.io.IOException If any path could not be relativized.
*/
private void relativizeProjectPaths( BuildJob[] buildJobs )
private void relativizeProjectPaths( List<BuildJob> buildJobs )
throws IOException
{
String projectsDirPath = projectsDirectory.getCanonicalPath();
Expand Down Expand Up @@ -2778,47 +2807,6 @@ private InvokerProperties getInvokerProperties( final File projectDirectory, Pro
props = new Properties();
}

// Path projectsSourceFolder = this.projectsDirectory.toPath();
// Path projectsTargetFolder;
// if ( cloneProjectsTo != null )
// {
// projectsTargetFolder = cloneProjectsTo.toPath();
// }
// else
// {
// projectsTargetFolder = projectsSourceFolder;
// }
//
// Path projectDir = projectsTargetFolder.relativize( projectDirectory.toPath() );
//
// for ( int i = 0; i < projectDir.getNameCount(); i++ )
// {
// Path subInvokerProperties;
// if ( i == 0 )
// {
// subInvokerProperties = projectsSourceFolder.resolve( invokerPropertiesFile );
// }
// else
// {
// subInvokerProperties =
// projectsSourceFolder.resolve( projectDir.subpath( 0, i ) ).resolve( invokerPropertiesFile );
// }
//
// getLog().debug( "Looking for " + subInvokerProperties );
//
// if ( Files.isRegularFile( subInvokerProperties ) )
// {
// try ( InputStream in = new FileInputStream( subInvokerProperties.toFile() ) )
// {
// props.load( in );
// }
// catch ( IOException e )
// {
// throw new MojoExecutionException( "Failed to read invoker properties: " + subInvokerProperties );
// }
// }
// }

File propertiesFile = new File( projectDirectory, invokerPropertiesFile );
if ( propertiesFile.isFile() )
{
Expand Down

0 comments on commit a9bcc8d

Please sign in to comment.