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 java.util.List;
22  
23  import org.apache.maven.artifact.ArtifactUtils;
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.scm.manager.NoSuchScmProviderException;
26  import org.apache.maven.scm.repository.ScmRepositoryException;
27  import org.apache.maven.shared.release.ReleaseExecutionException;
28  import org.apache.maven.shared.release.ReleaseFailureException;
29  import org.apache.maven.shared.release.ReleaseResult;
30  import org.apache.maven.shared.release.config.ReleaseDescriptor;
31  import org.apache.maven.shared.release.env.ReleaseEnvironment;
32  import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
33  import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
34  import org.apache.maven.shared.release.util.MavenExpression;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  import static java.util.Objects.requireNonNull;
38  
39  /**
40   * Phase that checks the validity of the POM before release.
41   *
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   */
44  public abstract class AbstractCheckPomPhase extends AbstractReleasePhase {
45  
46      private final ScmRepositoryConfigurator scmRepositoryConfigurator;
47  
48      /**
49       * @since 2.4
50       */
51      private final boolean scmRequired;
52  
53      /**
54       * @since 2.5.2
55       */
56      private final boolean snapshotsRequired;
57  
58      public AbstractCheckPomPhase(
59              ScmRepositoryConfigurator scmRepositoryConfigurator, boolean scmRequired, boolean snapshotsRequired) {
60          this.scmRepositoryConfigurator = requireNonNull(scmRepositoryConfigurator);
61          this.scmRequired = scmRequired;
62          this.snapshotsRequired = snapshotsRequired;
63      }
64  
65      @Override
66      public ReleaseResult execute(
67              ReleaseDescriptor releaseDescriptor,
68              ReleaseEnvironment releaseEnvironment,
69              List<MavenProject> reactorProjects)
70              throws ReleaseExecutionException, ReleaseFailureException {
71          ReleaseResult result = new ReleaseResult();
72  
73          // Currently, we don't deal with multiple SCM locations in a multiproject
74          if (scmRequired) {
75              if (StringUtils.isEmpty(releaseDescriptor.getScmSourceUrl())) {
76                  throw new ReleaseFailureException(
77                          "Missing required setting: scm connection or developerConnection must be specified.");
78              }
79  
80              try {
81                  scmRepositoryConfigurator.getConfiguredRepository(releaseDescriptor, releaseEnvironment.getSettings());
82              } catch (ScmRepositoryException e) {
83                  throw new ReleaseScmRepositoryException(e.getMessage(), e.getValidationMessages());
84              } catch (NoSuchScmProviderException e) {
85                  throw new ReleaseFailureException(
86                          "The provider given in the SCM URL could not be found: " + e.getMessage());
87              }
88          }
89  
90          boolean containsSnapshotProjects = false;
91  
92          for (MavenProject project : reactorProjects) {
93              String projectVersion = MavenExpression.evaluate(project.getVersion(), project.getProperties());
94              if (ArtifactUtils.isSnapshot(projectVersion)) {
95                  containsSnapshotProjects = true;
96  
97                  break;
98              }
99          }
100 
101         if (snapshotsRequired && !containsSnapshotProjects && !releaseDescriptor.isBranchCreation()) {
102             throw new ReleaseFailureException("You don't have a SNAPSHOT project in the reactor projects list.");
103         }
104 
105         result.setResultCode(ReleaseResult.SUCCESS);
106 
107         return result;
108     }
109 
110     @Override
111     public ReleaseResult simulate(
112             ReleaseDescriptor releaseDescriptor,
113             ReleaseEnvironment releaseEnvironment,
114             List<MavenProject> reactorProjects)
115             throws ReleaseExecutionException, ReleaseFailureException {
116         // It makes no modifications, so simulate is the same as execute
117         return execute(releaseDescriptor, releaseEnvironment, reactorProjects);
118     }
119 }