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 org.apache.maven.project.MavenProject;
23  import org.apache.maven.shared.release.ReleaseExecutionException;
24  import org.apache.maven.shared.release.ReleaseResult;
25  import org.apache.maven.shared.release.config.ReleaseDescriptor;
26  import org.apache.maven.shared.release.env.ReleaseEnvironment;
27  import org.apache.maven.shared.release.util.PomFinder;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.io.File;
31  import java.util.List;
32  
33  /**
34   * Run the integration tests for the project to verify that it builds before committing.
35   *
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   * @plexus.component role="org.apache.maven.shared.release.phase.ReleasePhase" role-hint="run-perform-goals"
38   */
39  public class RunPerformGoalsPhase
40      extends AbstractRunGoalsPhase
41  {
42      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
43                                    List<MavenProject> reactorProjects )
44          throws ReleaseExecutionException
45      {
46          return runLogic( releaseDescriptor, releaseEnvironment, reactorProjects, false );
47      }
48  
49      private ReleaseResult runLogic( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
50                                    List<MavenProject> reactorProjects, boolean simulate )
51          throws ReleaseExecutionException
52      {
53          String additionalArguments = releaseDescriptor.getAdditionalArguments();
54  
55          if ( releaseDescriptor.isUseReleaseProfile() )
56          {
57              if ( !StringUtils.isEmpty( additionalArguments ) )
58              {
59                  additionalArguments = additionalArguments + " -DperformRelease=true";
60              }
61              else
62              {
63                  additionalArguments = "-DperformRelease=true";
64              }
65          }
66          
67          String pomFileName = releaseDescriptor.getPomFileName();
68          if ( pomFileName == null )
69          {
70              pomFileName = "pom.xml";
71          }
72  
73          // ensure we don't use the release pom for the perform goals
74          // ^^ paranoia? A MavenExecutor has already access to this. Probably worth refactoring. 
75          if ( !StringUtils.isEmpty( additionalArguments ) )
76          {
77              additionalArguments = additionalArguments + " -f " + pomFileName;
78          }
79          else
80          {
81              additionalArguments = "-f " + pomFileName;
82          }
83          
84          if ( simulate )
85          {
86              ReleaseResult result = new ReleaseResult();
87  
88              logDebug( result, "Additional arguments: " + additionalArguments );
89              
90              logInfo( result, "Executing perform goals  - since this is simulation mode these goals are skipped." );
91              
92              return result;
93          }
94  
95          String workDir = releaseDescriptor.getWorkingDirectory();
96          if ( workDir == null )
97          {
98              workDir = System.getProperty( "user.dir" );
99          }
100 
101 
102         File pomFile = new File( workDir, pomFileName );
103         PomFinder pomFinder = new PomFinder( getLogger() );
104         boolean foundPom = false;
105 
106         if ( StringUtils.isEmpty( releaseDescriptor.getScmRelativePathProjectDirectory() ) )
107         {
108             foundPom = pomFinder.parsePom( pomFile );
109         }
110 
111         File workDirectory;
112         if ( simulate )
113         {
114             workDirectory = new File( releaseDescriptor.getWorkingDirectory() );
115         }
116         else
117         {
118             workDirectory = new File( releaseDescriptor.getCheckoutDirectory() );
119         }
120 
121         if ( foundPom )
122         {
123             File matchingPom = pomFinder.findMatchingPom( workDirectory );
124             if ( matchingPom != null )
125             {
126                 getLogger().info( "Invoking perform goals in directory " + matchingPom.getParent() );
127                 // The directory of the POM in a flat project layout is not
128                 // the same directory as the SCM checkout directory!
129                 // The same is true for a sparse checkout in e.g. GIT
130                 // the project to build could be in target/checkout/some/dir/
131                 workDirectory = matchingPom.getParentFile();
132             }
133         }
134 
135         return execute( releaseDescriptor, releaseEnvironment, workDirectory, additionalArguments );
136     }
137 
138     public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
139                                    List<MavenProject> reactorProjects )
140         throws ReleaseExecutionException
141     {
142         return runLogic( releaseDescriptor, releaseEnvironment, reactorProjects, true );
143     }
144 
145     protected String getGoals( ReleaseDescriptor releaseDescriptor )
146     {
147         return releaseDescriptor.getPerformGoals();
148     }
149 }