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