View Javadoc
1   package org.apache.maven.plugins.deploy;
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.execution.MavenSession;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.rtinfo.RuntimeInformation;
29  import org.eclipse.aether.RepositorySystem;
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.deployment.DeployRequest;
32  import org.eclipse.aether.deployment.DeploymentException;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.util.version.GenericVersionScheme;
35  import org.eclipse.aether.version.InvalidVersionSpecificationException;
36  import org.eclipse.aether.version.Version;
37  
38  /**
39   * Abstract class for Deploy mojo's.
40   */
41  public abstract class AbstractDeployMojo
42          extends AbstractMojo
43  {
44      /**
45       * Flag whether Maven is currently in online/offline mode.
46       */
47      @Parameter( defaultValue = "${settings.offline}", readonly = true )
48      private boolean offline;
49  
50      /**
51       * Parameter used to control how many times a failed deployment will be retried before giving up and failing. If a
52       * value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
53       *
54       * @since 2.7
55       */
56      @Parameter( property = "retryFailedDeploymentCount", defaultValue = "1" )
57      private int retryFailedDeploymentCount;
58  
59      @Component
60      private RuntimeInformation runtimeInformation;
61  
62      @Parameter( defaultValue = "${session}", readonly = true, required = true )
63      protected MavenSession session;
64  
65      @Component
66      protected RepositorySystem repositorySystem;
67  
68      private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";
69  
70      private static final String FIXED_MAVEN_VERSION = "3.9.0";
71  
72      /* Setters and Getters */
73  
74      void failIfOffline()
75              throws MojoFailureException
76      {
77          if ( offline )
78          {
79              throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" );
80          }
81      }
82  
83      /**
84       * If this plugin used in pre-3.9.0 Maven, the packaging {@code maven-plugin} will not deploy G level metadata.
85       */
86      protected void warnIfAffectedPackagingAndMaven( final String packaging )
87      {
88          if ( AFFECTED_MAVEN_PACKAGING.equals( packaging ) )
89          {
90              try
91              {
92                  GenericVersionScheme versionScheme = new GenericVersionScheme();
93                  Version fixedMavenVersion = versionScheme.parseVersion( FIXED_MAVEN_VERSION );
94                  Version currentMavenVersion = versionScheme.parseVersion( runtimeInformation.getMavenVersion() );
95                  if ( fixedMavenVersion.compareTo( currentMavenVersion ) > 0 )
96                  {
97                      getLog().warn( "" );
98                      getLog().warn( "You are about to deploy a maven-plugin using Maven " + currentMavenVersion + "." );
99                      getLog().warn( "This plugin should be used ONLY with Maven 3.9.0 and newer, as MNG-7055" );
100                     getLog().warn( "is fixed in those versions of Maven only!" );
101                     getLog().warn( "" );
102                 }
103             }
104             catch ( InvalidVersionSpecificationException e )
105             {
106                 // skip it: Generic does not throw, only API contains this exception
107             }
108         }
109     }
110 
111     /**
112      * Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
113      */
114     protected RemoteRepository getRemoteRepository( final String repositoryId, final String url )
115     {
116         RemoteRepository result = new RemoteRepository.Builder( repositoryId, "default", url ).build();
117 
118         if ( result.getAuthentication() == null || result.getProxy() == null )
119         {
120             RemoteRepository.Builder builder = new RemoteRepository.Builder( result );
121 
122             if ( result.getAuthentication() == null )
123             {
124                 builder.setAuthentication( session.getRepositorySession().getAuthenticationSelector()
125                         .getAuthentication( result ) );
126             }
127 
128             if ( result.getProxy() == null )
129             {
130                 builder.setProxy( session.getRepositorySession().getProxySelector().getProxy( result ) );
131             }
132 
133             result = builder.build();
134         }
135 
136         return result;
137     }
138 
139     /**
140      * Handles high level retries (this was buried into MAT).
141      */
142     protected void deploy( RepositorySystemSession session, DeployRequest deployRequest ) throws MojoExecutionException
143     {
144         int retryFailedDeploymentCounter = Math.max( 1, Math.min( 10, retryFailedDeploymentCount ) );
145         DeploymentException exception = null;
146         for ( int count = 0; count < retryFailedDeploymentCounter; count++ )
147         {
148             try
149             {
150                 if ( count > 0 )
151                 {
152                     getLog().info( "Retrying deployment attempt " + ( count + 1 ) + " of "
153                             + retryFailedDeploymentCounter );
154                 }
155 
156                 repositorySystem.deploy( session, deployRequest );
157                 exception = null;
158                 break;
159             }
160             catch ( DeploymentException e )
161             {
162                 if ( count + 1 < retryFailedDeploymentCounter )
163                 {
164                     getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage() );
165                     getLog().debug( e );
166                 }
167                 if ( exception == null )
168                 {
169                     exception = e;
170                 }
171             }
172         }
173         if ( exception != null )
174         {
175             throw new MojoExecutionException( exception.getMessage(), exception );
176         }
177     }
178 }