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)} instead.
52       */
53      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, File workingDirectory,
54                                    String additionalArguments )
55          throws ReleaseExecutionException
56      {
57          return execute( releaseDescriptor, new DefaultReleaseEnvironment(), workingDirectory, additionalArguments );
58      }
59  
60      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
61                                    File workingDirectory, String additionalArguments )
62          throws ReleaseExecutionException
63      {
64          ReleaseResult result = new ReleaseResult();
65  
66          try
67          {
68              String goals = getGoals( releaseDescriptor );
69              if ( !StringUtils.isEmpty( goals ) )
70              {
71                  logInfo( result, "Executing goals '" + goals + "'..." );
72  
73                  MavenExecutor mavenExecutor = mavenExecutors.get( releaseEnvironment.getMavenExecutorId() );
74  
75                  if ( mavenExecutor == null )
76                  {
77                      throw new ReleaseExecutionException(
78                          "Cannot find Maven executor with id: " + releaseEnvironment.getMavenExecutorId() );
79                  }
80  
81                  mavenExecutor.executeGoals( determineWorkingDirectory( workingDirectory,
82                                                                         releaseDescriptor.getScmRelativePathProjectDirectory() ),
83                                              goals, releaseEnvironment, releaseDescriptor.isInteractive(),
84                                              additionalArguments, releaseDescriptor.getPomFileName(), result );
85              }
86          }
87          catch ( MavenExecutorException e )
88          {
89              throw new ReleaseExecutionException( e.getMessage(), e );
90          }
91  
92          result.setResultCode( ReleaseResult.SUCCESS );
93  
94          return result;
95      }
96  
97      /**
98       * @deprecated Use {@link AbstractRunGoalsPhase#setMavenExecutor(String, MavenExecutor)} instead.
99       */
100     public void setMavenExecutor( MavenExecutor mavenExecutor )
101     {
102         setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mavenExecutor );
103     }
104 
105     public void setMavenExecutor( String id, MavenExecutor executor )
106     {
107         if ( mavenExecutors == null )
108         {
109             mavenExecutors = new HashMap<String, MavenExecutor>();
110         }
111 
112         mavenExecutors.put( id, executor );
113     }
114 
115     protected abstract String getGoals( ReleaseDescriptor releaseDescriptor );
116 
117     /**
118      * Determines the path of the working directory. By default, this is the
119      * checkout directory. For some SCMs, the project root directory is not the
120      * checkout directory itself, but a SCM-specific subdirectory.
121      *
122      * @param checkoutDirectory            The checkout directory as java.io.File
123      * @param relativePathProjectDirectory The relative path of the project directory within the checkout
124      *                                     directory or ""
125      * @return The working directory
126      */
127     protected File determineWorkingDirectory( File checkoutDirectory, String relativePathProjectDirectory )
128     {
129         File workingDirectory = checkoutDirectory;
130 
131         if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
132         {
133             workingDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
134         }
135 
136         return workingDirectory;
137     }
138 }