View Javadoc
1   package org.apache.maven.shared.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 org.apache.maven.project.MavenProject;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFile;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.command.status.StatusScmResult;
27  import org.apache.maven.scm.manager.NoSuchScmProviderException;
28  import org.apache.maven.scm.provider.ScmProvider;
29  import org.apache.maven.scm.repository.ScmRepository;
30  import org.apache.maven.scm.repository.ScmRepositoryException;
31  import org.apache.maven.shared.release.ReleaseExecutionException;
32  import org.apache.maven.shared.release.ReleaseFailureException;
33  import org.apache.maven.shared.release.ReleaseResult;
34  import org.apache.maven.shared.release.config.ReleaseDescriptor;
35  import org.apache.maven.shared.release.env.ReleaseEnvironment;
36  import org.apache.maven.shared.release.scm.ReleaseScmCommandException;
37  import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
38  import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
39  import org.apache.maven.shared.release.scm.ScmTranslator;
40  import org.codehaus.plexus.util.SelectorUtils;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  import java.io.File;
44  import java.util.Arrays;
45  import java.util.HashSet;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  import java.util.Set;
50  
51  /**
52   * See if there are any local modifications to the files before proceeding with SCM operations and the release.
53   * 
54   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
55   * @plexus.component role="org.apache.maven.shared.release.phase.ReleasePhase" role-hint="scm-check-modifications"
56   */
57  public class ScmCheckModificationsPhase
58      extends AbstractReleasePhase
59  {
60      /**
61       * Tool that gets a configured SCM repository from release configuration.
62       * 
63       * @plexus.requirement
64       */
65      private ScmRepositoryConfigurator scmRepositoryConfigurator;
66      
67      /**
68       * SCM URL translators mapped by provider name.
69       *
70       * @plexus.requirement role="org.apache.maven.shared.release.scm.ScmTranslator"
71       */
72      private Map<String, ScmTranslator> scmTranslators;
73  
74      /**
75       * The filepatterns to exclude from the status check.
76       * 
77       * @todo proper construction of filenames, especially release properties
78       */
79      private Set<String> exclusionPatterns = new HashSet<String>( Arrays.asList(
80          "**" + File.separator + "pom.xml.backup", "**" + File.separator + "pom.xml.tag",
81          "**" + File.separator + "pom.xml.next", "**" + File.separator + "pom.xml.branch",
82          "**" + File.separator + "release.properties", "**" + File.separator + "pom.xml.releaseBackup" ) );
83  
84      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
85                                    List<MavenProject> reactorProjects )
86          throws ReleaseExecutionException, ReleaseFailureException
87      {
88          ReleaseResult relResult = new ReleaseResult();
89  
90          List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes();
91  
92          if ( additionalExcludes != null )
93          {
94              // SelectorUtils expects OS-specific paths and patterns
95              for ( String additionalExclude : additionalExcludes )
96              {
97                  exclusionPatterns.add( additionalExclude.replace( "\\", File.separator ).replace( "/", File.separator ) );
98              }
99          }
100 
101         logInfo( relResult, "Verifying that there are no local modifications..." );
102         logInfo( relResult, "  ignoring changes on: " + StringUtils.join( exclusionPatterns.toArray(), ", " ) );
103 
104         ScmRepository repository;
105         ScmProvider provider;
106         try
107         {
108             repository =
109                 scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, releaseEnvironment.getSettings() );
110 
111             provider = scmRepositoryConfigurator.getRepositoryProvider( repository );
112         }
113         catch ( ScmRepositoryException e )
114         {
115             throw new ReleaseScmRepositoryException( e.getMessage() + " for URL: "
116                 + releaseDescriptor.getScmSourceUrl(), e.getValidationMessages() );
117         }
118         catch ( NoSuchScmProviderException e )
119         {
120             throw new ReleaseExecutionException( "Unable to configure SCM repository: " + e.getMessage(), e );
121         }
122 
123         StatusScmResult result;
124         try
125         {
126             result =
127                 provider.status( repository, new ScmFileSet( new File( releaseDescriptor.getWorkingDirectory() ) ) );
128         }
129         catch ( ScmException e )
130         {
131             throw new ReleaseExecutionException( "An error occurred during the status check process: " + e.getMessage(),
132                                                  e );
133         }
134 
135         if ( !result.isSuccess() )
136         {
137             throw new ReleaseScmCommandException( "Unable to check for local modifications", result );
138         }
139 
140         List<ScmFile> changedFiles = result.getChangedFiles();
141 
142         if ( !changedFiles.isEmpty() )
143         {
144             ScmTranslator scmTranslator = scmTranslators.get( repository.getProvider() );
145             
146             // TODO: would be nice for SCM status command to do this for me.
147             for ( Iterator<ScmFile> i = changedFiles.iterator(); i.hasNext(); )
148             {
149                 ScmFile f = i.next();
150 
151                 String path;
152                 if ( scmTranslator != null )
153                 {
154                     path = scmTranslator.toRelativePath( f.getPath() );
155                 }
156                 else
157                 {
158                     path = f.getPath();
159                 }
160 
161                 // SelectorUtils expects File.separator, don't standardize!
162                 String fileName = path.replace( "\\", File.separator ).replace( "/", File.separator );
163 
164                 for ( String exclusionPattern : exclusionPatterns )
165                 {
166                     if ( SelectorUtils.matchPath( exclusionPattern, fileName ) )
167                     {
168                         logDebug( relResult, "Ignoring changed file: " + fileName );
169                         i.remove();
170                         break;
171                     }
172                 }
173             }
174         }
175 
176         if ( !changedFiles.isEmpty() )
177         {
178             StringBuilder message = new StringBuilder();
179 
180             for ( ScmFile file : changedFiles )
181             {
182                 message.append( file.toString() );
183                 message.append( "\n" );
184             }
185 
186             throw new ReleaseFailureException( "Cannot prepare the release because you have local modifications : \n"
187                 + message );
188         }
189 
190         relResult.setResultCode( ReleaseResult.SUCCESS );
191 
192         return relResult;
193     }
194 
195     public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
196                                    List<MavenProject> reactorProjects )
197         throws ReleaseExecutionException, ReleaseFailureException
198     {
199         // It makes no modifications, so simulate is the same as execute
200         return execute( releaseDescriptor, releaseEnvironment, reactorProjects );
201     }
202 }