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