@Test void setCoordinatesFromId() { Dependency dependency = Dependency.withId("org.foo:bar:1.2.3"); dependency.resolve(); assertThat(dependency.getGroupId()).isEqualTo("org.foo"); assertThat(dependency.getArtifactId()).isEqualTo("bar"); assertThat(dependency.getVersion()).isEqualTo("1.2.3"); assertThat(dependency.getId()).isEqualTo("org.foo:bar:1.2.3"); }
private void index() { this.indexedDependencies.clear(); this.content.forEach((group) -> group.content.forEach((dependency) -> { // Apply defaults if (dependency.getVersionRange() == null && group.getVersionRange() != null) { dependency.setVersionRange(group.getVersionRange()); } if (dependency.getBom() == null && group.getBom() != null) { dependency.setBom(group.getBom()); } if (dependency.getRepository() == null && group.getRepository() != null) { dependency.setRepository(group.getRepository()); } dependency.resolve(); indexDependency(dependency.getId(), dependency); for (String alias : dependency.getAliases()) { indexDependency(alias, dependency); } })); }
@Override protected ObjectNode mapDependency(Dependency dependency) { ObjectNode content = mapValue(dependency); if (dependency.getVersionRange() != null) { content.put("versionRange", dependency.getVersionRange()); } if (dependency.getLinks() != null && !dependency.getLinks().isEmpty()) { content.set("_links", LinkMapper.mapLinks(dependency.getLinks())); } return content; }
/** * Generate an id using the groupId and artifactId. * @return the generated ID */ public String generateId() { if (this.groupId == null || this.artifactId == null) { throw new IllegalArgumentException("Could not generate id for " + this + ": at least groupId and artifactId must be set."); } setId(this.groupId + ":" + this.artifactId); return getId(); }
private static JsonNode mapDependency(Dependency dep) { ObjectNode node = nodeFactory.objectNode(); node.put("groupId", dep.getGroupId()); node.put("artifactId", dep.getArtifactId()); if (dep.getVersion() != null) { node.put("version", dep.getVersion()); } node.put("scope", dep.getScope()); if (dep.getBom() != null) { node.put("bom", dep.getBom()); } if (dep.getRepository() != null) { node.put("repository", dep.getRepository()); } return node; }
@Test void resolveMatchingVersionMapping() { Dependency dependency = Dependency.withId("web", null, null, "0.3.0.RELEASE"); dependency.setDescription("A web dependency"); dependency.getKeywords().addAll(Arrays.asList("foo", "bar")); dependency.getAliases().add("the-web"); dependency.getFacets().add("web"); dependency.getMappings().add(Dependency.Mapping .create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE")); dependency.getMappings().add(Dependency.Mapping .create("[1.2.0.RELEASE, 1.3.0.RELEASE)", null, null, "0.2.0.RELEASE")); dependency.resolve(); validateResolvedWebDependency(dependency.resolve(Version.parse("1.1.5.RELEASE")), "org.springframework.boot", "spring-boot-starter-web", "0.1.0.RELEASE"); validateResolvedWebDependency(dependency.resolve(Version.parse("1.2.0.RELEASE")), "org.springframework.boot", "spring-boot-starter-web", "0.2.0.RELEASE"); validateResolvedWebDependency(dependency.resolve(Version.parse("2.1.3.M1")), "org.springframework.boot", "spring-boot-starter-web", "0.3.0.RELEASE"); // default }
Map<String, Dependency> dependencies = new LinkedHashMap<>(); for (Dependency dependency : metadata.getDependencies().getAll()) { if (dependency.match(bootVersion)) { dependencies.put(dependency.getId(), dependency.resolve(bootVersion)); if (dependency.getRepository() != null) { repositories.put(dependency.getRepository(), metadata.getConfiguration() .getEnv().getRepositories().get(dependency.getRepository())); if (dependency.getBom() != null) { boms.put(dependency.getBom(), metadata.getConfiguration().getEnv() .getBoms().get(dependency.getBom()).resolve(bootVersion));
private static void assertBootStarter(Dependency actual, String name) { Dependency expected = new Dependency(); expected.asSpringBootStarter(name); assertDependency(actual, expected.getGroupId(), expected.getArtifactId(), expected.getVersion()); assertThat(actual.getId()).isEqualTo(name); }
private static void validateResolvedDependency(Dependency dependency, String id, String expectedGroupId, String expectedArtifactId, String expectedVersion) { assertThat(dependency.getId()).isEqualTo(id); assertThat(dependency.getGroupId()).isEqualTo(expectedGroupId); assertThat(dependency.getArtifactId()).isEqualTo(expectedArtifactId); assertThat(dependency.getVersion()).isEqualTo(expectedVersion); }
private static ObjectNode mapDependency(DependencyItem item) { ObjectNode node = JsonNodeFactory.instance.objectNode(); Dependency dependency = item.dependency; node.put("id", dependency.getId()); node.put("name", dependency.getName()); node.put("group", item.group); if (dependency.getDescription() != null) { node.put("description", dependency.getDescription()); } if (dependency.getWeight() > 0) { node.put("weight", dependency.getWeight()); } if (!CollectionUtils.isEmpty(dependency.getKeywords()) || !CollectionUtils.isEmpty(dependency.getAliases())) { List<String> all = new ArrayList<>(dependency.getKeywords()); all.addAll(dependency.getAliases()); node.put("keywords", StringUtils.collectionToCommaDelimitedString(all)); } return node; }
@Test void setIdFromCoordinatesNoVersion() { Dependency dependency = new Dependency(); dependency.setGroupId("org.foo"); dependency.setArtifactId("bar"); dependency.resolve(); assertThat(dependency.getId()).isEqualTo("org.foo:bar"); }
private void contribute(Map<String, Object> details, Dependency dependency) { if (!ObjectUtils.isEmpty(dependency.getMappings())) { Map<String, VersionRange> dep = new LinkedHashMap<>(); dependency.getMappings().forEach((it) -> { if (it.getRange() != null && it.getVersion() != null) { dep.put(it.getVersion(), it.getRange()); if (dependency.getRange() == null) { boolean openRange = dep.values().stream() .anyMatch((v) -> v.getHigherVersion() == null); depInfo.put(k, "Spring Boot " + r); }); details.put(dependency.getId(), depInfo); else if (dependency.getVersion() != null && dependency.getRange() != null) { Map<String, Object> dep = new LinkedHashMap<>(); String requirement = "Spring Boot " + dependency.getRange(); dep.put(dependency.getVersion(), requirement); details.put(dependency.getId(), dep);
@Test void resolveDependencies() { Dependency first = Dependency.withId("first", "org.foo", "first"); first.getMappings().add(Dependency.Mapping.create( "[1.0.0.RELEASE, 1.1.0.RELEASE)", "org.bar", "second", "0.1.0.RELEASE")); first.getMappings().add(Dependency.Mapping.create("1.1.0.RELEASE", "org.biz", "third", "0.2.0.RELEASE")); Dependency second = Dependency.withId("second", "org.foo", "second"); InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() .addDependencyGroup("test", first, second).build(); DependencyMetadata dependencyMetadata = this.provider.get(metadata, Version.parse("1.0.5.RELEASE")); assertThat(dependencyMetadata.getDependencies()).hasSize(2); assertThat(dependencyMetadata.getDependencies().get("first").getGroupId()) .isEqualTo("org.bar"); assertThat(dependencyMetadata.getDependencies().get("first").getArtifactId()) .isEqualTo("second"); assertThat(dependencyMetadata.getDependencies().get("first").getVersion()) .isEqualTo("0.1.0.RELEASE"); DependencyMetadata anotherDependencyMetadata = this.provider.get(metadata, Version.parse("1.1.0.RELEASE")); assertThat(anotherDependencyMetadata.getDependencies()).hasSize(2); assertThat(anotherDependencyMetadata.getDependencies().get("first").getGroupId()) .isEqualTo("org.biz"); assertThat( anotherDependencyMetadata.getDependencies().get("first").getArtifactId()) .isEqualTo("third"); assertThat(anotherDependencyMetadata.getDependencies().get("first").getVersion()) .isEqualTo("0.2.0.RELEASE"); }
public PomAssert hasDependency(Dependency expected) { String id = generateDependencyId(expected.getGroupId(), expected.getArtifactId()); assertThat(this.dependencies).containsKey(id); Dependency dependency = this.dependencies.get(id); if (expected.getVersion() != null) { assertThat(dependency.getVersion()).isEqualTo(expected.getVersion()); } if (expected.getScope() != null) { assertThat(dependency.getScope()).isEqualTo(expected.getScope()); } if (expected.getType() != null) { assertThat(dependency.getType()).isEqualTo(expected.getType()); } return this; }
@Test void mapDependency() throws Exception { Dependency d = Dependency.withId("foo", "org.foo", "foo"); d.setRepository("my-repo"); d.setBom("my-bom"); Repository repository = new Repository(); repository.setName("foo-repo"); repository.setUrl(new URL("http://example.com/foo")); BillOfMaterials bom = BillOfMaterials.create("org.foo", "foo-bom", "1.0.0.RELEASE"); DependencyMetadata metadata = new DependencyMetadata( Version.parse("1.2.0.RELEASE"), Collections.singletonMap(d.getId(), d), Collections.singletonMap("repo-id", repository), Collections.singletonMap("bom-id", bom)); JSONObject content = new JSONObject(this.mapper.write(metadata)); assertThat(content.getJSONObject("dependencies").getJSONObject("foo") .getString("bom")).isEqualTo("my-bom"); assertThat(content.getJSONObject("dependencies").getJSONObject("foo") .getString("repository")).isEqualTo("my-repo"); assertThat(content.getJSONObject("repositories").getJSONObject("repo-id") .getString("name")).isEqualTo("foo-repo"); assertThat(content.getJSONObject("boms").getJSONObject("bom-id") .getString("artifactId")).isEqualTo("foo-bom"); assertThat(content.getJSONObject("boms").getJSONObject("bom-id") .getString("version")).isEqualTo("1.0.0.RELEASE"); }
if (getId() == null) { if (!hasCoordinates()) { throw new InvalidInitializrMetadataException( "Invalid dependency, should have at least an id or a groupId/artifactId pair."); generateId(); else if (!hasCoordinates()) { StringTokenizer st = new StringTokenizer(getId(), ":"); if (st.countTokens() == 1) { // assume spring-boot-starter asSpringBootStarter(getId()); throw new InvalidInitializrMetadataException( "Invalid dependency, id should have the form groupId:artifactId[:version] but got " + getId()); updateVersionRanges(VersionParser.DEFAULT);
@Override public String toString() { return "Dependency{" + "id='" + getId() + '\'' + ", groupId='" + this.groupId + '\'' + ", artifactId='" + this.artifactId + '\'' + ", version='" + this.version + '\'' + '}'; }
protected String generateDependencyTable(InitializrMetadata metadata) { String[][] dependencyTable = new String[metadata.getDependencies().getAll().size() + 1][]; dependencyTable[0] = new String[] { "Id", "Description", "Required version" }; int i = 1; for (Dependency dep : metadata.getDependencies().getAll().stream() .sorted(Comparator.comparing(MetadataElement::getId)) .collect(Collectors.toList())) { String[] data = new String[3]; data[0] = dep.getId(); data[1] = (dep.getDescription() != null) ? dep.getDescription() : dep.getName(); data[2] = dep.getVersionRequirement(); dependencyTable[i++] = data; } return TableGenerator.generate(dependencyTable); }
private static void assertDependency(Dependency actual, String groupId, String artifactId, String version) { assertThat(actual.getGroupId()).isEqualTo(groupId); assertThat(actual.getArtifactId()).isEqualTo(artifactId); assertThat(actual.getVersion()).isEqualTo(version); }
@Test void createRootSpringBootStarter() { Dependency d = new Dependency(); d.asSpringBootStarter(""); assertThat(d.getGroupId()).isEqualTo("org.springframework.boot"); assertThat(d.getArtifactId()).isEqualTo("spring-boot-starter"); }