1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39 public abstract class AbstractDeployMojo extends AbstractMojo {
40
41
42
43 @Parameter(defaultValue = "${settings.offline}", readonly = true)
44 private boolean offline;
45
46
47
48
49
50
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
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
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
94 }
95 }
96 }
97
98
99
100
101 protected RemoteRepository getRemoteRepository(final String repositoryId, final String url) {
102
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
126
127
128
129
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 }