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 java.io.File;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.maven.shared.release.ReleaseExecutionException;
27  import org.apache.maven.shared.release.ReleaseResult;
28  import org.apache.maven.shared.release.config.ReleaseDescriptor;
29  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
30  import org.apache.maven.shared.release.env.ReleaseEnvironment;
31  import org.apache.maven.shared.release.exec.MavenExecutor;
32  import org.apache.maven.shared.release.exec.MavenExecutorException;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  /**
36   * Run the integration tests for the project to verify that it builds before committing.
37   *
38   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39   */
40  public abstract class AbstractRunGoalsPhase
41      extends AbstractReleasePhase
42  {
43      /**
44       * Component to assist in executing Maven.
45       *
46       * @plexus.requirement role="org.apache.maven.shared.release.exec.MavenExecutor"
47       */
48      private Map<String, MavenExecutor> mavenExecutors;
49  
50      /**
51       * @deprecated Use {@link AbstractRunGoalsPhase#execute(ReleaseDescriptor, ReleaseEnvironment, File, String)}
52       * instead.
53       */
54      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, File workingDirectory,
55                                    String additionalArguments )
56          throws ReleaseExecutionException
57      {
58          return execute( releaseDescriptor, new DefaultReleaseEnvironment(), workingDirectory, additionalArguments );
59      }
60  
61      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
62                                    File workingDirectory, String additionalArguments )
63          throws ReleaseExecutionException
64      {
65          ReleaseResult result = new ReleaseResult();
66  
67          try
68          {
69              String goals = getGoals( releaseDescriptor );
70              if ( !StringUtils.isEmpty( goals ) )
71              {
72                  logInfo( result, "Executing goals '" + goals + "'..." );
73  
74                  MavenExecutor mavenExecutor = mavenExecutors.get( releaseEnvironment.getMavenExecutorId() );
75  
76                  if ( mavenExecutor == null )
77                  {
78                      throw new ReleaseExecutionException(
79                          "Cannot find Maven executor with id: " + releaseEnvironment.getMavenExecutorId() );
80                  }
81  
82                  File wd =
83                      determineWorkingDirectory( workingDirectory,
84                                                 releaseDescriptor.getScmRelativePathProjectDirectory() );
85                  mavenExecutor.executeGoals( wd, goals, releaseEnvironment, releaseDescriptor.isInteractive(),
86                                              additionalArguments, releaseDescriptor.getPomFileName(), result );
87              }
88          }
89          catch ( MavenExecutorException e )
90          {
91              throw new ReleaseExecutionException( e.getMessage(), e );
92          }
93  
94          result.setResultCode( ReleaseResult.SUCCESS );
95  
96          return result;
97      }
98  
99      /**
100      * @deprecated Use {@link AbstractRunGoalsPhase#setMavenExecutor(String, MavenExecutor)} instead.
101      */
102     public void setMavenExecutor( MavenExecutor mavenExecutor )
103     {
104         setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mavenExecutor );
105     }
106 
107     public void setMavenExecutor( String id, MavenExecutor executor )
108     {
109         if ( mavenExecutors == null )
110         {
111             mavenExecutors = new HashMap<String, MavenExecutor>();
112         }
113 
114         mavenExecutors.put( id, executor );
115     }
116 
117     protected abstract String getGoals( ReleaseDescriptor releaseDescriptor );
118 
119     /**
120      * Determines the path of the working directory. By default, this is the
121      * checkout directory. For some SCMs, the project root directory is not the
122      * checkout directory itself, but a SCM-specific subdirectory.
123      *
124      * @param checkoutDirectory            The checkout directory as java.io.File
125      * @param relativePathProjectDirectory The relative path of the project directory within the checkout
126      *                                     directory or ""
127      * @return The working directory
128      */
129     protected File determineWorkingDirectory( File checkoutDirectory, String relativePathProjectDirectory )
130     {
131         File workingDirectory = checkoutDirectory;
132 
133         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
134         {
135             workingDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
136         }
137 
138         return workingDirectory;
139     }
140 }