1 package org.apache.maven.plugins.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41 public abstract class AbstractDeployMojo
42 extends AbstractMojo
43 {
44
45
46
47 @Parameter( defaultValue = "${settings.offline}", readonly = true )
48 private boolean offline;
49
50
51
52
53
54
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
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
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
107 }
108 }
109 }
110
111
112
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
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 }