33import org .apache .maven .artifact .repository .ArtifactRepository ;
44import org .apache .maven .artifact .repository .ArtifactRepositoryFactory ;
55import org .apache .maven .artifact .repository .layout .ArtifactRepositoryLayout ;
6+ import org .apache .maven .model .Repository ;
67import org .apache .maven .plugin .MojoExecutionException ;
78import org .apache .maven .plugin .MojoFailureException ;
89import org .apache .maven .plugins .annotations .Component ;
910import org .apache .maven .plugins .annotations .Parameter ;
1011import org .apache .maven .project .MavenProjectHelper ;
1112import org .codehaus .plexus .util .FileUtils ;
13+ import org .eclipse .aether .DefaultRepositorySystemSession ;
1214import org .eclipse .aether .RepositorySystemSession ;
1315import org .eclipse .aether .artifact .DefaultArtifact ;
1416import org .eclipse .aether .impl .ArtifactResolver ;
17+ import org .eclipse .aether .internal .impl .EnhancedLocalRepositoryManagerFactory ;
1518import org .eclipse .aether .repository .LocalRepository ;
1619import org .eclipse .aether .repository .RemoteRepository ;
20+ import org .eclipse .aether .repository .RepositoryPolicy ;
1721import org .eclipse .aether .resolution .ArtifactRequest ;
1822import org .eclipse .aether .resolution .ArtifactResolutionException ;
1923import org .eclipse .aether .resolution .ArtifactResult ;
2731import java .io .InputStreamReader ;
2832import java .io .OutputStreamWriter ;
2933import java .io .PrintWriter ;
30- import java .lang .reflect .Field ;
3134import java .nio .charset .Charset ;
3235import java .nio .file .Files ;
3336import java .util .ArrayList ;
@@ -54,7 +57,10 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
5457 protected String snapshotDeploymentRepository ;
5558
5659 @ Parameter (defaultValue = "${repositorySystemSession}" , required = true )
57- private RepositorySystemSession session ;
60+ protected RepositorySystemSession session ;
61+
62+ @ Component
63+ protected EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory ;
5864
5965 @ Parameter (defaultValue = "${project.build.directory}" , required = true )
6066 protected File buildDirectory ;
@@ -82,23 +88,39 @@ public abstract class AbstractGitflowBasedRepositoryMojo extends AbstractGitflow
8288 */
8389 protected ArtifactRepository getDeploymentRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
8490 Matcher matcher = ALT_REPO_SYNTAX_PATTERN .matcher (altRepository );
91+ Repository candidate = null ;
8592 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+ }
88106 }
89107
90108 if (getLog ().isDebugEnabled ()) {
91109 getLog ().debug ("Getting maven deployment repository (to target artifacts) for: " + altRepository );
92110 }
93111
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 ());
98117
99- ArtifactRepositoryLayout repoLayout = getLayout (layout );
118+ ArtifactRepositoryLayout repoLayout = getLayout (layout );
100119
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+ }
102124 }
103125
104126 /**
@@ -109,7 +131,7 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
109131 * @throws MojoExecutionException
110132 * @throws MojoFailureException
111133 */
112- private RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
134+ protected RemoteRepository getRepository (final String altRepository ) throws MojoExecutionException , MojoFailureException {
113135 if (getLog ().isDebugEnabled ()) {
114136 getLog ().debug ("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository );
115137 }
@@ -137,15 +159,14 @@ private RemoteRepository getRepository(final String altRepository) throws MojoEx
137159 }
138160
139161 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 ());
147168 }
148- buffer .append ( ':' ).append ( result .getArtifact ().getBaseVersion () );
169+ buffer .append (':' ).append (result .getArtifact ().getBaseVersion ());
149170 return buffer .toString ();
150171 }
151172
@@ -183,47 +204,46 @@ private String getCoordinates(org.apache.maven.artifact.Artifact artifact) {
183204 * group:artifact:type:classifier:version
184205 */
185206 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 ());
187208
188- File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
209+ File catalog = new File (buildDirectory , project .getArtifact ().getArtifactId () + ".txt" );
189210
190- PrintWriter writer = null ;
211+ PrintWriter writer = null ;
191212
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 );
207224 }
225+ } else {
226+ getLog ().info ("No primary artifact to catalog, cataloging attached artifacts instead." );
227+ }
208228
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 );
216235 }
236+ }
217237
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 ();
226245 }
246+ }
227247 }
228248
229249 /**
@@ -250,35 +270,30 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
250270 // A place to store our resolved files...
251271 List <ArtifactResult > resolvedArtifacts = new ArrayList <ArtifactResult >();
252272
253- // Keep track of the original base directory.
254- Field localBaseDir = null ;
255- File originalBaseDir = session .getLocalRepositoryManager ().getRepository ().getBasedir ();
256273
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+
258278 File tempRepo = null ;
259279 if (disableLocal ) {
260- getLog ().info ("Disabling local repository @ " + session .getLocalRepository ().getBasedir ());
280+ getLog ().info ("Disabling local repository @ " + tempSession .getLocalRepository ().getBasedir ());
261281 try {
262- localBaseDir = LocalRepository .class .getDeclaredField ("basedir" );
263- localBaseDir .setAccessible (true );
264-
265- // Generate a new temp directory.
266282 tempRepo = Files .createTempDirectory ("gitflow-helper-maven-plugin-repo" ).toFile ();
267283
268284 getLog ().info ("Using temporary local repository @ " + tempRepo .getAbsolutePath ());
269- localBaseDir . set ( session . getLocalRepositoryManager (). getRepository (), tempRepo );
285+ tempSession . setLocalRepositoryManager ( localRepositoryManagerFactory . newInstance ( tempSession , new LocalRepository ( tempRepo )) );
270286 } catch (Exception ex ) {
271287 getLog ().warn ("Failed to disable local repository path." , ex );
272288 }
273289 }
274290
275-
276291 List <ArtifactRequest > requiredArtifacts = new ArrayList <ArtifactRequest >();
277292
278293 // Locate our text catalog classifier file. :-)
279294 BufferedReader reader = null ;
280295 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 ));
282297 resolvedArtifacts .add (catalogResult );
283298
284299 if (catalogResult .isResolved ()) {
@@ -301,14 +316,15 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
301316 if (reader != null ) {
302317 try {
303318 reader .close ();
304- } catch (IOException ioe ) {}
319+ } catch (IOException ioe ) {
320+ }
305321 }
306322 }
307323
308324
309325 // Resolve the artifacts from the catalog (if there are any)
310326 try {
311- resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (session , requiredArtifacts ));
327+ resolvedArtifacts .addAll (artifactResolver .resolveArtifacts (tempSession , requiredArtifacts ));
312328 } catch (ArtifactResolutionException are ) {
313329 throw new MojoExecutionException ("Failed to resolve the required project files from: " + sourceRepository , are );
314330 }
@@ -339,12 +355,6 @@ protected void attachExistingArtifacts(final String sourceRepository, final bool
339355
340356 // Restore the local repository, again using reflection.
341357 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- }
348358 if (tempRepo != null ) {
349359 try {
350360 FileUtils .deleteDirectory (tempRepo );
0 commit comments