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.apache.maven.shared.release.util.ReleaseUtil;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  import java.util.List;
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 class CheckPomPhase
44      extends AbstractReleasePhase
45  {
46      
47      /**
48       * @since 2.4
49       */
50      private boolean scmRequired = true;
51      
52      private ScmRepositoryConfigurator scmRepositoryConfigurator;
53  
54      public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment, List<MavenProject> reactorProjects )
55          throws ReleaseExecutionException, ReleaseFailureException
56      {
57          ReleaseResult result = new ReleaseResult();
58  
59          // Currently, we don't deal with multiple SCM locations in a multiproject
60          if ( scmRequired && StringUtils.isEmpty( releaseDescriptor.getScmSourceUrl() ) )
61          {
62              MavenProject rootProject = ReleaseUtil.getRootProject( reactorProjects );
63              if ( rootProject != null && rootProject.getScm() != null )
64              {
65                  if ( rootProject.getScm().getDeveloperConnection() != null )
66                  {
67                      releaseDescriptor.setScmSourceUrl( rootProject.getScm().getDeveloperConnection() );
68                  }
69                  else if ( rootProject.getScm().getConnection() != null )
70                  {
71                      releaseDescriptor.setScmSourceUrl( rootProject.getScm().getConnection() );
72                  }
73              }
74  
75              if ( StringUtils.isEmpty( releaseDescriptor.getScmSourceUrl() ) )
76              {
77                  throw new ReleaseFailureException(
78                      "Missing required setting: scm connection or developerConnection must be specified." );
79              }
80  
81              // As long as Scm.getId() does not exist, read it as a property
82              releaseDescriptor.setScmId( rootProject.getProperties().getProperty( "project.scm.id" ) );
83  
84              try
85              {
86                  scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, releaseEnvironment.getSettings() );
87              }
88              catch ( ScmRepositoryException e )
89              {
90                  throw new ReleaseScmRepositoryException( e.getMessage(), e.getValidationMessages() );
91              }
92              catch ( NoSuchScmProviderException e )
93              {
94                  throw new ReleaseFailureException(
95                      "The provider given in the SCM URL could not be found: " + e.getMessage() );
96              }
97          }
98  
99          boolean containsSnapshotProjects = false;
100 
101         for ( MavenProject project : reactorProjects )
102         {
103             if ( ArtifactUtils.isSnapshot( project.getVersion() ) )
104             {
105                 containsSnapshotProjects = true;
106             }
107         }
108 
109         if ( !containsSnapshotProjects && !releaseDescriptor.isBranchCreation() )
110         {
111             throw new ReleaseFailureException( "You don't have a SNAPSHOT project in the reactor projects list." );
112         }
113 
114         result.setResultCode( ReleaseResult.SUCCESS );
115 
116         return result;
117     }
118 
119     public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment, List<MavenProject> reactorProjects )
120         throws ReleaseExecutionException, ReleaseFailureException
121     {
122         // It makes no modifications, so simulate is the same as execute
123         return execute( releaseDescriptor, releaseEnvironment, reactorProjects );
124     }
125 }