3
3
import org .apache .maven .artifact .repository .ArtifactRepository ;
4
4
import org .apache .maven .artifact .repository .ArtifactRepositoryFactory ;
5
5
import org .apache .maven .artifact .repository .layout .ArtifactRepositoryLayout ;
6
+ import org .apache .maven .model .Repository ;
6
7
import org .apache .maven .plugin .MojoExecutionException ;
7
8
import org .apache .maven .plugin .MojoFailureException ;
8
9
import org .apache .maven .plugins .annotations .Component ;
9
10
import org .apache .maven .plugins .annotations .Parameter ;
10
11
import org .apache .maven .project .MavenProjectHelper ;
11
12
import org .codehaus .plexus .util .FileUtils ;
13
+ import org .eclipse .aether .DefaultRepositorySystemSession ;
12
14
import org .eclipse .aether .RepositorySystemSession ;
13
15
import org .eclipse .aether .artifact .DefaultArtifact ;
14
16
import org .eclipse .aether .impl .ArtifactResolver ;
17
+ import org .eclipse .aether .internal .impl .EnhancedLocalRepositoryManagerFactory ;
15
18
import org .eclipse .aether .repository .LocalRepository ;
16
19
import org .eclipse .aether .repository .RemoteRepository ;
20
+ import org .eclipse .aether .repository .RepositoryPolicy ;
17
21
import org .eclipse .aether .resolution .ArtifactRequest ;
18
22
import org .eclipse .aether .resolution .ArtifactResolutionException ;
19
23
import org .eclipse .aether .resolution .ArtifactResult ;
27
31
import java .io .InputStreamReader ;
28
32
import java .io .OutputStreamWriter ;
29
33
import java .io .PrintWriter ;
30
- import java .lang .reflect .Field ;
31
34
import java .nio .charset .Charset ;
32
35
import java .nio .file .Files ;
33
36
import java .util .ArrayList ;
@@ -54,7 +57,10 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
54
57
protected String snapshotDeploymentRepository ;
55
58
56
59
@ Parameter (defaultValue = "${repositorySystemSession}" , required = true )
57
- private RepositorySystemSession session ;
60
+ protected RepositorySystemSession session ;
61
+
62
+ @ Component
63
+ protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory ;
58
64
59
65
@ Parameter (defaultValue = "${project.build.directory}" , required = true )
60
66
protected File buildDirectory ;
@@ -82,23 +88,39 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
82
88
*/
83
89
protected ArtifactRepository getDeploymentRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
84
90
Matcher matcher = ALT_REPO_SYNTAX_PATTERN .matcher (altRepository );
91
+ Repository candidate = null ;
85
92
if (!matcher .matches ()) {
86
- throw new MojoFailureException (altRepository , "Invalid syntax for repository." ,
87
- "Invalid syntax for repository. Use \" id::layout::url::unique\" ." );
93
+ for (int i = 0 ; i < project .getRepositories ().size (); i ++) {
94
+ candidate = project .getRepositories ().get (i );
95
+ getLog ().debug ("Checking defined repository ID: " + candidate .getId ().trim () + " against: " + altRepository .trim ());
96
+ if (candidate .getId ().trim ().equals (altRepository .trim ())) {
97
+ break ;
98
+ }
99
+ candidate = null ;
100
+ }
101
+
102
+ if (candidate == null ) {
103
+ throw new MojoFailureException (altRepository , "Invalid syntax for repository or repository id not resolved.." ,
104
+ "Invalid syntax for repository. Use \" id::layout::url::unique\" or only specify the \" id\" ." );
105
+ }
88
106
}
89
107
90
108
if (getLog ().isDebugEnabled ()) {
91
109
getLog ().debug ("Getting maven deployment repository (to target artifacts) for: " + altRepository );
92
110
}
93
111
94
- String id = matcher .group (1 ).trim ();
95
- String layout = matcher .group (2 ).trim ();
96
- String url = matcher .group (3 ).trim ();
97
- boolean unique = Boolean .parseBoolean (matcher .group (4 ).trim ());
112
+ if (candidate == null ) {
113
+ String id = matcher .group (1 ).trim ();
114
+ String layout = matcher .group (2 ).trim ();
115
+ String url = matcher .group (3 ).trim ();
116
+ boolean unique = Boolean .parseBoolean (matcher .group (4 ).trim ());
98
117
99
- ArtifactRepositoryLayout repoLayout = getLayout (layout );
118
+ ArtifactRepositoryLayout repoLayout = getLayout (layout );
100
119
101
- return repositoryFactory .createDeploymentArtifactRepository (id , url , repoLayout , unique );
120
+ return repositoryFactory .createDeploymentArtifactRepository (id , url , repoLayout , unique );
121
+ } else {
122
+ return repositoryFactory .createDeploymentArtifactRepository (candidate .getId (), candidate .getUrl (), getLayout (candidate .getLayout ()), candidate .getSnapshots ().isEnabled ());
123
+ }
102
124
}
103
125
104
126
/**
@@ -109,7 +131,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
109
131
* @throws MojoExecutionException
110
132
* @throws MojoFailureException
111
133
*/
112
- private RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
134
+ protected RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
113
135
if (getLog ().isDebugEnabled ()) {
114
136
getLog ().debug ("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository );
115
137
}
@@ -137,15 +159,14 @@ private RemoteRepository getRepository(final String altRepository) throws MojoEx
137
159
}
138
160
139
161
private String getCoordinates (ArtifactResult result ) {
140
- StringBuilder buffer = new StringBuilder ( 128 );
141
- buffer .append ( result .getArtifact ().getGroupId () );
142
- buffer .append ( ':' ).append ( result .getArtifact ().getArtifactId () );
143
- buffer .append ( ':' ).append ( result .getArtifact ().getExtension () );
144
- if ( result .getArtifact ().getClassifier ().length () > 0 )
145
- {
146
- buffer .append ( ':' ).append ( result .getArtifact ().getClassifier () );
162
+ StringBuilder buffer = new StringBuilder (128 );
163
+ buffer .append (result .getArtifact ().getGroupId ());
164
+ buffer .append (':' ).append (result .getArtifact ().getArtifactId ());
165
+ buffer .append (':' ).append (result .getArtifact ().getExtension ());
166
+ if (result .getArtifact ().getClassifier ().length () > 0 ) {
167
+ buffer .append (':' ).append (result .getArtifact ().getClassifier ());
147
168
}
148
- buffer .append ( ':' ).append ( result .getArtifact ().getBaseVersion () );
169
+ buffer .append (':' ).append (result .getArtifact ().getBaseVersion ());
149
170
return buffer .toString ();
150
171
}
151
172
@@ -183,47 +204,46 @@ private String getCoordinates(org.apache.maven.artifact.Artifact artifact) {
183
204
* group:artifact:type:classifier:version
184
205
*/
185
206
protected void attachArtifactCatalog () throws MojoExecutionException {
186
- getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
207
+ getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
187
208
188
- File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
209
+ File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
189
210
190
- PrintWriter writer = null ;
211
+ PrintWriter writer = null ;
191
212
192
- try {
193
- catalog .delete ();
194
- buildDirectory .mkdirs ();
195
- writer = new PrintWriter (new OutputStreamWriter (new FileOutputStream (catalog ), Charset .forName ("UTF-8" )));
196
-
197
- if (project .getArtifact () != null && project .getArtifact ().getFile () != null &&
198
- project .getArtifact ().getFile ().exists () && !project .getArtifact ().getFile ().isDirectory ())
199
- {
200
- String coords = getCoordinates (project .getArtifact ());
201
- if (!coords .isEmpty ()){
202
- getLog ().info ("Cataloging: " + coords );
203
- writer .println (coords );
204
- }
205
- } else {
206
- getLog ().info ("No primary artifact to catalog, cataloging attached artifacts instead." );
213
+ try {
214
+ catalog .delete ();
215
+ buildDirectory .mkdirs ();
216
+ writer = new PrintWriter (new OutputStreamWriter (new FileOutputStream (catalog ), Charset .forName ("UTF-8" )));
217
+
218
+ if (project .getArtifact () != null && project .getArtifact ().getFile () != null &&
219
+ project .getArtifact ().getFile ().exists () && !project .getArtifact ().getFile ().isDirectory ()) {
220
+ String coords = getCoordinates (project .getArtifact ());
221
+ if (!coords .isEmpty ()) {
222
+ getLog ().info ("Cataloging: " + coords );
223
+ writer .println (coords );
207
224
}
225
+ } else {
226
+ getLog ().info ("No primary artifact to catalog, cataloging attached artifacts instead." );
227
+ }
208
228
209
- // Iterate the attached artifacts.
210
- for (org .apache .maven .artifact .Artifact artifact : project .getAttachedArtifacts ()) {
211
- String coords = getCoordinates (artifact );
212
- if (!coords .isEmpty ()) {
213
- getLog ().info ("Cataloging: " + coords );
214
- writer .println (coords );
215
- }
229
+ // Iterate the attached artifacts.
230
+ for (org .apache .maven .artifact .Artifact artifact : project .getAttachedArtifacts ()) {
231
+ String coords = getCoordinates (artifact );
232
+ if (!coords .isEmpty ()) {
233
+ getLog ().info ("Cataloging: " + coords );
234
+ writer .println (coords );
216
235
}
236
+ }
217
237
218
- getLog ().info ("Attaching catalog artifact: " + catalog );
219
- projectHelper .attachArtifact (project , "txt" , "catalog" , catalog );
220
- } catch (IOException ioe ) {
221
- throw new MojoExecutionException ("Failed to create catalog of artifacts" , ioe );
222
- } finally {
223
- if (writer != null ) {
224
- writer .close ();
225
- }
238
+ getLog ().info ("Attaching catalog artifact: " + catalog );
239
+ projectHelper .attachArtifact (project , "txt" , "catalog" , catalog );
240
+ } catch (IOException ioe ) {
241
+ throw new MojoExecutionException ("Failed to create catalog of artifacts" , ioe );
242
+ } finally {
243
+ if (writer != null ) {
244
+ writer .close ();
226
245
}
246
+ }
227
247
}
228
248
229
249
/**
@@ -250,35 +270,30 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
250
270
// A place to store our resolved files...
251
271
List <ArtifactResult > resolvedArtifacts = new ArrayList <ArtifactResult >();
252
272
253
- // Keep track of the original base directory.
254
- Field localBaseDir = null ;
255
- File originalBaseDir = session .getLocalRepositoryManager ().getRepository ().getBasedir ();
256
273
257
- // Disable the local repository - using a bit of reflection that I wish we didn't need to use.
274
+ // Use a custom repository session, setup to force a few behaviors we like.
275
+ DefaultRepositorySystemSession tempSession = new DefaultRepositorySystemSession (session );
276
+ tempSession .setUpdatePolicy (RepositoryPolicy .UPDATE_POLICY_ALWAYS );
277
+
258
278
File tempRepo = null ;
259
279
if (disableLocal ) {
260
- getLog ().info ("Disabling local repository @ " + session .getLocalRepository ().getBasedir ());
280
+ getLog ().info ("Disabling local repository @ " + tempSession .getLocalRepository ().getBasedir ());
261
281
try {
262
- localBaseDir = LocalRepository .class .getDeclaredField ("basedir" );
263
- localBaseDir .setAccessible (true );
264
-
265
- // Generate a new temp directory.
266
282
tempRepo = Files .createTempDirectory ("gitflow-helper-maven-plugin-repo" ).toFile ();
267
283
268
284
getLog ().info ("Using temporary local repository @ " + tempRepo .getAbsolutePath ());
269
- localBaseDir . set ( session . getLocalRepositoryManager (). getRepository (), tempRepo );
285
+ tempSession . setLocalRepositoryManager ( localRepositoryManagerFactory . newInstance ( tempSession , new LocalRepository ( tempRepo )) );
270
286
} catch (Exception ex ) {
271
287
getLog ().warn ("Failed to disable local repository path." , ex );
272
288
}
273
289
}
274
290
275
-
276
291
List <ArtifactRequest > requiredArtifacts = new ArrayList <ArtifactRequest >();
277
292
278
293
// Locate our text catalog classifier file. :-)
279
294
BufferedReader reader = null ;
280
295
try {
281
- ArtifactResult catalogResult = artifactResolver .resolveArtifact (session , new ArtifactRequest (new DefaultArtifact (project .getGroupId (), project .getArtifactId (), "catalog" , "txt" , project .getVersion ()), remoteRepositories , null ));
296
+ ArtifactResult catalogResult = artifactResolver .resolveArtifact (tempSession , new ArtifactRequest (new DefaultArtifact (project .getGroupId (), project .getArtifactId (), "catalog" , "txt" , project .getVersion ()), remoteRepositories , null ));
282
297
resolvedArtifacts .add (catalogResult );
283
298
284
299
if (catalogResult .isResolved ()) {
@@ -301,14 +316,15 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
301
316
if (reader != null ) {
302
317
try {
303
318
reader .close ();
304
- } catch (IOException ioe ) {}
319
+ } catch (IOException ioe ) {
320
+ }
305
321
}
306
322
}
307
323
308
324
309
325
// Resolve the artifacts from the catalog (if there are any)
310
326
try {
311
- resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (session , requiredArtifacts ));
327
+ resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (tempSession , requiredArtifacts ));
312
328
} catch (ArtifactResolutionException are ) {
313
329
throw new MojoExecutionException ("Failed to resolve the required project files from: " + sourceRepository , are );
314
330
}
@@ -339,12 +355,6 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
339
355
340
356
// Restore the local repository, again using reflection.
341
357
if (disableLocal ) {
342
- try {
343
- localBaseDir .set (session .getLocalRepositoryManager ().getRepository (), originalBaseDir );
344
- localBaseDir .setAccessible (false );
345
- } catch (Exception ex ) {
346
- getLog ().warn ("Failed to restore original local repository path." , ex );
347
- }
348
358
if (tempRepo != null ) {
349
359
try {
350
360
FileUtils .deleteDirectory (tempRepo );
0 commit comments