View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.phase;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.shared.release.ReleaseExecutionException;
31  import org.apache.maven.shared.release.ReleaseResult;
32  import org.apache.maven.shared.release.config.ReleaseDescriptor;
33  import org.apache.maven.shared.release.env.ReleaseEnvironment;
34  import org.apache.maven.shared.release.exec.MavenExecutor;
35  import org.apache.maven.shared.release.util.PomFinder;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
39  
40  /**
41   * Run the effective release build of the project and its deploy to remote repository.
42   *
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   */
45  @Singleton
46  @Named("run-perform-goals")
47  public class RunPerformGoalsPhase extends AbstractRunGoalsPhase {
48      @Inject
49      public RunPerformGoalsPhase(Map<String, MavenExecutor> mavenExecutors) {
50          super(mavenExecutors);
51      }
52  
53      @Override
54      public ReleaseResult execute(
55              ReleaseDescriptor releaseDescriptor,
56              ReleaseEnvironment releaseEnvironment,
57              List<MavenProject> reactorProjects)
58              throws ReleaseExecutionException {
59          return runLogic(releaseDescriptor, releaseEnvironment, false);
60      }
61  
62      private ReleaseResult runLogic(
63              ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment, boolean simulate)
64              throws ReleaseExecutionException {
65          String additionalArguments = getAdditionalArguments(releaseDescriptor);
66  
67          if (releaseDescriptor.isUseReleaseProfile()) {
68              if (!(additionalArguments == null || additionalArguments.isEmpty())) {
69                  additionalArguments = additionalArguments + " -DperformRelease=true";
70              } else {
71                  additionalArguments = "-DperformRelease=true";
72              }
73          }
74  
75          String pomFileName = releaseDescriptor.getPomFileName();
76          if (pomFileName == null) {
77              pomFileName = "pom.xml";
78          }
79  
80          // ensure we don't use the release pom for the perform goals
81          // ^^ paranoia? A MavenExecutor has already access to this. Probably worth refactoring.
82          if (!(additionalArguments == null || additionalArguments.isEmpty())) {
83              additionalArguments = additionalArguments + " -f " + pomFileName;
84          } else {
85              additionalArguments = "-f " + pomFileName;
86          }
87  
88          if (simulate) {
89              ReleaseResult result = new ReleaseResult();
90  
91              logInfo(
92                      result,
93                      "Simulating perform goals '" + buffer().strong(getGoals(releaseDescriptor))
94                              + "' - since this is simulation mode these goals are skipped.");
95              logInfo(result, "    with additional arguments: " + additionalArguments);
96  
97              return result;
98          }
99  
100         String workDir = releaseDescriptor.getWorkingDirectory();
101         if (workDir == null) {
102             workDir = System.getProperty("user.dir");
103         }
104 
105         File pomFile = new File(workDir, pomFileName);
106         PomFinder pomFinder = new PomFinder(getLogger());
107         boolean foundPom = false;
108 
109         if (StringUtils.isEmpty(releaseDescriptor.getScmRelativePathProjectDirectory())) {
110             foundPom = pomFinder.parsePom(pomFile);
111         }
112 
113         File workDirectory = new File(releaseDescriptor.getCheckoutDirectory());
114 
115         if (foundPom) {
116             File matchingPom = pomFinder.findMatchingPom(workDirectory);
117             if (matchingPom != null) {
118                 getLogger().info("Invoking perform goals in directory " + matchingPom.getParent());
119                 // The directory of the POM in a flat project layout is not
120                 // the same directory as the SCM checkout directory!
121                 // The same is true for a sparse checkout in e.g. GIT
122                 // the project to build could be in target/checkout/some/dir/
123                 workDirectory = matchingPom.getParentFile();
124             }
125         }
126 
127         return execute(releaseDescriptor, releaseEnvironment, workDirectory, additionalArguments, false);
128     }
129 
130     @Override
131     public ReleaseResult simulate(
132             ReleaseDescriptor releaseDescriptor,
133             ReleaseEnvironment releaseEnvironment,
134             List<MavenProject> reactorProjects)
135             throws ReleaseExecutionException {
136         return runLogic(releaseDescriptor, releaseEnvironment, true);
137     }
138 
139     @Override
140     protected String getGoals(ReleaseDescriptor releaseDescriptor) {
141         return releaseDescriptor.getPerformGoals();
142     }
143 }