You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultArtifactDiscoverer.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package org.apache.maven.repository.discovery;
  2. /*
  3. * Copyright 2001-2005 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import org.apache.maven.artifact.Artifact;
  18. import org.apache.maven.artifact.factory.ArtifactFactory;
  19. import org.codehaus.plexus.util.StringUtils;
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.StringTokenizer;
  25. /**
  26. * Artifact discoverer for the new repository layout (Maven 2.0+).
  27. *
  28. * @author John Casey
  29. * @author Brett Porter
  30. */
  31. public class DefaultArtifactDiscoverer
  32. extends AbstractArtifactDiscoverer
  33. {
  34. private ArtifactFactory artifactFactory;
  35. public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean convertSnapshots )
  36. {
  37. List artifacts = new ArrayList();
  38. String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
  39. for ( int i = 0; i < artifactPaths.length; i++ )
  40. {
  41. String path = artifactPaths[i];
  42. Artifact artifact = buildArtifact( path );
  43. if ( artifact != null )
  44. {
  45. if ( convertSnapshots || !artifact.isSnapshot() )
  46. {
  47. artifacts.add( artifact );
  48. }
  49. }
  50. }
  51. return artifacts;
  52. }
  53. private Artifact buildArtifact( String path )
  54. {
  55. Artifact result;
  56. List pathParts = new ArrayList();
  57. StringTokenizer st = new StringTokenizer( path, "/\\" );
  58. while ( st.hasMoreTokens() )
  59. {
  60. pathParts.add( st.nextToken() );
  61. }
  62. Collections.reverse( pathParts );
  63. if ( pathParts.size() < 4 )
  64. {
  65. addKickedOutPath( path );
  66. return null;
  67. }
  68. // the actual artifact filename.
  69. String filename = (String) pathParts.remove( 0 );
  70. // the next one is the version.
  71. String version = (String) pathParts.remove( 0 );
  72. // the next one is the artifactId.
  73. String artifactId = (String) pathParts.remove( 0 );
  74. // the remaining are the groupId.
  75. Collections.reverse( pathParts );
  76. String groupId = StringUtils.join( pathParts.iterator(), "." );
  77. result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" );
  78. String remainingFilename = filename;
  79. if ( !remainingFilename.startsWith( artifactId + "-" ) )
  80. {
  81. addKickedOutPath( path );
  82. return null;
  83. }
  84. remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
  85. if ( result.isSnapshot() )
  86. {
  87. result = artifactFactory.createArtifact( groupId, artifactId,
  88. remainingFilename.substring( 0, remainingFilename.length() - 4 ),
  89. Artifact.SCOPE_RUNTIME, "jar" );
  90. // poor encapsulation requires we do this to populate base version
  91. if ( !result.isSnapshot() )
  92. {
  93. addKickedOutPath( path );
  94. return null;
  95. }
  96. if ( !result.getBaseVersion().equals( version ) )
  97. {
  98. addKickedOutPath( path );
  99. return null;
  100. }
  101. }
  102. else if ( !remainingFilename.startsWith( version ) )
  103. {
  104. addKickedOutPath( path );
  105. return null;
  106. }
  107. result.setFile( new File( path ) );
  108. return result;
  109. }
  110. }