GradleResolver implements MavenResolver { @Override public File resolveArtifact(ArtifactCoordinates artifactCoordinates, String packaging) throws IOException { String filter = toGradleArtifactFileName(artifactCoordinates, packaging); Path artifactDirectory = Paths.get(gradleCachePath, artifactCoordinates.getGroupId(), artifactCoordinates.getArtifactId(), artifactCoordinates.getVersion()); if (Files.exists(artifactDirectory)) { File latestArtifactFile = null; for (Path hashDir : Files.list(artifactDirectory).collect(Collectors.toList())) { for (Path artifact : Files.list(hashDir).collect(Collectors.toList())) { if (artifact.endsWith(filter)) { File artifactFile = artifact.toFile(); if (latestArtifactFile == null || latestArtifactFile.lastModified() < artifactFile.lastModified()) { latestArtifactFile = artifactFile; } } } } if (latestArtifactFile != null) { return latestArtifactFile; } } return downloadFromRemoteRepository(artifactCoordinates, packaging, artifactDirectory); } GradleResolver(String gradleCachePath); @Override File resolveArtifact(ArtifactCoordinates artifactCoordinates, String packaging);  }
@Test public void testResolveArtifact() throws IOException { File dirFile = TempFileManager.INSTANCE.newTempDirectory("gradle", null); Path gradleCachePath = dirFile.toPath(); String group = "org.wildfly.swarm"; String packaging = "jar"; String artifact = "test"; String version = "1.0"; String classifier = "sources"; ArtifactCoordinates artifactCoordinates = new ArtifactCoordinates(group, artifact, version, classifier); Path artifactDir = Files.createDirectories(gradleCachePath.resolve(group).resolve(artifact).resolve(version).resolve("hash")); File artifactFile = Files.createFile(artifactDir.resolve(artifact + "-" + version + "-" + classifier + "." + packaging)).toFile(); GradleResolver resolver = new GradleResolver(gradleCachePath.toString()); File resolvedArtifactFile = resolver.resolveArtifact(artifactCoordinates, packaging); assertEquals(artifactFile, resolvedArtifactFile); }
@Test public void testResolveArtifact_latest() throws IOException, InterruptedException { File dirFile = TempFileManager.INSTANCE.newTempDirectory("gradle", null); Path gradleCachePath = dirFile.toPath(); String group = "org.wildfly.swarm"; String packaging = "jar"; String artifact = "test"; String version = "1.0"; String classifier = "sources"; ArtifactCoordinates artifactCoordinates = new ArtifactCoordinates(group, artifact, version, classifier); Path artifactDir = Files.createDirectories(gradleCachePath.resolve(group).resolve(artifact).resolve(version).resolve("hash1")); File artifactFile = Files.createFile(artifactDir.resolve(artifact + "-" + version + "-" + classifier + "." + packaging)).toFile(); Thread.sleep(2000); Path artifactDirLatest = Files.createDirectories(gradleCachePath.resolve(group).resolve(artifact).resolve(version).resolve("hash2")); File artifactFileLatest = Files.createFile(artifactDirLatest.resolve(artifact + "-" + version + "-" + classifier + "." + packaging)).toFile(); GradleResolver resolver = new GradleResolver(gradleCachePath.toString()); File resolvedArtifactFile = resolver.resolveArtifact(artifactCoordinates, packaging); assertEquals(artifactFileLatest, resolvedArtifactFile); }
@Test public void testResolveArtifact_notExists() throws IOException { File dirFile = TempFileManager.INSTANCE.newTempDirectory("gradle", null); Path gradleCachePath = dirFile.toPath(); String group = "org.wildfly.swarm"; String packaging = "jar"; String artifact = "test"; String version = "1.0"; String classifier = "sources"; ArtifactCoordinates artifactCoordinates = new ArtifactCoordinates(group, artifact, version, classifier); Path artifactDir = Files.createDirectories(gradleCachePath.resolve(group).resolve(artifact).resolve(version).resolve("hash")); File artifactFile = Files.createFile(artifactDir.resolve(artifact + "-" + version + "-" + classifier + ".pom")).toFile(); GradleResolver resolver = new GradleResolver(gradleCachePath.toString()); File resolvedArtifactFile = resolver.resolveArtifact(artifactCoordinates, packaging); assertNull(resolvedArtifactFile); }