1 package org.apache.maven.plugins.release;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.apache.maven.artifact.ArtifactUtils;
30 import org.apache.maven.execution.MavenSession;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.settings.Settings;
36 import org.apache.maven.shared.release.ReleaseManager;
37 import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
38 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
39 import org.apache.maven.shared.release.env.ReleaseEnvironment;
40 import org.codehaus.plexus.util.StringUtils;
41
42
43
44
45
46
47 public abstract class AbstractReleaseMojo
48 extends AbstractMojo
49 {
50
51
52 @Parameter( defaultValue = "${basedir}", readonly = true, required = true )
53 private File basedir;
54
55
56
57 @Parameter( defaultValue = "${settings}", readonly = true, required = true )
58 private Settings settings;
59
60
61
62 @Parameter( defaultValue = "${project}", readonly = true, required = true )
63 protected MavenProject project;
64
65
66
67 @Component
68 protected ReleaseManager releaseManager;
69
70
71
72
73 @Parameter( alias = "prepareVerifyArgs", property = "arguments" )
74 private String arguments;
75
76
77
78
79
80 @Parameter( property = "pomFileName", defaultValue = "${project.file.name}" )
81 private String pomFileName;
82
83
84
85 @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
86 private List<MavenProject> reactorProjects;
87
88
89
90
91
92
93 @Parameter( defaultValue = "${maven.home}" )
94 private File mavenHome;
95
96
97
98
99
100
101 @Parameter( defaultValue = "${java.home}" )
102 private File javaHome;
103
104
105
106
107
108
109 @Parameter ( defaultValue = "${maven.repo.local}" )
110 private File localRepoDirectory;
111
112
113
114
115
116
117 @Parameter( defaultValue = "invoker", property = "mavenExecutorId" )
118 private String mavenExecutorId;
119
120
121
122
123 @Parameter( defaultValue = "${session}", readonly = true, required = true )
124 protected MavenSession session;
125
126
127
128
129
130
131
132
133 @Parameter( defaultValue = "default", property = "releaseStrategyId" )
134 private String releaseStrategyId;
135
136
137
138
139
140
141 protected ReleaseEnvironment getReleaseEnvironment()
142 {
143 return new DefaultReleaseEnvironment().setSettings( settings )
144 .setJavaHome( javaHome )
145 .setMavenHome( mavenHome )
146 .setLocalRepositoryDirectory( localRepoDirectory )
147 .setMavenExecutorId( mavenExecutorId );
148 }
149
150
151
152
153
154
155 protected ReleaseDescriptorBuilder createReleaseDescriptor()
156 {
157 ReleaseDescriptorBuilder descriptor = new ReleaseDescriptorBuilder();
158
159 descriptor.setInteractive( settings.isInteractiveMode() );
160
161 Path workingDirectory;
162 try
163 {
164 workingDirectory = getCommonBasedir( reactorProjects );
165 }
166 catch ( IOException e )
167 {
168 throw new RuntimeException( e.getMessage() );
169 }
170 descriptor.setWorkingDirectory( workingDirectory.toFile().getAbsolutePath() );
171
172 Path rootBasedir = basedir.toPath();
173 if ( rootBasedir.equals( workingDirectory ) )
174 {
175 descriptor.setPomFileName( pomFileName );
176 }
177 else
178 {
179 descriptor.setPomFileName( workingDirectory.relativize( rootBasedir ).resolve( pomFileName ).toString() );
180 }
181
182 for ( MavenProject project : reactorProjects )
183 {
184 String versionlessKey = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
185 descriptor.putOriginalVersion( versionlessKey, project.getVersion() );
186 }
187
188 descriptor.setAdditionalArguments( this.arguments );
189
190 List<String> profileIds = session.getRequest().getActiveProfiles();
191 String additionalProfiles = getAdditionalProfiles();
192
193 if ( !profileIds.isEmpty() || StringUtils.isNotBlank( additionalProfiles ) )
194 {
195 List<String> profiles = new ArrayList<>( profileIds );
196
197 if ( additionalProfiles != null )
198 {
199 profiles.addAll( Arrays.asList( additionalProfiles.split( "," ) ) );
200 }
201
202 descriptor.setActivateProfiles( profiles );
203 }
204
205 descriptor.setReleaseStrategyId( releaseStrategyId );
206
207 return descriptor;
208 }
209
210
211
212
213
214
215 protected String getAdditionalProfiles()
216 {
217 return null;
218 }
219
220
221
222
223
224
225 void setReleaseManager( ReleaseManager releaseManager )
226 {
227 this.releaseManager = releaseManager;
228 }
229
230
231
232
233
234
235 Settings getSettings()
236 {
237 return settings;
238 }
239
240 protected final File getBasedir()
241 {
242 return basedir;
243 }
244
245
246
247
248
249
250 public void setBasedir( File basedir )
251 {
252 this.basedir = basedir;
253 }
254
255 public void setPomFileName( String pomFileName )
256 {
257 this.pomFileName = pomFileName;
258 }
259
260
261
262
263
264
265 public List<MavenProject> getReactorProjects()
266 {
267 return reactorProjects;
268 }
269
270
271
272
273
274
275 protected void addArgument( String argument )
276 {
277 if ( arguments != null )
278 {
279 arguments += " " + argument;
280 }
281 else
282 {
283 arguments = argument;
284 }
285 }
286
287 static Path getCommonBasedir( List<MavenProject> reactorProjects )
288 throws IOException
289 {
290 Path basePath = reactorProjects.get( 0 ).getBasedir().toPath();
291
292 for ( MavenProject reactorProject : reactorProjects )
293 {
294 Path matchPath = reactorProject.getBasedir().toPath();
295 while ( !basePath.startsWith( matchPath ) )
296 {
297 matchPath = matchPath.getParent();
298 }
299 basePath = matchPath;
300 }
301
302 return basePath;
303 }
304 }