View Javadoc

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.Arrays;
24  import java.util.List;
25  
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.project.ProjectSorter;
31  import org.apache.maven.shared.invoker.Invoker;
32  
33  /**
34   * Goal to resume building a reactor at a certain point 
35   *
36   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
37   * @goal resume
38   * @aggregator
39   * @phase process-sources
40   */
41  public class ResumeMojo
42      extends AbstractMojo
43  {
44      
45      /**
46       * @parameter expression="${project.collectedProjects}"
47       */
48      List collectedProjects;
49      
50      /**
51       * Location of the file.
52       * @parameter expression="${basedir}"
53       */
54      File baseDir;
55      
56      /**
57       * @parameter expression="${make.group}" default-value="${project.groupId}"
58       */
59      String continueFromGroup;
60      
61      /**
62       * The artifact from which we'll resume, e.g. "com.mycompany:foo" or just "foo"
63       * @parameter expression="${fromArtifact}" default-value="null"
64       * @required
65       */
66      String continueFromProject;
67      
68      /**
69       * The project folder from which we'll resume
70       * @parameter expression="${from}" default-value="null"
71       * @required
72       */
73      File continueFromFolder;
74      
75      /**
76       * Goals to run on subproject
77       * @parameter expression="${make.goals}" default-value="install"
78       */
79      String goals;
80      
81      /**
82       * @component
83       */
84      Invoker invoker;
85      
86      /**
87       * Don't really do anything; just print a message that describes what the command would have done
88       * @parameter expression="${make.printOnly}"
89       */
90      boolean printOnly = false;
91      
92      /**
93       * @component
94       */
95      SimpleInvoker simpleInvoker;
96      
97      public void execute()
98          throws MojoExecutionException, MojoFailureException
99      {
100     	if ( "null".equals( continueFromProject ) )
101     	{
102     	    continueFromProject = null;
103     	}
104     	if ( new File( "null" ).equals( continueFromFolder ) )
105     	{
106     	    continueFromFolder = null;
107     	}
108         if ( continueFromFolder == null && continueFromProject == null )
109         {
110             throw new MojoFailureException("You must specify either a folder or a project with -Dfrom=baz/bar or -DfromArtifact=com.mycompany:foo (groupId is optional)");
111         }
112         if (continueFromFolder != null && continueFromProject != null )
113         {
114             throw new MojoFailureException("You can't specify both a folder (" + continueFromFolder + ") and an artifact (" + continueFromProject + ")");
115         }
116         if ( continueFromFolder != null && !continueFromFolder.exists() )
117         {
118             throw new MojoFailureException("Folder doesn't exist: " + continueFromFolder.getAbsolutePath() );
119         }
120         String[] reactorIncludes;
121         try
122         {
123             
124             if (collectedProjects.size() == 0) {
125                 throw new NonReactorException();
126             }
127             ProjectSorter ps = new ProjectSorter( collectedProjects );
128                         
129             List sortedProjects = ps.getSortedProjects();
130             
131             String projectName = null;
132             if ( continueFromProject != null)
133             {
134                 projectName = continueFromProject;
135                 if ( projectName.indexOf(':') != -1 ) {
136                     int index = continueFromProject.indexOf(':');
137                     continueFromGroup = continueFromProject.substring( 0, index );
138                     projectName = continueFromProject.substring( index+1 );
139                 }
140             }
141             
142             boolean found = false;
143             int i = 0;
144             for (; i < sortedProjects.size(); i++) {
145                 MavenProject mp = (MavenProject) sortedProjects.get( i );
146                 if ( continueFromFolder == null )
147                 {
148                     if ( continueFromGroup.equals( mp.getGroupId() ) && projectName.equals( mp.getArtifactId() ) )
149                     {
150                         found = true;
151                         break;
152                     }
153                 } else {
154                     if ( continueFromFolder.equals( mp.getFile().getParentFile() ) )
155                     {
156                         found = true;
157                         break;
158                     }
159                 }
160             }
161             
162             if (!found) throw new MissingProjectException(continueFromGroup + ":" + projectName);
163             
164             // construct array of relative POM paths
165             reactorIncludes = new String[sortedProjects.size() - i];
166             for ( int j = i; j < sortedProjects.size(); j++ )
167             {
168                 MavenProject mp = (MavenProject) sortedProjects.get( j );
169                 String path = RelativePather.getRelativePath( baseDir, mp.getFile() );
170                 reactorIncludes[j- i] = path;
171             }
172         }
173         catch (MojoFailureException e) {
174             throw e;
175         }
176         catch ( Exception e )
177         {
178             throw new MojoExecutionException( "Problem generating dependency tree", e );
179         }
180 
181         simpleInvoker.runReactor( reactorIncludes, Arrays.asList( goals.split( "," ) ), invoker, printOnly, getLog() );
182 
183     }
184 }