DependencyConfigurationUtil { public static Collection<File> getConfiguredDependencies(Configuration configuration) { if(repositorySystem == null) { repositorySystem = new MavenRepositorySystem(); } List<File> artifactsToPreload = new LinkedList<File>(); addDefaultGlobalExclusions(); List<Object> excludePatterns = configuration.getList(PRELOAD_EXCLUDES, Collections.EMPTY_LIST); for (Object excludePattern : excludePatterns) { String excludePatternString = excludePattern.toString(); if(excludePattern != null && !excludePatternString.isEmpty()) { logger.info("Excluding {} from dependency resolution.", excludePattern); repositorySystem.addGlobalExclusion(excludePatternString); } } processMavenRepositories(configuration, repositorySystem); List<Object> artifacts = configuration.getList(PRELOAD_ARTIFACTS, Collections.EMPTY_LIST); for (Object artifactSpecification : artifacts) { logger.info("Resolving artifact {}.", artifactSpecification); try { List<File> dependencies = repositorySystem. resolveDependencies(artifactSpecification.toString()); addToListIfUnique(dependencies, artifactsToPreload); logger.debug("{} resolved to {}.", artifactSpecification, dependencies); } catch (DependencyResolutionException ex) { logger.warn("Could not resolve artifact {}.", artifactSpecification, ex); } } List<Object> poms = configuration.getList(PRELOAD_POMS, Collections.EMPTY_LIST); for (Object pomPath : poms) { logger.info("Resolving artifacts from POM file: {}.", pomPath); try { List<File> dependencies = repositorySystem. resolveDependenciesFromPom(new File(pomPath.toString())); addToListIfUnique(dependencies, artifactsToPreload); logger.debug("{} resolved to {}.", pomPath, dependencies); } catch (DependencyResolutionException ex) { logger.warn("Could not resolve artifacts from {}.", pomPath, ex); } } repositorySystem = null; return artifactsToPreload; }  static Collection<File> getConfiguredDependencies(Configuration configuration); static final String MAVEN_REPOSITORIES; static final String MAVEN_REPO_LAYOUT; static final String MAVEN_REPO_URL; static final String PRELOAD_ARTIFACTS; static final String PRELOAD_EXCLUDES; static final String PRELOAD_POMS; }
@Test public void testPreloadArtifact() throws DependencyResolutionException { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_ARTIFACTS, EXAMPLE_APP_COORDS); DependencyConfigurationUtil.getConfiguredDependencies(configuration); ArgumentCaptor<Artifact> artifact = ArgumentCaptor.forClass(Artifact.class); verify(mavenRepositorySystem, times(1)).resolveDependencies(artifact.capture(), argThat(isEmptyList()), argThat(isEmptyList())); assertThat(artifact.getValue().getGroupId(), is(equalTo(EXAMPLE_APP_GROUPID))); assertThat(artifact.getValue().getArtifactId(), is(equalTo(EXAMPLE_APP_ARTIFACTID))); assertThat(artifact.getValue().getVersion(), is(equalTo(EXAMPLE_APP_VERSION))); }
@Test public void testPreloadMultipleArtifacts() throws DependencyResolutionException { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_ARTIFACTS, EXAMPLE_APP_COORDS); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_ARTIFACTS, EXAMPLE_APP2_COORDS); DependencyConfigurationUtil.getConfiguredDependencies(configuration); verify(mavenRepositorySystem, times(1)).resolveDependencies(EXAMPLE_APP_COORDS); verify(mavenRepositorySystem, times(1)).resolveDependencies(EXAMPLE_APP2_COORDS); }
@Test public void testMavenRepository() throws DependencyResolutionException { StringBuilder config = new StringBuilder("preload:").append("\n"); config.append(" maven_repositories:").append("\n"); config.append(" - ").append(EXAMPLE_REPO_ID).append(":").append("\n"); config.append(" ").append(DependencyConfigurationUtil.MAVEN_REPO_LAYOUT). append(": ").append(EXAMPLE_REPO_TYPE).append("\n"); config.append(" ").append(DependencyConfigurationUtil.MAVEN_REPO_URL). append(": ").append(EXAMPLE_REPO_URL).append("\n"); config.append(" - ").append(EXAMPLE_REPO2_ID).append(":").append("\n"); config.append(" ").append(DependencyConfigurationUtil.MAVEN_REPO_LAYOUT). append(": ").append(EXAMPLE_REPO2_TYPE).append("\n"); config.append(" ").append(DependencyConfigurationUtil.MAVEN_REPO_URL). append(": ").append(EXAMPLE_REPO2_URL).append("\n"); ByteArrayInputStream configBytes = new ByteArrayInputStream( config.toString().getBytes(Charsets.UTF_8)); YamlConfiguration configuration = new YamlConfiguration(); configuration.load(new InputStreamReader(configBytes)); DependencyConfigurationUtil.getConfiguredDependencies(configuration); ArgumentCaptor<RemoteRepository> repo = ArgumentCaptor.forClass(RemoteRepository.class); verify(mavenRepositorySystem, times(2)).addRepository(repo.capture()); assertThat(repo.getAllValues().get(0).getId(), is(equalTo(EXAMPLE_REPO_ID))); assertThat(repo.getAllValues().get(0).getContentType(), is(equalTo(EXAMPLE_REPO_TYPE))); assertThat(repo.getAllValues().get(0).getUrl(), is(equalTo(EXAMPLE_REPO_URL))); assertThat(repo.getAllValues().get(1).getId(), is(equalTo(EXAMPLE_REPO2_ID))); assertThat(repo.getAllValues().get(1).getContentType(), is(equalTo(EXAMPLE_REPO2_TYPE))); assertThat(repo.getAllValues().get(1).getUrl(), is(equalTo(EXAMPLE_REPO2_URL))); }
@Test public void testExclusion() throws DependencyResolutionException { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_EXCLUDES, EXAMPLE_APP_COORDS); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_EXCLUDES, EXAMPLE_APP2_COORDS); DependencyConfigurationUtil.getConfiguredDependencies(configuration); verify(mavenRepositorySystem).addGlobalExclusion(eq(EXAMPLE_APP_COORDS)); verify(mavenRepositorySystem).addGlobalExclusion(eq(EXAMPLE_APP2_COORDS)); }
@Test public void testPom() throws DependencyResolutionException { PropertiesConfiguration configuration = new PropertiesConfiguration(); String pomPath = getClass().getResource(EXAMPLE_POM_PATH).getPath(); String pom2Path = getClass().getResource(EXAMPLE_POM2_PATH).getPath(); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_POMS, pomPath); configuration.addProperty(DependencyConfigurationUtil.PRELOAD_POMS, pom2Path); DependencyConfigurationUtil.getConfiguredDependencies(configuration); verify(mavenRepositorySystem, times(1)).resolveDependenciesFromPom(eq(new File(pomPath))); verify(mavenRepositorySystem, times(1)).resolveDependenciesFromPom(eq(new File(pom2Path))); }