19
19
* under the License.
20
20
*/
21
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 ;
22
+ import org .apache .maven .api .RemoteRepository ;
23
+ import org .apache .maven .api .Version ;
24
+ import org .apache .maven .api .plugin .Mojo ;
25
+ import org .apache .maven .api .Session ;
26
+ import org .apache .maven .api .services .RepositoryFactory ;
27
+ import org .apache .maven .api .plugin .MojoException ;
28
+ import org .apache .maven .api .plugin .annotations .Parameter ;
29
+ import org .apache .maven .api .services .VersionParser ;
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
37
32
38
33
/**
39
34
* Abstract class for Deploy mojo's.
40
35
*/
41
36
public abstract class AbstractDeployMojo
42
- extends AbstractMojo
37
+ implements Mojo
43
38
{
39
+ private static final String AFFECTED_MAVEN_PACKAGING = "maven-plugin" ;
40
+
41
+ private static final String FIXED_MAVEN_VERSION = "3.9.0" ;
42
+
43
+ protected final Logger logger = LoggerFactory .getLogger ( getClass () );
44
+
44
45
/**
45
46
* Flag whether Maven is currently in online/offline mode.
46
47
*/
@@ -56,123 +57,64 @@ public abstract class AbstractDeployMojo
56
57
@ Parameter ( property = "retryFailedDeploymentCount" , defaultValue = "1" )
57
58
private int retryFailedDeploymentCount ;
58
59
59
- @ Component
60
- private RuntimeInformation runtimeInformation ;
61
-
62
60
@ 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" ;
61
+ protected Session session ;
71
62
72
63
/* Setters and Getters */
73
64
74
65
void failIfOffline ()
75
- throws MojoFailureException
66
+ throws MojoException
76
67
{
77
68
if ( offline )
78
69
{
79
- throw new MojoFailureException ( "Cannot deploy artifacts when Maven is in offline mode" );
70
+ throw new MojoException ( "Cannot deploy artifacts when Maven is in offline mode" );
80
71
}
81
72
}
82
73
74
+ public int getRetryFailedDeploymentCount ()
75
+ {
76
+ return retryFailedDeploymentCount ;
77
+ }
78
+
83
79
/**
84
80
* If this plugin used in pre-3.9.0 Maven, the packaging {@code maven-plugin} will not deploy G level metadata.
85
81
*/
86
82
protected void warnIfAffectedPackagingAndMaven ( final String packaging )
87
83
{
88
84
if ( AFFECTED_MAVEN_PACKAGING .equals ( packaging ) )
89
85
{
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 )
86
+ VersionParser parser = session .getService ( VersionParser .class );
87
+ Version fixedMavenVersion = parser .parseVersion ( FIXED_MAVEN_VERSION );
88
+ Version currentMavenVersion = parser .parseVersion ( session .getMavenVersion () );
89
+ if ( fixedMavenVersion .compareTo ( currentMavenVersion ) > 0 )
105
90
{
106
- // skip it: Generic does not throw, only API contains this exception
91
+ getLog ().warn ( "" );
92
+ getLog ().warn ( "You are about to deploy a maven-plugin using Maven " + currentMavenVersion + "." );
93
+ getLog ().warn ( "This plugin should be used ONLY with Maven 3.9.0 and newer, as MNG-7055" );
94
+ getLog ().warn ( "is fixed in those versions of Maven only!" );
95
+ getLog ().warn ( "" );
107
96
}
108
97
}
109
98
}
110
99
111
100
/**
112
101
* Creates resolver {@link RemoteRepository} equipped with needed whistles and bells.
113
102
*/
114
- protected RemoteRepository getRemoteRepository ( final String repositoryId , final String url )
103
+ protected RemoteRepository createDeploymentArtifactRepository ( String id , String url )
115
104
{
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
- }
105
+ return getSession ().getService ( RepositoryFactory .class )
106
+ .createRemote ( id , url );
107
+ }
135
108
136
- return result ;
109
+ protected Session getSession ()
110
+ {
111
+ return session ;
137
112
}
138
113
139
- /**
140
- * Handles high level retries (this was buried into MAT).
141
- */
142
- protected void deploy ( RepositorySystemSession session , DeployRequest deployRequest ) throws MojoExecutionException
114
+ protected Logger getLog ()
143
115
{
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
- }
116
+ return logger ;
177
117
}
118
+
119
+
178
120
}
0 commit comments