public static Dependency withId(String id, String groupId, String artifactId, String version, String scope) { Dependency dependency = new Dependency(); dependency.setId(id); dependency.groupId = groupId; dependency.artifactId = artifactId; dependency.version = version; dependency.scope = (scope != null) ? scope : SCOPE_COMPILE; return dependency; }
/** * Add a default dependency if the project does not define any dependency. */ protected void addDefaultDependency() { Dependency root = new Dependency(); root.setId(DEFAULT_STARTER); root.asSpringBootStarter(""); this.resolvedDependencies.add(root); }
/** * Resolve this instance according to the specified Spring Boot {@link Version}. * Return a {@link Dependency} instance that has its state resolved against the * specified version. * @param bootVersion the Spring Boot version * @return this instance */ public Dependency resolve(Version bootVersion) { for (Mapping mapping : this.mappings) { if (mapping.range.match(bootVersion)) { Dependency dependency = new Dependency(this); dependency.groupId = (mapping.groupId != null) ? mapping.groupId : this.groupId; dependency.artifactId = (mapping.artifactId != null) ? mapping.artifactId : this.artifactId; dependency.version = (mapping.version != null) ? mapping.version : this.version; dependency.versionRequirement = mapping.range.toString(); dependency.mappings = null; return dependency; } } return this; }
/** * Update this request once it has been resolved with the specified * {@link InitializrMetadata}. * @param metadata the initializr metadata */ protected void afterResolution(InitializrMetadata metadata) { if ("war".equals(getPackaging())) { if (!hasWebFacet()) { // Need to be able to bootstrap the web app this.resolvedDependencies.add(determineWebDependency(metadata)); this.facets.add("web"); } // Add the tomcat starter in provided scope Dependency tomcat = new Dependency().asSpringBootStarter("tomcat"); tomcat.setScope(Dependency.SCOPE_PROVIDED); this.resolvedDependencies.add(tomcat); } if (this.resolvedDependencies.stream().noneMatch(Dependency::isStarter)) { // There"s no starter so we add the default one addDefaultDependency(); } }
@Test void createRootSpringBootStarter() { Dependency d = new Dependency(); d.asSpringBootStarter(""); assertThat(d.getGroupId()).isEqualTo("org.springframework.boot"); assertThat(d.getArtifactId()).isEqualTo("spring-boot-starter"); }
@Test void setIdFromCoordinatesNoVersion() { Dependency dependency = new Dependency(); dependency.setGroupId("org.foo"); dependency.setArtifactId("bar"); dependency.resolve(); assertThat(dependency.getId()).isEqualTo("org.foo:bar"); }
private static String generateDependencyId(String groupId, String artifactId) { Dependency dependency = new Dependency(); dependency.setGroupId(groupId); dependency.setArtifactId(artifactId); return dependency.generateId(); }
Node item = nodes.item(i); if (item instanceof Element) { Dependency dependency = new Dependency(); Element element = (Element) item; NodeList groupId = element.getElementsByTagName("groupId");
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); }
@Test void setIdFromCoordinates() { Dependency dependency = new Dependency(); dependency.setGroupId("org.foo"); dependency.setArtifactId("bar"); dependency.setVersion("1.0"); dependency.resolve(); assertThat(dependency.getId()).isEqualTo("org.foo:bar"); }
@Test void generateIdWithNoArtifactId() { Dependency dependency = new Dependency(); dependency.setGroupId("foo"); assertThatIllegalArgumentException().isThrownBy(dependency::generateId); }
@Test void generateIdWithNoGroupId() { Dependency dependency = new Dependency(); dependency.setArtifactId("bar"); assertThatIllegalArgumentException().isThrownBy(dependency::generateId); }
@Test void invalidDependency() { assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(() -> new Dependency().resolve()); }
public InitializrMetadataTestBuilder addDependencyGroup(String name, String... ids) { this.builder.withCustomizer((it) -> { DependencyGroup group = new DependencyGroup(); group.setName(name); for (String id : ids) { Dependency dependency = new Dependency(); dependency.setId(id); group.getContent().add(dependency); } it.getDependencies().getContent().add(group); }); return this; }