private OArtifactReference getArtifactReference(String groupArtifactVersion, Path pathToArtifact) { if (groupArtifactVersion == null || pathToArtifact == null) return null; if (!pathToArtifact.toString().endsWith(".jar")) return null; DefaultArtifact defaultArtifact = new DefaultArtifact(groupArtifactVersion); return OArtifactReference.valueOf(defaultArtifact.setFile(pathToArtifact.toFile())); }
/** * Installs a file as artifact to a local repository. * * @param spec <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version> * @param file artifact file * @throws InstallationException */ public void install(String spec, File file) throws InstallationException { InstallRequest request = new InstallRequest(); Artifact artifact = new DefaultArtifact(spec).setFile(file); request.setArtifacts(ImmutableList.of(artifact)); repositorySystem.install(session, request); }
private File resolve(final String group, final String artifact, final String version, final String classifier) { final DefaultArtifact art = new DefaultArtifact(group, artifact, classifier, "jar", version); final ArtifactRequest artifactRequest = new ArtifactRequest().setArtifact(art).setRepositories(remoteRepositories); final LocalRepositoryManager lrm = session.getLocalRepositoryManager(); art.setFile(new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifactRequest.getArtifact()))); try { final ArtifactResult result = repositorySystem.resolveArtifact(session, artifactRequest); if (result.isMissing()) { throw new IllegalStateException("Can't find commons-cli, please add it to the pom."); } return result.getArtifact().getFile(); } catch (final ArtifactResolutionException e) { throw new IllegalStateException(e.getMessage(), e); } }
@Test public void testWithInvalidArtifact() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(new File("does not exist.jar")); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testWithInvalidArtifact() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(new File("does not exist.jar")); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testDisabledCache() throws IOException { String gacv = "org.acme:acme:jar:1.0"; cache = new Cache(true, false, cacheFile); ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); File file = File.createTempFile("acme", ".jar"); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(file); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testDisabledCache() throws IOException { String gacv = "org.acme:acme:jar:1.0"; cache = new Cache(true, false, cacheFile); ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); File file = File.createTempFile("acme", ".jar"); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(file); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testCacheDisabledForSnapshots() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; cache = new Cache(false, true, cacheFile); ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testCachingOfReleaseAndUpdate() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(TEMP_FILE); Artifact artifact2 = new DefaultArtifact("org.acme:acme-dep:jar:1.0").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Arrays.asList(artifact, artifact2)); list = cache.get(gacv, options); assertThat(list).hasSize(2); }
@Test public void testCacheDisabledForSnapshots() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; cache = new Cache(false, true, cacheFile); ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testSnapshotEviction() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); Optional<Cache.CacheEntry> entry = cache.find(gacv, options); entry.get().setInsertionTime(System.currentTimeMillis() - 25 * 60 * 60 * 1000); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testCachingOfReleaseAndUpdate() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(TEMP_FILE); Artifact artifact2 = new DefaultArtifact("org.acme:acme-dep:jar:1.0").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Arrays.asList(artifact, artifact2)); list = cache.get(gacv, options); assertThat(list).hasSize(2); }
@Test public void testSnapshotEviction() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); Optional<Cache.CacheEntry> entry = cache.find(gacv, options); entry.get().setInsertionTime(System.currentTimeMillis() - 25 * 60 * 60 * 1000); list = cache.get(gacv, options); assertThat(list).isNull(); }
@Test public void testCachingOfSnapshotAndUpdate() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); Artifact artifact2 = new DefaultArtifact("org.acme:acme-dep:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Arrays.asList(artifact, artifact2)); list = cache.get(gacv, options); assertThat(list).hasSize(2); }
@Test public void testNonSnapshotEviction() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); Optional<Cache.CacheEntry> entry = cache.find(gacv, options); entry.get().setInsertionTime(System.currentTimeMillis() - 22 * 60 * 60 * 1000); list = cache.get(gacv, options); assertThat(list).hasSize(1); }
@Test public void testCachingOfSnapshotAndUpdate() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); Artifact artifact2 = new DefaultArtifact("org.acme:acme-dep:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.put(gacv, options, Arrays.asList(artifact, artifact2)); list = cache.get(gacv, options); assertThat(list).hasSize(2); }
@Test public void testNonSnapshotEviction() throws IOException { String gacv = "org.acme:acme:jar:1.0-SNAPSHOT"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0-SNAPSHOT").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); Optional<Cache.CacheEntry> entry = cache.find(gacv, options); entry.get().setInsertionTime(System.currentTimeMillis() - 22 * 60 * 60 * 1000); list = cache.get(gacv, options); assertThat(list).hasSize(1); }
@Test public void testCacheReloading() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.writeCacheOnFile(); cache = new Cache(false, false, cacheFile); list = cache.get(gacv, options); assertThat(list).hasSize(1); }
@Test public void testCacheReloading() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); cache.writeCacheOnFile(); cache = new Cache(false, false, cacheFile); list = cache.get(gacv, options); assertThat(list).hasSize(1); }
@Test public void testCachingUsingDifferentResolutionOption() throws IOException { String gacv = "org.acme:acme:jar:1.0"; ResolutionOptions options = new ResolutionOptions(); List<Artifact> list = cache.get(gacv, options); assertThat(list).isNull(); Artifact artifact = new DefaultArtifact("org.acme:acme:jar:1.0").setFile(TEMP_FILE); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); options = new ResolutionOptions().setWithTransitive(false); list = cache.get(gacv, options); assertThat(list).isNull(); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); assertThat(cache.size()).isEqualTo(2); options = new ResolutionOptions().setWithTransitive(true).addExclusion("org.acme:transitive"); list = cache.get(gacv, options); assertThat(list).isNull(); cache.put(gacv, options, Collections.singletonList(artifact)); list = cache.get(gacv, options); assertThat(list).hasSize(1); assertThat(cache.size()).isEqualTo(3); }