1 | |
package org.apache.maven.shared.release.phase; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 80 | public class ScmCheckModificationsPhase |
58 | |
extends AbstractReleasePhase |
59 | |
{ |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private ScmRepositoryConfigurator scmRepositoryConfigurator; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private Map<String, ScmTranslator> scmTranslators; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 80 | private Set<String> exclusionPatterns = new HashSet<String>( Arrays.asList( new String[] { |
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 | 44 | ReleaseResult relResult = new ReleaseResult(); |
89 | |
|
90 | 44 | List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes(); |
91 | |
|
92 | 44 | if ( additionalExcludes != null ) |
93 | |
{ |
94 | |
|
95 | 44 | for ( String additionalExclude : additionalExcludes ) |
96 | |
{ |
97 | 8 | exclusionPatterns.add( additionalExclude.replace( "\\", File.separator ).replace( "/", File.separator ) ); |
98 | |
} |
99 | |
} |
100 | |
|
101 | 44 | logInfo( relResult, "Verifying that there are no local modifications..." ); |
102 | 44 | logInfo( relResult, " ignoring changes on: " + StringUtils.join( exclusionPatterns.toArray(), ", " ) ); |
103 | |
|
104 | |
ScmRepository repository; |
105 | |
ScmProvider provider; |
106 | |
try |
107 | |
{ |
108 | 44 | repository = |
109 | |
scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, releaseEnvironment.getSettings() ); |
110 | |
|
111 | 36 | provider = scmRepositoryConfigurator.getRepositoryProvider( repository ); |
112 | |
} |
113 | 4 | catch ( ScmRepositoryException e ) |
114 | |
{ |
115 | 4 | throw new ReleaseScmRepositoryException( e.getMessage() + " for URL: " |
116 | |
+ releaseDescriptor.getScmSourceUrl(), e.getValidationMessages() ); |
117 | |
} |
118 | 4 | catch ( NoSuchScmProviderException e ) |
119 | |
{ |
120 | 4 | throw new ReleaseExecutionException( "Unable to configure SCM repository: " + e.getMessage(), e ); |
121 | 36 | } |
122 | |
|
123 | |
StatusScmResult result; |
124 | |
try |
125 | |
{ |
126 | 36 | result = |
127 | |
provider.status( repository, new ScmFileSet( new File( releaseDescriptor.getWorkingDirectory() ) ) ); |
128 | |
} |
129 | 4 | catch ( ScmException e ) |
130 | |
{ |
131 | 4 | throw new ReleaseExecutionException( "An error occurred during the status check process: " + e.getMessage(), |
132 | |
e ); |
133 | 32 | } |
134 | |
|
135 | 32 | if ( !result.isSuccess() ) |
136 | |
{ |
137 | 4 | throw new ReleaseScmCommandException( "Unable to check for local modifications", result ); |
138 | |
} |
139 | |
|
140 | 28 | List<ScmFile> changedFiles = result.getChangedFiles(); |
141 | |
|
142 | 28 | if ( !changedFiles.isEmpty() ) |
143 | |
{ |
144 | 18 | ScmTranslator scmTranslator = scmTranslators.get( repository.getProvider() ); |
145 | |
|
146 | |
|
147 | 18 | for ( Iterator<ScmFile> i = changedFiles.iterator(); i.hasNext(); ) |
148 | |
{ |
149 | 50 | ScmFile f = i.next(); |
150 | |
|
151 | |
String path; |
152 | 50 | if ( scmTranslator != null ) |
153 | |
{ |
154 | 0 | path = scmTranslator.toRelativePath( f.getPath() ); |
155 | |
} |
156 | |
else |
157 | |
{ |
158 | 50 | path = f.getPath(); |
159 | |
} |
160 | |
|
161 | |
|
162 | 50 | String fileName = path.replace( "\\", File.separator ).replace( "/", File.separator ); |
163 | |
|
164 | 50 | for ( String exclusionPattern : exclusionPatterns ) |
165 | |
{ |
166 | 316 | if ( SelectorUtils.matchPath( exclusionPattern, fileName ) ) |
167 | |
{ |
168 | 30 | logDebug( relResult, "Ignoring changed file: " + fileName ); |
169 | 30 | i.remove(); |
170 | |
} |
171 | |
} |
172 | 50 | } |
173 | |
} |
174 | |
|
175 | 28 | if ( !changedFiles.isEmpty() ) |
176 | |
{ |
177 | 12 | StringBuilder message = new StringBuilder(); |
178 | |
|
179 | 12 | for ( ScmFile file : changedFiles ) |
180 | |
{ |
181 | 20 | message.append( file.toString() ); |
182 | 20 | message.append( "\n" ); |
183 | |
} |
184 | |
|
185 | 12 | throw new ReleaseFailureException( "Cannot prepare the release because you have local modifications : \n" |
186 | |
+ message ); |
187 | |
} |
188 | |
|
189 | 16 | relResult.setResultCode( ReleaseResult.SUCCESS ); |
190 | |
|
191 | 16 | return relResult; |
192 | |
} |
193 | |
|
194 | |
public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment, |
195 | |
List<MavenProject> reactorProjects ) |
196 | |
throws ReleaseExecutionException, ReleaseFailureException |
197 | |
{ |
198 | |
|
199 | 22 | return execute( releaseDescriptor, releaseEnvironment, reactorProjects ); |
200 | |
} |
201 | |
} |