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