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