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 java.io.File;
23  import java.lang.reflect.Method;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.Profile;
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.ReleaseDescriptor;
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   * Base class with shared configuration.
44   *
45   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
46   * @version $Id: AbstractReleaseMojo.java 1620213 2014-08-24 22:16:33Z hboutemy $
47   */
48  public abstract class AbstractReleaseMojo
49      extends AbstractMojo
50  {
51      /**
52       */
53      @Parameter( defaultValue = "${basedir}", readonly = true, required = true )
54      private File basedir;
55  
56      /**
57       */
58      @Parameter( defaultValue = "${settings}", readonly = true, required = true )
59      private Settings settings;
60  
61      /**
62       */
63      @Parameter( defaultValue = "${project}", readonly = true, required = true )
64      protected MavenProject project;
65  
66      /**
67       */
68      @Component
69      protected ReleaseManager releaseManager;
70  
71      /**
72       * Additional arguments to pass to the Maven executions, separated by spaces.
73       */
74      @Parameter( alias = "prepareVerifyArgs", property = "arguments" )
75      private String arguments;
76  
77      /**
78       * The file name of the POM to execute any goals against.
79       */
80      @Parameter( property = "pomFileName" )
81      private String pomFileName;
82  
83      /**
84       */
85      @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
86      private List<MavenProject> reactorProjects;
87  
88      /**
89       * The {@code M2_HOME} parameter to use for forked Maven invocations.
90       *
91       * @since 2.0-beta-8
92       */
93      @Parameter( defaultValue = "${maven.home}" )
94      private File mavenHome;
95  
96      /**
97       * The {@code JAVA_HOME} parameter to use for forked Maven invocations.
98       *
99       * @since 2.0-beta-8
100      */
101     @Parameter( defaultValue = "${java.home}" )
102     private File javaHome;
103 
104     /**
105      * The command-line local repository directory in use for this build (if specified).
106      *
107      * @since 2.0-beta-8
108      */
109     @Parameter ( defaultValue = "${maven.repo.local}" )
110     private File localRepoDirectory;
111 
112     /**
113      * Role hint of the {@link org.apache.maven.shared.release.exec.MavenExecutor} implementation to use.
114      *
115      * @since 2.0-beta-8
116      */
117     @Parameter( defaultValue = "invoker", property = "mavenExecutorId" )
118     private String mavenExecutorId;
119 
120     /**
121      * @since 2.0
122      */
123     @Parameter( defaultValue = "${session}", readonly = true, required = true )
124     protected MavenSession session;
125 
126     /**
127      * Gets the enviroment settings configured for this release.
128      *
129      * @return The release environment, never <code>null</code>.
130      */
131     protected ReleaseEnvironment getReleaseEnvironment()
132     {
133         return new DefaultReleaseEnvironment().setSettings( settings )
134                                               .setJavaHome( javaHome )
135                                               .setMavenHome( mavenHome )
136                                               .setLocalRepositoryDirectory( localRepoDirectory )
137                                               .setMavenExecutorId( mavenExecutorId );
138     }
139 
140     /**
141      * Creates the release descriptor from the various goal parameters.
142      *
143      * @return The release descriptor, never <code>null</code>.
144      */
145     protected ReleaseDescriptor createReleaseDescriptor()
146     {
147         ReleaseDescriptor descriptor = new ReleaseDescriptor();
148 
149         descriptor.setInteractive( settings.isInteractiveMode() );
150 
151         descriptor.setWorkingDirectory( basedir.getAbsolutePath() );
152 
153         descriptor.setPomFileName( pomFileName );
154 
155         List<String> profileIds = getActiveProfileIds();
156         String additionalProfiles = getAdditionalProfiles();
157 
158         String args = this.arguments;
159         if ( !profileIds.isEmpty() || StringUtils.isNotBlank( additionalProfiles ) )
160         {
161             if ( !StringUtils.isEmpty( args ) )
162             {
163                 args += " -P ";
164             }
165             else
166             {
167                 args = "-P ";
168             }
169 
170             for ( Iterator<String> it = profileIds.iterator(); it.hasNext(); )
171             {
172                 args += it.next();
173                 if ( it.hasNext() )
174                 {
175                     args += ",";
176                 }
177             }
178             
179             if ( additionalProfiles != null )
180             {
181                 if ( !profileIds.isEmpty() )
182                 {
183                     args += ",";
184                 }
185                 args += additionalProfiles;
186             }
187         }
188         descriptor.setAdditionalArguments( args );
189 
190         return descriptor;
191     }
192 
193     /**
194      * 
195      * @return a List with profile ids, never {@code null}
196      */
197     @SuppressWarnings( "unchecked" )
198     private List<String> getActiveProfileIds()
199     {
200         List<String> profiles;
201         try
202         {
203             // Try to use M3-methods
204             Method getRequestMethod = this.session.getClass().getMethod( "getRequest" );
205             Object mavenExecutionRequest = getRequestMethod.invoke( this.session );
206             Method getActiveProfilesMethod = mavenExecutionRequest.getClass().getMethod( "getActiveProfiles" );
207             profiles = (List<String>) getActiveProfilesMethod.invoke( mavenExecutionRequest );
208         }
209         catch ( Exception e )
210         {
211             if ( project.getActiveProfiles() == null || project.getActiveProfiles().isEmpty() )
212             {
213                 profiles = Collections.emptyList();
214             }
215             else
216             {
217                 profiles = new ArrayList<String>( project.getActiveProfiles().size() );
218                 for ( Object profile : project.getActiveProfiles() )
219                 {
220                     profiles.add( ( (Profile) profile ).getId() );
221                 }
222             }
223         }
224         return profiles;
225     }
226 
227     /**
228      * Gets the comma separated list of additional profiles for the release build.
229      *
230      * @return additional profiles to enable during release
231      */
232     protected String getAdditionalProfiles()
233     {
234         return null;
235     }
236 
237     /**
238      * Sets the component used to perform release actions.
239      *
240      * @param releaseManager The release manager implementation to use, must not be <code>null</code>.
241      */
242     void setReleaseManager( ReleaseManager releaseManager )
243     {
244         this.releaseManager = releaseManager;
245     }
246 
247     /**
248      * Gets the effective settings for this build.
249      *
250      * @return The effective settings for this build, never <code>null</code>.
251      */
252     Settings getSettings()
253     {
254         return settings;
255     }
256     
257     protected final File getBasedir()
258     {
259         return basedir;
260     }
261 
262     /**
263      * Sets the base directory of the build.
264      *
265      * @param basedir The build's base directory, must not be <code>null</code>.
266      */
267     public void setBasedir( File basedir )
268     {
269         this.basedir = basedir;
270     }
271 
272     /**
273      * Gets the list of projects in the build reactor.
274      *
275      * @return The list of reactor project, never <code>null</code>.
276      */
277     public List<MavenProject> getReactorProjects()
278     {
279         return reactorProjects;
280     }
281 
282     /**
283      * Add additional arguments.
284      *
285      * @param argument The argument to add, must not be <code>null</code>.
286      */
287     protected void addArgument( String argument )
288     {
289         if ( arguments != null )
290         {
291             arguments += " " + argument;
292         }
293         else
294         {
295             arguments = argument;
296         }
297     }
298 
299     /**
300      * This method takes some of the release configuration picked up from the command line system properties and copies
301      * it into the release config object.
302      *
303      * @param config The release configuration to merge the system properties into, must not be <code>null</code>.
304      * @param sysPropertiesConfig The configuration from the system properties to merge in, must not be
305      *            <code>null</code>.
306      */
307     protected void mergeCommandLineConfig( ReleaseDescriptor config, ReleaseDescriptor sysPropertiesConfig )
308     {
309         // If the user specifies versions, these should override the existing versions
310         if ( sysPropertiesConfig.getReleaseVersions() != null )
311         {
312             config.getReleaseVersions().putAll( sysPropertiesConfig.getReleaseVersions() );
313         }
314         if ( sysPropertiesConfig.getDevelopmentVersions() != null )
315         {
316             config.getDevelopmentVersions().putAll( sysPropertiesConfig.getDevelopmentVersions() );
317         }
318     }
319 }