9
9
import org .apache .maven .plugins .annotations .Parameter ;
10
10
import org .apache .maven .project .MavenProjectHelper ;
11
11
import org .codehaus .plexus .util .FileUtils ;
12
- import org .eclipse .aether .DefaultRepositorySystemSession ;
13
12
import org .eclipse .aether .RepositorySystemSession ;
14
13
import org .eclipse .aether .artifact .DefaultArtifact ;
15
14
import org .eclipse .aether .impl .ArtifactResolver ;
16
- import org .eclipse .aether .internal .impl .EnhancedLocalRepositoryManagerFactory ;
17
15
import org .eclipse .aether .repository .LocalRepository ;
18
16
import org .eclipse .aether .repository .RemoteRepository ;
19
- import org .eclipse .aether .repository .RepositoryPolicy ;
20
17
import org .eclipse .aether .resolution .ArtifactRequest ;
21
18
import org .eclipse .aether .resolution .ArtifactResolutionException ;
22
19
import org .eclipse .aether .resolution .ArtifactResult ;
30
27
import java .io .InputStreamReader ;
31
28
import java .io .OutputStreamWriter ;
32
29
import java .io .PrintWriter ;
30
+ import java .lang .reflect .Field ;
33
31
import java .nio .charset .Charset ;
34
32
import java .nio .file .Files ;
35
33
import java .util .ArrayList ;
@@ -56,10 +54,7 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
56
54
protected String snapshotDeploymentRepository ;
57
55
58
56
@ Parameter (defaultValue = "${repositorySystemSession}" , required = true )
59
- protected RepositorySystemSession session ;
60
-
61
- @ Component
62
- protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory ;
57
+ private RepositorySystemSession session ;
63
58
64
59
@ Parameter (defaultValue = "${project.build.directory}" , required = true )
65
60
protected File buildDirectory ;
@@ -114,7 +109,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
114
109
* @throws MojoExecutionException
115
110
* @throws MojoFailureException
116
111
*/
117
- protected RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
112
+ private RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
118
113
if (getLog ().isDebugEnabled ()) {
119
114
getLog ().debug ("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository );
120
115
}
@@ -142,14 +137,15 @@ protected RemoteRepository getRepository(final String altRepository) throws Mojo
142
137
}
143
138
144
139
private String getCoordinates (ArtifactResult result ) {
145
- StringBuilder buffer = new StringBuilder (128 );
146
- buffer .append (result .getArtifact ().getGroupId ());
147
- buffer .append (':' ).append (result .getArtifact ().getArtifactId ());
148
- buffer .append (':' ).append (result .getArtifact ().getExtension ());
149
- if (result .getArtifact ().getClassifier ().length () > 0 ) {
150
- buffer .append (':' ).append (result .getArtifact ().getClassifier ());
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 () );
151
147
}
152
- buffer .append (':' ).append (result .getArtifact ().getBaseVersion ());
148
+ buffer .append ( ':' ).append ( result .getArtifact ().getBaseVersion () );
153
149
return buffer .toString ();
154
150
}
155
151
@@ -187,46 +183,47 @@ private String getCoordinates(org.apache.maven.artifact.Artifact artifact) {
187
183
* group:artifact:type:classifier:version
188
184
*/
189
185
protected void attachArtifactCatalog () throws MojoExecutionException {
190
- getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
186
+ getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
191
187
192
- File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
188
+ File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
193
189
194
- PrintWriter writer = null ;
190
+ PrintWriter writer = null ;
195
191
196
- try {
197
- catalog .delete ();
198
- buildDirectory .mkdirs ();
199
- writer = new PrintWriter (new OutputStreamWriter (new FileOutputStream (catalog ), Charset .forName ("UTF-8" )));
200
-
201
- if (project .getArtifact () != null && project .getArtifact ().getFile () != null &&
202
- project .getArtifact ().getFile ().exists () && !project .getArtifact ().getFile ().isDirectory ()) {
203
- String coords = getCoordinates (project .getArtifact ());
204
- if (!coords .isEmpty ()) {
205
- getLog ().info ("Cataloging: " + coords );
206
- writer .println (coords );
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." );
207
207
}
208
- } else {
209
- getLog ().info ("No primary artifact to catalog, cataloging attached artifacts instead." );
210
- }
211
208
212
- // Iterate the attached artifacts.
213
- for (org .apache .maven .artifact .Artifact artifact : project .getAttachedArtifacts ()) {
214
- String coords = getCoordinates (artifact );
215
- if (!coords .isEmpty ()) {
216
- getLog ().info ("Cataloging: " + coords );
217
- writer .println (coords );
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
+ }
218
216
}
219
- }
220
217
221
- getLog ().info ("Attaching catalog artifact: " + catalog );
222
- projectHelper .attachArtifact (project , "txt" , "catalog" , catalog );
223
- } catch (IOException ioe ) {
224
- throw new MojoExecutionException ("Failed to create catalog of artifacts" , ioe );
225
- } finally {
226
- if (writer != null ) {
227
- writer .close ();
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
+ }
228
226
}
229
- }
230
227
}
231
228
232
229
/**
@@ -253,30 +250,35 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
253
250
// A place to store our resolved files...
254
251
List <ArtifactResult > resolvedArtifacts = new ArrayList <ArtifactResult >();
255
252
253
+ // Keep track of the original base directory.
254
+ Field localBaseDir = null ;
255
+ File originalBaseDir = session .getLocalRepositoryManager ().getRepository ().getBasedir ();
256
256
257
- // Use a custom repository session, setup to force a few behaviors we like.
258
- DefaultRepositorySystemSession tempSession = new DefaultRepositorySystemSession (session );
259
- tempSession .setUpdatePolicy (RepositoryPolicy .UPDATE_POLICY_ALWAYS );
260
-
257
+ // Disable the local repository - using a bit of reflection that I wish we didn't need to use.
261
258
File tempRepo = null ;
262
259
if (disableLocal ) {
263
- getLog ().info ("Disabling local repository @ " + tempSession .getLocalRepository ().getBasedir ());
260
+ getLog ().info ("Disabling local repository @ " + session .getLocalRepository ().getBasedir ());
264
261
try {
262
+ localBaseDir = LocalRepository .class .getDeclaredField ("basedir" );
263
+ localBaseDir .setAccessible (true );
264
+
265
+ // Generate a new temp directory.
265
266
tempRepo = Files .createTempDirectory ("gitflow-helper-maven-plugin-repo" ).toFile ();
266
267
267
268
getLog ().info ("Using temporary local repository @ " + tempRepo .getAbsolutePath ());
268
- tempSession . setLocalRepositoryManager ( localRepositoryManagerFactory . newInstance ( tempSession , new LocalRepository ( tempRepo )) );
269
+ localBaseDir . set ( session . getLocalRepositoryManager (). getRepository (), tempRepo );
269
270
} catch (Exception ex ) {
270
271
getLog ().warn ("Failed to disable local repository path." , ex );
271
272
}
272
273
}
273
274
275
+
274
276
List <ArtifactRequest > requiredArtifacts = new ArrayList <ArtifactRequest >();
275
277
276
278
// Locate our text catalog classifier file. :-)
277
279
BufferedReader reader = null ;
278
280
try {
279
- ArtifactResult catalogResult = artifactResolver .resolveArtifact (tempSession , new ArtifactRequest (new DefaultArtifact (project .getGroupId (), project .getArtifactId (), "catalog" , "txt" , project .getVersion ()), remoteRepositories , null ));
281
+ ArtifactResult catalogResult = artifactResolver .resolveArtifact (session , new ArtifactRequest (new DefaultArtifact (project .getGroupId (), project .getArtifactId (), "catalog" , "txt" , project .getVersion ()), remoteRepositories , null ));
280
282
resolvedArtifacts .add (catalogResult );
281
283
282
284
if (catalogResult .isResolved ()) {
@@ -299,15 +301,14 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
299
301
if (reader != null ) {
300
302
try {
301
303
reader .close ();
302
- } catch (IOException ioe ) {
303
- }
304
+ } catch (IOException ioe ) {}
304
305
}
305
306
}
306
307
307
308
308
309
// Resolve the artifacts from the catalog (if there are any)
309
310
try {
310
- resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (tempSession , requiredArtifacts ));
311
+ resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (session , requiredArtifacts ));
311
312
} catch (ArtifactResolutionException are ) {
312
313
throw new MojoExecutionException ("Failed to resolve the required project files from: " + sourceRepository , are );
313
314
}
@@ -338,6 +339,12 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
338
339
339
340
// Restore the local repository, again using reflection.
340
341
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
+ }
341
348
if (tempRepo != null ) {
342
349
try {
343
350
FileUtils .deleteDirectory (tempRepo );
0 commit comments