View Javadoc

1   package org.apache.maven.plugins.release;
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.model.Profile;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.scm.manager.ScmManager;
28  import org.apache.maven.settings.Settings;
29  import org.apache.maven.shared.release.ReleaseManager;
30  import org.apache.maven.shared.release.config.ReleaseDescriptor;
31  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
32  import org.apache.maven.shared.release.env.ReleaseEnvironment;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.io.File;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * Base class with shared configuration.
42   * 
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   * @version $Id: AbstractReleaseMojo.java 707476 2008-10-23 21:12:30Z bentmann $
45   */
46  public abstract class AbstractReleaseMojo
47      extends AbstractMojo
48  {
49      /**
50       * The SCM username to use.
51       * 
52       * @parameter expression="${username}"
53       */
54      private String username;
55  
56      /**
57       * The SCM password to use.
58       * 
59       * @parameter expression="${password}"
60       */
61      private String password;
62  
63      /**
64       * The SCM tag to use.
65       * 
66       * @parameter expression="${tag}" alias="releaseLabel"
67       */
68      private String tag;
69  
70      /**
71       * The tag base directory in SVN, you must define it if you don't use the standard svn layout (trunk/tags/branches).
72       * For example, <code>http://svn.apache.org/repos/asf/maven/plugins/tags</code>. The URL is an SVN URL and does not
73       * include the SCM provider and protocol.
74       * 
75       * @parameter expression="${tagBase}"
76       */
77      private String tagBase;
78  
79      /**
80       * @parameter expression="${basedir}"
81       * @required
82       * @readonly
83       */
84      protected File basedir;
85  
86      /**
87       * @parameter expression="${settings}"
88       * @required
89       * @readonly
90       */
91      protected Settings settings;
92  
93      /**
94       * @parameter expression="${project}"
95       * @required
96       * @readonly
97       */
98      protected MavenProject project;
99  
100     /**
101      * @component
102      */
103     protected ReleaseManager releaseManager;
104 
105     /**
106      * Additional arguments to pass to the Maven executions, separated by spaces.
107      * 
108      * @parameter expression="${arguments}" alias="prepareVerifyArgs"
109      */
110     private String arguments;
111 
112     /**
113      * The file name of the POM to execute any goals against.
114      * 
115      * @parameter expression="${pomFileName}"
116      */
117     private String pomFileName;
118 
119     /**
120      * The message prefix to use for all SCM changes.
121      * 
122      * @parameter expression="${scmCommentPrefix}" default-value="[maven-release-plugin] "
123      */
124     private String scmCommentPrefix;
125 
126     /**
127      * @parameter expression="${reactorProjects}"
128      * @required
129      * @readonly
130      */
131     protected List reactorProjects;
132 
133     /**
134      * List of provider implementations.
135      * 
136      * @parameter
137      */
138     private Map providerImplementations;
139 
140     /**
141      * The M2_HOME parameter to use for forked Maven invocations.
142      * 
143      * @parameter default-value="${maven.home}"
144      */
145     protected File mavenHome;
146 
147     /**
148      * The JAVA_HOME parameter to use for forked Maven invocations.
149      * 
150      * @parameter default-value="${java.home}"
151      */
152     protected File javaHome;
153 
154     /**
155      * The command-line local repository directory in use for this build (if specified).
156      * 
157      * @parameter default-value="${maven.repo.local}"
158      */
159     protected File localRepoDirectory;
160 
161     /**
162      * Role hint of the {@link org.apache.maven.shared.release.exec.MavenExecutor} implementation to use.
163      * 
164      * @parameter expression="${mavenExecutorId}" default-value="invoker"
165      */
166     protected String mavenExecutorId;
167 
168     /**
169      * The SCM manager.
170      * 
171      * @component
172      */
173     private ScmManager scmManager;
174 
175     /**
176      * Gets the enviroment settings configured for this release.
177      * 
178      * @return The release environment, never <code>null</code>.
179      */
180     protected ReleaseEnvironment getReleaseEnvironment()
181     {
182         return new DefaultReleaseEnvironment().setSettings( settings )
183                                               .setJavaHome( javaHome )
184                                               .setMavenHome( mavenHome )
185                                               .setLocalRepositoryDirectory( localRepoDirectory )
186                                               .setMavenExecutorId( mavenExecutorId );
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     public void execute()
193         throws MojoExecutionException, MojoFailureException
194     {
195         if ( providerImplementations != null )
196         {
197             for ( Iterator i = providerImplementations.keySet().iterator(); i.hasNext(); )
198             {
199                 String providerType = (String) i.next();
200                 String providerImplementation = (String) providerImplementations.get( providerType );
201                 getLog().info( "Change the default '" + providerType + "' provider implementation to '"
202                     + providerImplementation + "'." );
203                 scmManager.setScmProviderImplementation( providerType, providerImplementation );
204             }
205         }
206     }
207 
208     /**
209      * Creates the release descriptor from the various goal parameters.
210      * 
211      * @return The release descriptor, never <code>null</code>.
212      */
213     protected ReleaseDescriptor createReleaseDescriptor()
214     {
215         ReleaseDescriptor descriptor = new ReleaseDescriptor();
216 
217         descriptor.setInteractive( settings.isInteractiveMode() );
218 
219         descriptor.setScmPassword( password );
220         descriptor.setScmReleaseLabel( tag );
221         descriptor.setScmTagBase( tagBase );
222         descriptor.setScmUsername( username );
223         descriptor.setScmCommentPrefix( scmCommentPrefix );
224 
225         descriptor.setWorkingDirectory( basedir.getAbsolutePath() );
226 
227         descriptor.setPomFileName( pomFileName );
228 
229         List profiles = project.getActiveProfiles();
230 
231         String arguments = this.arguments;
232         if ( profiles != null && !profiles.isEmpty() )
233         {
234             if ( !StringUtils.isEmpty( arguments ) )
235             {
236                 arguments += " -P ";
237             }
238             else
239             {
240                 arguments = "-P ";
241             }
242 
243             for ( Iterator it = profiles.iterator(); it.hasNext(); )
244             {
245                 Profile profile = (Profile) it.next();
246 
247                 arguments += profile.getId();
248                 if ( it.hasNext() )
249                 {
250                     arguments += ",";
251                 }
252             }
253 
254             String additionalProfiles = getAdditionalProfiles();
255             if ( additionalProfiles != null )
256             {
257                 if ( !profiles.isEmpty() )
258                 {
259                     arguments += ",";
260                 }
261                 arguments += additionalProfiles;
262             }
263         }
264         descriptor.setAdditionalArguments( arguments );
265 
266         return descriptor;
267     }
268 
269     /**
270      * Gets the comma separated list of additional profiles for the release build.
271      * 
272      * @return additional profiles to enable during release
273      */
274     protected String getAdditionalProfiles()
275     {
276         return null;
277     }
278 
279     /**
280      * Sets the component used to perform release actions.
281      * 
282      * @param releaseManager The release manager implementation to use, must not be <code>null</code>.
283      */
284     void setReleaseManager( ReleaseManager releaseManager )
285     {
286         this.releaseManager = releaseManager;
287     }
288 
289     /**
290      * Gets the effective settings for this build.
291      * 
292      * @return The effective settings for this build, never <code>null</code>.
293      */
294     Settings getSettings()
295     {
296         return settings;
297     }
298 
299     /**
300      * Sets the base directory of the build.
301      * 
302      * @param basedir The build's base directory, must not be <code>null</code>.
303      */
304     public void setBasedir( File basedir )
305     {
306         this.basedir = basedir;
307     }
308 
309     /**
310      * Gets the list of projects in the build reactor.
311      * 
312      * @return The list of reactor project, never <code>null</code>.
313      */
314     public List getReactorProjects()
315     {
316         return reactorProjects;
317     }
318 
319     /**
320      * Add additional arguments.
321      * 
322      * @param argument The argument to add, must not be <code>null</code>.
323      */
324     protected void addArgument( String argument )
325     {
326         if ( arguments != null )
327         {
328             arguments += " " + argument;
329         }
330         else
331         {
332             arguments = argument;
333         }
334     }
335 }