View Javadoc

1   package org.apache.maven.continuum.release.phase;
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.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
29  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
30  import org.apache.maven.continuum.release.ContinuumReleaseException;
31  import org.apache.maven.profiles.DefaultProfileManager;
32  import org.apache.maven.profiles.ProfileManager;
33  import org.apache.maven.project.DuplicateProjectException;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.project.MavenProjectBuilder;
36  import org.apache.maven.project.ProjectBuildingException;
37  import org.apache.maven.project.ProjectSorter;
38  import org.apache.maven.settings.MavenSettingsBuilder;
39  import org.apache.maven.settings.Settings;
40  import org.apache.maven.shared.release.ReleaseExecutionException;
41  import org.apache.maven.shared.release.ReleaseFailureException;
42  import org.apache.maven.shared.release.ReleaseResult;
43  import org.apache.maven.shared.release.config.ReleaseDescriptor;
44  import org.apache.maven.shared.release.phase.AbstractReleasePhase;
45  import org.codehaus.plexus.PlexusConstants;
46  import org.codehaus.plexus.PlexusContainer;
47  import org.codehaus.plexus.context.Context;
48  import org.codehaus.plexus.context.ContextException;
49  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
50  import org.codehaus.plexus.util.dag.CycleDetectedException;
51  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
52  
53  /**
54   * Generate the reactor projects
55   *
56   * @author Edwin Punzalan
57   * @version $Id: GenerateReactorProjectsPhase.java 917967 2010-03-02 11:19:19Z oching $
58   */
59  public class GenerateReactorProjectsPhase
60      extends AbstractReleasePhase
61      implements Contextualizable
62  {
63      private MavenProjectBuilder projectBuilder;
64  
65      private MavenSettingsBuilder settingsBuilder;
66  
67      private PlexusContainer container;
68  
69      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, Settings settings, List reactorProjects )
70          throws ReleaseExecutionException, ReleaseFailureException
71      {
72          ReleaseResult result = new ReleaseResult();
73  
74          try
75          {
76              reactorProjects.addAll( getReactorProjects( releaseDescriptor ) );
77          }
78          catch ( ContinuumReleaseException e )
79          {
80              throw new ReleaseExecutionException( "Unable to get reactor projects: " + e.getMessage(), e );
81          }
82  
83          result.setResultCode( ReleaseResult.SUCCESS );
84  
85          return result;
86      }
87  
88      public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, Settings settings, List reactorProjects )
89          throws ReleaseExecutionException, ReleaseFailureException
90      {
91          return execute( releaseDescriptor, settings, reactorProjects );
92      }
93  
94      private List getReactorProjects( ReleaseDescriptor descriptor )
95          throws ContinuumReleaseException
96      {
97          List<MavenProject> reactorProjects = new ArrayList<MavenProject>();
98  
99          MavenProject project;
100         try
101         {
102             ArtifactRepository repository = getLocalRepository( descriptor.getAdditionalArguments() );
103 
104             project = projectBuilder.build( getProjectDescriptorFile( descriptor ), repository,
105                                                             getProfileManager( getSettings() ) );
106 
107             reactorProjects.add( project );
108 
109             addModules( reactorProjects, project, repository );
110         }
111         catch ( ProjectBuildingException e )
112         {
113             throw new ContinuumReleaseException( "Failed to build project.", e );
114         }
115 
116         try
117         {
118             reactorProjects = new ProjectSorter( reactorProjects ).getSortedProjects();
119         }
120         catch ( CycleDetectedException e )
121         {
122             throw new ContinuumReleaseException( "Failed to sort projects.", e );
123         }
124         catch ( DuplicateProjectException e )
125         {
126             throw new ContinuumReleaseException( "Failed to sort projects.", e );
127         }
128 
129         return reactorProjects;
130     }
131 
132     private void addModules( List<MavenProject> reactorProjects, MavenProject project, ArtifactRepository repository )
133         throws ContinuumReleaseException
134     {
135         for ( Object o : project.getModules() )
136         {
137             String moduleDir = o.toString();
138 
139             File pomFile = new File( project.getBasedir(), moduleDir + "/pom.xml" );
140 
141             try
142             {
143                 MavenProject reactorProject =
144                     projectBuilder.build( pomFile, repository, getProfileManager( getSettings() ) );
145 
146                 reactorProjects.add( reactorProject );
147 
148                 addModules( reactorProjects, reactorProject, repository );
149             }
150             catch ( ProjectBuildingException e )
151             {
152                 throw new ContinuumReleaseException( "Failed to build project.", e );
153             }
154         }
155     }
156 
157     private File getProjectDescriptorFile( ReleaseDescriptor descriptor )
158     {
159         String parentPath = descriptor.getWorkingDirectory();
160 
161         String pomFilename = descriptor.getPomFileName();
162         if ( pomFilename == null )
163         {
164             pomFilename = "pom.xml";
165         }
166 
167         return new File( parentPath, pomFilename );
168     }
169 
170     private ArtifactRepository getLocalRepository( String arguments )
171         throws ContinuumReleaseException
172     {
173         String localRepository = null;
174         boolean found = false;
175 
176         if ( arguments != null )
177         {
178             String[] args = arguments.split( " " );
179     
180             for ( String arg : args )
181             {
182                 if ( arg.contains( "-Dmaven.repo.local=" ) )
183                 {
184                     localRepository = arg.substring( arg.indexOf( "=" ) + 1 );
185 
186                     if ( localRepository.endsWith( "\"" ) )
187                     {
188                         localRepository = localRepository.substring( 0, localRepository.indexOf( "\"" ) );
189                         break;
190                     }
191 
192                     found = true;
193                     continue;
194                 }
195 
196                 if ( found )
197                 {
198                     localRepository += " " + arg;
199                     
200                     if ( localRepository.endsWith( "\"" ) )
201                     {
202                         localRepository = localRepository.substring( 0, localRepository.indexOf( "\"" ) );
203                         break;
204                     }
205                 }
206             }
207         }
208     
209         if ( localRepository == null )
210         {
211             localRepository = getSettings().getLocalRepository();
212         }
213 
214         return new DefaultArtifactRepository( "local-repository", "file://" + localRepository,
215                                               new DefaultRepositoryLayout() );
216     }
217 
218     private ProfileManager getProfileManager( Settings settings )
219     {
220         return new DefaultProfileManager( container, settings );
221     }
222 
223     private Settings getSettings()
224         throws ContinuumReleaseException
225     {
226         try
227         {
228             return settingsBuilder.buildSettings();
229         }
230         catch ( IOException e )
231         {
232             throw new ContinuumReleaseException( "Failed to get Maven Settings.", e );
233         }
234         catch ( XmlPullParserException e )
235         {
236             throw new ContinuumReleaseException( "Failed to get Maven Settings.", e );
237         }
238     }
239 
240     public void contextualize( Context context )
241         throws ContextException
242     {
243         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
244     }
245 }