Coverage Report - org.apache.maven.plugin.reactor.MakeMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
MakeMojo
86%
67/78
74%
28/38
10
 
 1  
 package org.apache.maven.plugin.reactor;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.HashSet;
 26  
 import java.util.List;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.apache.maven.artifact.ArtifactUtils;
 30  
 import org.apache.maven.plugin.AbstractMojo;
 31  
 import org.apache.maven.plugin.MojoExecutionException;
 32  
 import org.apache.maven.plugin.MojoFailureException;
 33  
 import org.apache.maven.project.MavenProject;
 34  
 import org.apache.maven.shared.invoker.Invoker;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 import org.codehaus.plexus.util.dag.DAG;
 37  
 import org.codehaus.plexus.util.dag.Vertex;
 38  
 
 39  
 /**
 40  
  * Goal to build a project X and all of the reactor projects on which X depends 
 41  
  *
 42  
  * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
 43  
  * @goal make
 44  
  * @aggregator
 45  
  * @phase process-sources
 46  
  */
 47  4
 public class MakeMojo
 48  
     extends AbstractMojo
 49  
 {
 50  
     /**
 51  
      * Location of the POM file; provided by Maven
 52  
      * @parameter expression="${basedir}"
 53  
      * 
 54  
      */
 55  
     File baseDir;
 56  
     
 57  
     /**
 58  
      * A list of every project in this reactor; provided by Maven
 59  
      * @parameter expression="${project.collectedProjects}"
 60  
      */
 61  
     List collectedProjects;
 62  
     
 63  
     /**
 64  
      * If you don't specify a groupId in your artifactList, we'll use this as the default groupId.
 65  
      * @parameter expression="${make.group}" default-value="${project.groupId}"
 66  
      * 
 67  
      */
 68  
     String defaultGroup;
 69  
     
 70  
     /**
 71  
      * A list of artifacts to build, e.g. "com.mycompany:bar,com.mycompany:foo" or just "foo,bar", or just "foo" 
 72  
      * @parameter expression="${make.artifacts}" default-value=""
 73  
      * @required
 74  
      */
 75  
     String artifactList;
 76  
     
 77  
     /**
 78  
      * A list of relative paths to build, e.g. "foo,baz/bar"
 79  
      * @parameter expression="${make.folders}" default-value=""
 80  
      * @required
 81  
      */
 82  
     String folderList;
 83  
     
 84  
     /**
 85  
      * Goals to run on subproject.
 86  
      * @parameter expression="${make.goals}" default-value="install"
 87  
      */
 88  
     String goals;
 89  
     
 90  
     /**
 91  
      * Provided by Maven
 92  
      * @component
 93  
      */
 94  
     Invoker invoker;
 95  
     
 96  
     /**
 97  
      * Don't really do anything; just print a command that describes what the command would have done
 98  
      * @parameter expression="${make.printOnly}"
 99  
      */
 100  4
     private boolean printOnly = false;
 101  
     
 102  
     /**
 103  
      * @component
 104  
      */
 105  
     SimpleInvoker simpleInvoker;
 106  
     
 107  
     /**
 108  
      * The artifact from which we'll resume, e.g. "com.mycompany:foo" or just "foo"
 109  
      * @parameter expression="${fromArtifact}"
 110  
      */
 111  
     String continueFromProject;
 112  
     
 113  
     /**
 114  
      * The project folder from which we'll resume
 115  
      * @parameter expression="${from}"
 116  
      */
 117  
     File continueFromFolder;
 118  
     
 119  
     public void execute()
 120  
         throws MojoExecutionException, MojoFailureException
 121  
     {
 122  4
         if ( artifactList == null && folderList == null ) {
 123  0
             throw new MojoFailureException("You must specify either folders or projects with -Dmake.folders=foo,baz/bar or -Dmake.projects=com.mycompany:foo,com.mycompany:bar");
 124  
         }
 125  
         String[] reactorIncludes;
 126  
         List sortedProjects;
 127  
         try
 128  
         {
 129  4
             if (collectedProjects.size() == 0) {
 130  0
                 throw new NonReactorException();
 131  
             }
 132  4
             SuperProjectSorter ps = new SuperProjectSorter( collectedProjects );
 133  4
             DAG dag = ps.getDAG();
 134  
             
 135  
             // gather projects
 136  4
             collectArtifactListFromFolderList( collectedProjects );
 137  4
             String[] artifacts = StringUtils.split( artifactList, "," );
 138  4
             Set visited = new HashSet();
 139  4
             Set out = new HashSet();
 140  8
             for (int i = 0; i < artifacts.length; i++) {
 141  4
                 String project = artifacts[i];
 142  4
                 if ( project.indexOf(':') == -1 ) {
 143  0
                     project = defaultGroup + ":" + project;
 144  
                 }
 145  4
                 Vertex projectVertex = dag.getVertex( project );
 146  4
                 if ( projectVertex == null ) throw new MissingProjectException(project);
 147  4
                 gatherProjects( projectVertex, ps, visited, out );
 148  
             }
 149  
             
 150  
             // sort them again
 151  4
             ps = new SuperProjectSorter( new ArrayList( out ) );
 152  4
             sortedProjects = ps.getSortedProjects();
 153  
             
 154  
             // construct array of relative POM paths
 155  4
             reactorIncludes = new String[sortedProjects.size()];
 156  13
             for ( int i = 0; i < sortedProjects.size(); i++ )
 157  
             {
 158  9
                 MavenProject mp = (MavenProject) sortedProjects.get( i );
 159  9
                 String path = RelativePather.getRelativePath( baseDir, mp.getFile() );
 160  9
                 reactorIncludes[i] = path;
 161  
             }
 162  
         }
 163  0
         catch (MojoFailureException e) {
 164  0
             throw e;
 165  
         }        
 166  0
         catch ( Exception e )
 167  
         {
 168  0
             throw new MojoExecutionException( "Problem generating dependency tree", e );
 169  4
         }
 170  
 
 171  4
         if (continueFromFolder != null || continueFromProject != null) {
 172  1
             ResumeMojo resumer = new ResumeMojo();
 173  1
             resumer.baseDir = baseDir;
 174  1
             resumer.collectedProjects = sortedProjects;
 175  1
             resumer.continueFromFolder = continueFromFolder;
 176  1
             resumer.continueFromProject = continueFromProject;
 177  1
             resumer.goals = goals;
 178  1
             resumer.invoker = invoker;
 179  1
             resumer.simpleInvoker = simpleInvoker;
 180  1
             resumer.printOnly = printOnly;
 181  1
             resumer.continueFromGroup = defaultGroup;
 182  1
             resumer.execute();
 183  1
         } else {
 184  3
             simpleInvoker.runReactor( reactorIncludes, Arrays.asList( goals.split( "," ) ), invoker, printOnly, getLog() );
 185  
         }
 186  
 
 187  4
     }
 188  
 
 189  
     void collectArtifactListFromFolderList(List collectedProjects) throws MojoFailureException
 190  
     {
 191  4
         if ( folderList == null )
 192  3
             return;
 193  1
         String[] folders = StringUtils.split( folderList, "," );
 194  1
         Set pathSet = new HashSet();
 195  2
         for ( int i = 0; i < folders.length; i++ )
 196  
         {
 197  1
             File file = new File( baseDir, folders[i] );
 198  1
             if ( !file.exists() )
 199  
             {
 200  0
                 throw new MojoFailureException("Folder doesn't exist: " + file.getAbsolutePath() );
 201  
             }
 202  1
             String path = file.getAbsolutePath();
 203  1
             pathSet.add( path );
 204  
         }
 205  1
         if (artifactList == null) artifactList = "";
 206  1
         StringBuffer artifactBuffer = new StringBuffer(artifactList);
 207  4
         for ( int i = 0; i < collectedProjects.size(); i++ )
 208  
         {
 209  3
             MavenProject mp = (MavenProject) collectedProjects.get( i );
 210  3
             if ( pathSet.contains( mp.getFile().getParentFile().getAbsolutePath() ) )
 211  
             {
 212  1
                 if ( artifactBuffer.length() > 0 )
 213  
                 {
 214  0
                     artifactBuffer.append( ',' );
 215  
                 }
 216  1
                 String id = ArtifactUtils.versionlessKey( mp.getGroupId(), mp.getArtifactId() );
 217  1
                 artifactBuffer.append( id );
 218  
             }
 219  
         }
 220  1
         if ( artifactBuffer.length() == 0 )
 221  
         {
 222  0
             throw new MojoFailureException("No folders matched: " + folderList);
 223  
         }
 224  1
         artifactList = artifactBuffer.toString();
 225  1
     }
 226  
 
 227  
     protected Set gatherProjects( Vertex v, SuperProjectSorter ps, Set visited, Set out )
 228  
     {
 229  5
         visited.add( v );
 230  5
         out.add( ps.getProjectMap().get( v.getLabel() ) );
 231  5
         List children = v.getChildren();
 232  8
         for ( int i = 0; i < children.size(); i++ )
 233  
         {
 234  3
             Vertex child = (Vertex) children.get( i );
 235  3
             if ( visited.contains( child ) )
 236  0
                 continue;
 237  3
             gatherProjects( child, ps, visited, out );
 238  
         }
 239  5
         return out;
 240  
     }
 241  
 }