]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1859] Improve handling of invalid artifacts
authorMartin Stockhammer <martin_s@apache.org>
Fri, 14 Apr 2017 16:45:49 +0000 (18:45 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Fri, 14 Apr 2017 16:45:49 +0000 (18:45 +0200)
Throws only exception if all artifacts are invalid.
Ignores resolver-status.properties

archiva-modules/plugins/maven2-repository/pom.xml
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryStorage.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java
archiva-modules/plugins/maven2-repository/src/test/resources/resolver-status.properties [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/resources/test01.properties [new file with mode: 0644]

index c77e2ff6529b698809b02cf6bc6d7d5a12725211..cd65a89c49793c5836e01009f5cd16077935ac16 100644 (file)
               <exclude>src/test/resources/m1-repo-filelist.txt</exclude>
               <exclude>src/test/repositories/**</exclude>
               <exclude>src/test/expected-poms/**</exclude>
+              <exclude>src/test/resources/resolver-status.properties</exclude>
+              <exclude>src/test/resources/test01.properties</exclude>
             </excludes>
           </configuration>
         </plugin>
index 58a4dee568e4e5da669d47773b4883a5db5fe1bd..efdb46087a50680d291fe69ad0dc1663f3973f99 100644 (file)
@@ -148,6 +148,9 @@ public class Maven2RepositoryStorage
 
     private static final String METADATA_FILENAME = METADATA_FILENAME_START + ".xml";
 
+    // This array must be lexically sorted
+    private static final String[] IGNORED_FILES = { METADATA_FILENAME, "resolver-status.properties" };
+
     private static final MavenXpp3Reader MAVEN_XPP_3_READER = new MavenXpp3Reader();
 
 
@@ -615,13 +618,25 @@ public class Maven2RepositoryStorage
         List<ArtifactMetadata> artifacts = new ArrayList<>();
         if ( files != null )
         {
+            int errorCount=0;
             for ( File file : files )
             {
-                ArtifactMetadata metadata =
-                    getArtifactFromFile( readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(),
-                                         readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(),
-                                         file );
-                artifacts.add( metadata );
+                try {
+                    ArtifactMetadata metadata =
+                            getArtifactFromFile(readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(),
+                                    readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(),
+                                    file);
+                    artifacts.add(metadata);
+                } catch (Exception ex) {
+                    LOGGER.error("Error while retrieving metadata of file {} (Project: {}, Repository: {}): {}",
+                            file.getName(), readMetadataRequest.getProjectId(), readMetadataRequest.getRepositoryId(),
+                            ex.getMessage());
+                    errorCount++;
+                }
+            }
+            // We throw only an error, if the number of errors equals the number of files
+            if (errorCount>0 && errorCount==files.length) {
+                throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
             }
         }
         return artifacts;
@@ -1016,7 +1031,7 @@ public class Maven2RepositoryStorage
             {
                 return false;
             }
-            else if ( name.equals( METADATA_FILENAME ) )
+            else if ( Arrays.binarySearch(IGNORED_FILES, name)>=0 )
             {
                 return false;
             }
index c277434d84668c1eadf13bb41881132a368288af..93f6bc7fbc126cabb34273d0d860a224e45af13f 100644 (file)
@@ -101,11 +101,14 @@ public class DependencyTreeBuilderTestMaven3
             builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
                                          TEST_VERSION );
 
-        assertThat( treeEntries ).isNotNull().isNotEmpty().contains(
-            new TreeEntry( new Artifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, "", "" ) ) );
+        Artifact artifact = new Artifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, "", "" );
+        artifact.setFileExtension("jar");
+        assertThat( treeEntries ).isNotNull().isNotEmpty().contains(new TreeEntry(artifact) );
 
+        artifact = new Artifact( "commons-lang", "commons-lang", "2.2", "compile", "" );
+        artifact.setFileExtension("jar");
         assertThat( treeEntries.get( 0 ).getChilds() ).isNotNull().isNotEmpty().contains(
-            new TreeEntry( new Artifact( "commons-lang", "commons-lang", "2.2", "compile", "" ) ) );
+            new TreeEntry(artifact) );
     }
 
 
index 3f3b8e2dc716021a4df9724fe8a13b5f4dab6dca..24e9b2609cc744ca028f0e0223814a25aa56d35e 100644 (file)
@@ -52,6 +52,12 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import java.io.File;
 import java.io.IOException;
+import java.net.URL;
+import java.nio.file.CopyOption;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -324,6 +330,90 @@ public class Maven2RepositoryMetadataResolverTest
         assertEquals( "pom", facet.getType() );
     }
 
+    @Test
+    public void testGetArtifactMetadataSnapshotsMRM1859()
+            throws Exception
+    {
+        Path repoDir = Paths.get("target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT");
+        URL url = Thread.currentThread().getContextClassLoader().getResource("resolver-status.properties");
+        Path resFile = Paths.get(url.toURI());
+        Path destFile = repoDir.resolve(resFile.getFileName());
+        Files.copy(resFile, destFile, StandardCopyOption.REPLACE_EXISTING);
+        URL url2 = Thread.currentThread().getContextClassLoader().getResource("test01.properties");
+        Path resFile2 = Paths.get(url2.toURI());
+        Path destFile2 = repoDir.resolve(resFile2.getFileName());
+        Files.copy(resFile2, destFile2, StandardCopyOption.REPLACE_EXISTING);
+
+        try {
+
+
+            Collection<ArtifactMetadata> testArtifacts = storage.readArtifactsMetadata(
+                    new ReadMetadataRequest(TEST_REPO_ID, "com.example.test", "test-artifact", "1.0-SNAPSHOT", ALL));
+            List<ArtifactMetadata> artifacts = new ArrayList<>(testArtifacts);
+            Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {
+                @Override
+                public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
+                    return o1.getId().compareTo(o2.getId());
+                }
+            });
+
+            assertEquals(6, artifacts.size());
+
+            ArtifactMetadata artifactMetadata = artifacts.get(0);
+            assertEquals("test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId());
+            MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(1, facet.getBuildNumber());
+            assertEquals("20100308.230825", facet.getTimestamp());
+            assertNull(facet.getClassifier());
+            assertEquals("jar", facet.getType());
+
+            artifactMetadata = artifacts.get(1);
+            assertEquals("test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId());
+            facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(1, facet.getBuildNumber());
+            assertEquals("20100308.230825", facet.getTimestamp());
+            assertNull(facet.getClassifier());
+            assertEquals("pom", facet.getType());
+
+            artifactMetadata = artifacts.get(2);
+            assertEquals("test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId());
+            facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(2, facet.getBuildNumber());
+            assertEquals("20100310.014828", facet.getTimestamp());
+            assertEquals("javadoc", facet.getClassifier());
+            assertEquals("javadoc", facet.getType());
+
+            artifactMetadata = artifacts.get(3);
+            assertEquals("test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId());
+            facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(2, facet.getBuildNumber());
+            assertEquals("20100310.014828", facet.getTimestamp());
+            assertEquals("sources", facet.getClassifier());
+            assertEquals("java-source", facet.getType());
+
+            artifactMetadata = artifacts.get(4);
+            assertEquals("test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId());
+            facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(2, facet.getBuildNumber());
+            assertEquals("20100310.014828", facet.getTimestamp());
+            assertNull(facet.getClassifier());
+            assertEquals("jar", facet.getType());
+
+            artifactMetadata = artifacts.get(5);
+            assertEquals("test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId());
+            facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
+            assertEquals(2, facet.getBuildNumber());
+            assertEquals("20100310.014828", facet.getTimestamp());
+            assertNull(facet.getClassifier());
+            assertEquals("pom", facet.getType());
+
+        } finally {
+            Files.delete(destFile);
+            Files.delete(destFile2);
+        }
+
+    }
+
     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
     {
         assertDependency( dependency, groupId, artifactId, version, "compile" );
diff --git a/archiva-modules/plugins/maven2-repository/src/test/resources/resolver-status.properties b/archiva-modules/plugins/maven2-repository/src/test/resources/resolver-status.properties
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/archiva-modules/plugins/maven2-repository/src/test/resources/test01.properties b/archiva-modules/plugins/maven2-repository/src/test/resources/test01.properties
new file mode 100644 (file)
index 0000000..e69de29