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.api.RemoteRepository;
22  import org.apache.maven.api.Session;
23  import org.apache.maven.api.Version;
24  import org.apache.maven.api.di.Inject;
25  import org.apache.maven.api.plugin.Log;
26  import org.apache.maven.api.plugin.Mojo;
27  import org.apache.maven.api.plugin.MojoException;
28  import org.apache.maven.api.plugin.annotations.Parameter;
29  
30  /**
31   * Abstract class for Deploy mojo's.
32   */
33  public abstract class AbstractDeployMojo implements Mojo {
34      private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin";
35  
36      private static final String FIXED_MAVEN_VERSION = "3.9.0";
37  
38      @Inject
39      protected Log logger;
40  
41      @Inject
42      protected Session session;
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      /* Setters and Getters */
60  
61      void failIfOffline() throws MojoException {
62          if (offline) {
63              throw new MojoException("Cannot deploy artifacts when Maven is in offline mode");
64          }
65      }
66  
67      public int getRetryFailedDeploymentCount() {
68          return retryFailedDeploymentCount;
69      }
70  
71      /**
72       * If this plugin used in pre-3.9.0 Maven, the packaging {@code maven-plugin} will not deploy G level metadata.
73       */
74      protected void warnIfAffectedPackagingAndMaven(String packaging) {
75          if (AFFECTED_MAVEN_PACKAGING.equals(packaging)) {
76              Version fixedMavenVersion = session.parseVersion(FIXED_MAVEN_VERSION);
77              Version currentMavenVersion = session.getMavenVersion();
78              if (fixedMavenVersion.compareTo(currentMavenVersion) > 0) {
79                  getLog().warn("");
80                  getLog().warn("You are about to deploy a maven-plugin using Maven " + currentMavenVersion + ".");
81                  getLog().warn("This plugin should be used ONLY with Maven 3.9.0 and newer, as MNG-7055");
82                  getLog().warn("is fixed in those versions of Maven only!");
83                  getLog().warn("");
84              }
85          }
86      }
87  
88      /**
89       * Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
90       */
91      protected RemoteRepository createDeploymentArtifactRepository(String id, String url) {
92          return getSession().createRemoteRepository(id, url);
93      }
94  
95      protected Session getSession() {
96          return session;
97      }
98  
99      protected Log getLog() {
100         return logger;
101     }
102 }