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         // TODO: RepositorySystem#newDeploymentRepository does this very same thing!
103         RemoteRepository result = new RemoteRepository.Builder(repositoryId, "default", url).build();
104 
105         if (result.getAuthentication() == null || result.getProxy() == null) {
106             RemoteRepository.Builder builder = new RemoteRepository.Builder(result);
107 
108             if (result.getAuthentication() == null) {
109                 builder.setAuthentication(session.getRepositorySession()
110                         .getAuthenticationSelector()
111                         .getAuthentication(result));
112             }
113 
114             if (result.getProxy() == null) {
115                 builder.setProxy(
116                         session.getRepositorySession().getProxySelector().getProxy(result));
117             }
118 
119             result = builder.build();
120         }
121 
122         return result;
123     }
124 
125     // I'm not sure if retries will work with deploying on client level ...
126     // Most repository managers block a duplicate artifacts.
127 
128     // Eg, when we have an artifact list, even simple pom and jar in one request with released version,
129     // next try can fail due to duplicate.
130 
131     protected void deploy(DeployRequest deployRequest) throws MojoExecutionException {
132         int retryFailedDeploymentCounter = Math.max(1, Math.min(10, retryFailedDeploymentCount));
133         DeploymentException exception = null;
134         for (int count = 0; count < retryFailedDeploymentCounter; count++) {
135             try {
136                 if (count > 0) {
137                     getLog().info("Retrying deployment attempt " + (count + 1) + " of " + retryFailedDeploymentCounter);
138                 }
139 
140                 repositorySystem.deploy(session.getRepositorySession(), deployRequest);
141                 exception = null;
142                 break;
143             } catch (DeploymentException e) {
144                 if (count + 1 < retryFailedDeploymentCounter) {
145                     getLog().warn("Encountered issue during deployment: " + e.getLocalizedMessage());
146                     getLog().debug(e);
147                 }
148                 if (exception == null) {
149                     exception = e;
150                 }
151             }
152         }
153         if (exception != null) {
154             throw new MojoExecutionException(exception.getMessage(), exception);
155         }
156     }
157 }