1010import org .apache .maven .plugins .annotations .Parameter ;
1111import org .apache .maven .project .MavenProjectHelper ;
1212import org .codehaus .plexus .util .FileUtils ;
13+ import org .eclipse .aether .DefaultRepositorySystemSession ;
1314import org .eclipse .aether .RepositorySystemSession ;
1415import org .eclipse .aether .artifact .DefaultArtifact ;
1516import org .eclipse .aether .impl .ArtifactResolver ;
17+ import org .eclipse .aether .internal .impl .EnhancedLocalRepositoryManagerFactory ;
1618import org .eclipse .aether .repository .LocalRepository ;
1719import org .eclipse .aether .repository .RemoteRepository ;
20+ import org .eclipse .aether .repository .RepositoryPolicy ;
1821import org .eclipse .aether .resolution .ArtifactRequest ;
1922import org .eclipse .aether .resolution .ArtifactResolutionException ;
2023import org .eclipse .aether .resolution .ArtifactResult ;
2831import java .io .InputStreamReader ;
2932import java .io .OutputStreamWriter ;
3033import java .io .PrintWriter ;
31- import java .lang .reflect .Field ;
3234import java .nio .charset .Charset ;
3335import java .nio .file .Files ;
3436import java .util .ArrayList ;
@@ -55,7 +57,10 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
5557 protected String snapshotDeploymentRepository ;
5658
5759 @ Parameter (defaultValue = "${repositorySystemSession}" , required = true )
58- private RepositorySystemSession session ;
60+ protected RepositorySystemSession session ;
61+
62+ @ Component
63+ protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory ;
5964
6065 @ Parameter (defaultValue = "${project.build.directory}" , required = true )
6166 protected File buildDirectory ;
@@ -126,7 +131,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
126131 * @throws MojoExecutionException
127132 * @throws MojoFailureException
128133 */
129- private RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
134+ protected RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
130135 if (getLog ().isDebugEnabled ()) {
131136 getLog ().debug ("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository );
132137 }
@@ -154,15 +159,14 @@ private RemoteRepository getRepository(final String altRepository) throws MojoEx
154159 }
155160
156161 private String getCoordinates (ArtifactResult result ) {
157- StringBuilder buffer = new StringBuilder ( 128 );
158- buffer .append ( result .getArtifact ().getGroupId () );
159- buffer .append ( ':' ).append ( result .getArtifact ().getArtifactId () );
160- buffer .append ( ':' ).append ( result .getArtifact ().getExtension () );
161- if ( result .getArtifact ().getClassifier ().length () > 0 )
162- {
163- 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 ());
164168 }
165- buffer .append ( ':' ).append ( result .getArtifact ().getBaseVersion () );
169+ buffer .append (':' ).append (result .getArtifact ().getBaseVersion ());
166170 return buffer .toString ();
167171 }
168172
@@ -200,47 +204,46 @@ private String getCoordinates(org.apache.maven.artifact.Artifact artifact) {
200204 * group:artifact:type:classifier:version
201205 */
202206 protected void attachArtifactCatalog () throws MojoExecutionException {
203- getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
207+ getLog ().info ("Cataloging Artifacts for promotion & reattachment: " + project .getBuild ().getDirectory ());
204208
205- File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
209+ File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
206210
207- PrintWriter writer = null ;
211+ PrintWriter writer = null ;
208212
209- try {
210- catalog .delete ();
211- buildDirectory .mkdirs ();
212- writer = new PrintWriter (new OutputStreamWriter (new FileOutputStream (catalog ), Charset .forName ("UTF-8" )));
213-
214- if (project .getArtifact () != null && project .getArtifact ().getFile () != null &&
215- project .getArtifact ().getFile ().exists () && !project .getArtifact ().getFile ().isDirectory ())
216- {
217- String coords = getCoordinates (project .getArtifact ());
218- if (!coords .isEmpty ()){
219- getLog ().info ("Cataloging: " + coords );
220- writer .println (coords );
221- }
222- } else {
223- 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 );
224224 }
225+ } else {
226+ getLog ().info ("No primary artifact to catalog, cataloging attached artifacts instead." );
227+ }
225228
226- // Iterate the attached artifacts.
227- for (org .apache .maven .artifact .Artifact artifact : project .getAttachedArtifacts ()) {
228- String coords = getCoordinates (artifact );
229- if (!coords .isEmpty ()) {
230- getLog ().info ("Cataloging: " + coords );
231- writer .println (coords );
232- }
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 );
233235 }
236+ }
234237
235- getLog ().info ("Attaching catalog artifact: " + catalog );
236- projectHelper .attachArtifact (project , "txt" , "catalog" , catalog );
237- } catch (IOException ioe ) {
238- throw new MojoExecutionException ("Failed to create catalog of artifacts" , ioe );
239- } finally {
240- if (writer != null ) {
241- writer .close ();
242- }
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 ();
243245 }
246+ }
244247 }
245248
246249 /**
@@ -267,35 +270,30 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
267270 // A place to store our resolved files...
268271 List <ArtifactResult > resolvedArtifacts = new ArrayList <ArtifactResult >();
269272
270- // Keep track of the original base directory.
271- Field localBaseDir = null ;
272- File originalBaseDir = session .getLocalRepositoryManager ().getRepository ().getBasedir ();
273273
274- // 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+
275278 File tempRepo = null ;
276279 if (disableLocal ) {
277- getLog ().info ("Disabling local repository @ " + session .getLocalRepository ().getBasedir ());
280+ getLog ().info ("Disabling local repository @ " + tempSession .getLocalRepository ().getBasedir ());
278281 try {
279- localBaseDir = LocalRepository .class .getDeclaredField ("basedir" );
280- localBaseDir .setAccessible (true );
281-
282- // Generate a new temp directory.
283282 tempRepo = Files .createTempDirectory ("gitflow-helper-maven-plugin-repo" ).toFile ();
284283
285284 getLog ().info ("Using temporary local repository @ " + tempRepo .getAbsolutePath ());
286- localBaseDir . set ( session . getLocalRepositoryManager (). getRepository (), tempRepo );
285+ tempSession . setLocalRepositoryManager ( localRepositoryManagerFactory . newInstance ( tempSession , new LocalRepository ( tempRepo )) );
287286 } catch (Exception ex ) {
288287 getLog ().warn ("Failed to disable local repository path." , ex );
289288 }
290289 }
291290
292-
293291 List <ArtifactRequest > requiredArtifacts = new ArrayList <ArtifactRequest >();
294292
295293 // Locate our text catalog classifier file. :-)
296294 BufferedReader reader = null ;
297295 try {
298- 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 ));
299297 resolvedArtifacts .add (catalogResult );
300298
301299 if (catalogResult .isResolved ()) {
@@ -318,14 +316,15 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
318316 if (reader != null ) {
319317 try {
320318 reader .close ();
321- } catch (IOException ioe ) {}
319+ } catch (IOException ioe ) {
320+ }
322321 }
323322 }
324323
325324
326325 // Resolve the artifacts from the catalog (if there are any)
327326 try {
328- resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (session , requiredArtifacts ));
327+ resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (tempSession , requiredArtifacts ));
329328 } catch (ArtifactResolutionException are ) {
330329 throw new MojoExecutionException ("Failed to resolve the required project files from: " + sourceRepository , are );
331330 }
@@ -356,12 +355,6 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
356355
357356 // Restore the local repository, again using reflection.
358357 if (disableLocal ) {
359- try {
360- localBaseDir .set (session .getLocalRepositoryManager ().getRepository (), originalBaseDir );
361- localBaseDir .setAccessible (false );
362- } catch (Exception ex ) {
363- getLog ().warn ("Failed to restore original local repository path." , ex );
364- }
365358 if (tempRepo != null ) {
366359 try {
367360 FileUtils .deleteDirectory (tempRepo );
0 commit comments