Handle both snapshots and releases enabled flags in Maven Pom

This commit improves the handling of repositories with Maven.
Previously, the writer wrongly assumed that the default for releases
and snapshots are true and false respectively. However, both defaults
are true which means that snapshots repository are considered for
releases, and releases repositories are considered for snapshots.

MavenRepository has now separate flags for those and the writer makes
sure to only update the `enabled` flag if the chosen value is not
true. This doesn't increase the content for repository definitions
while offering better performance for dependencies resolution.

Closes gh-1226
This commit is contained in:
Stephane Nicoll
2021-04-20 20:20:41 +02:00
parent 88d6e79fca
commit 17df3b9b5d
23 changed files with 303 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,11 +29,12 @@ import io.spring.initializr.generator.version.Version;
class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer<Build> {
private static final MavenRepository SPRING_MILESTONES = MavenRepository
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones").build();
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones")
.onlyReleases().build();
private static final MavenRepository SPRING_SNAPSHOTS = MavenRepository
.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot").name("Spring Snapshots")
.snapshotsEnabled(true).build();
.onlySnapshots().build();
private final Version springBootVersion;

View File

@@ -218,8 +218,8 @@ class BuildComplianceTests extends AbstractComplianceTests {
Dependency bar = Dependency.withId("bar", "org.acme", "bar");
bar.setRepository("bar-repository");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addDependencyGroup("test", foo, bar)
.addRepository("foo-repository", "foo-repo", "https://example.com/foo", false)
.addRepository("bar-repository", "bar-repo", "https://example.com/bar", true).build();
.addReleasesRepository("foo-repository", "foo-repo", "https://example.com/foo")
.addSnapshotsRepository("bar-repository", "bar-repo", "https://example.com/bar").build();
ProjectStructure project = generateProject(java, build, "2.4.1", (description) -> {
description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo));
description.addDependency("bar", MetadataBuildItemMapper.toDependency(bar));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -75,8 +75,8 @@ class DependencyManagementBuildCustomizerTests {
BillOfMaterials bom = BillOfMaterials.create("com.example", "foo-bom", "1.0.0");
bom.getRepositories().add("bar-repo");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addBom("foo-bom", bom)
.addRepository("foo-repo", "foo-repo", "https://example.com/foo", false)
.addRepository("bar-repo", "bar-repo", "https://example.com/bar", false)
.addReleasesRepository("foo-repo", "foo-repo", "https://example.com/foo")
.addReleasesRepository("bar-repo", "bar-repo", "https://example.com/bar")
.addDependencyGroup("test", dependency).build();
Build build = createBuild(metadata);
build.dependencies().add(dependency.getId());

View File

@@ -45,6 +45,9 @@
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
@@ -52,6 +55,9 @@
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

View File

@@ -49,14 +49,17 @@
<id>foo-repository</id>
<name>foo-repo</name>
<url>https://example.com/foo</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>bar-repository</id>
<name>bar-repo</name>
<url>https://example.com/bar</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -230,7 +230,16 @@ public class InitializrMetadataTestBuilder {
return this;
}
public InitializrMetadataTestBuilder addRepository(String id, String name, String url, boolean snapshotsEnabled) {
public InitializrMetadataTestBuilder addReleasesRepository(String id, String name, String url) {
return addRepository(id, name, url, true, false);
}
public InitializrMetadataTestBuilder addSnapshotsRepository(String id, String name, String url) {
return addRepository(id, name, url, false, true);
}
public InitializrMetadataTestBuilder addRepository(String id, String name, String url, boolean releasesEnabled,
boolean snapshotsEnabled) {
this.builder.withCustomizer((it) -> {
Repository repo = new Repository();
repo.setName(name);
@@ -240,6 +249,7 @@ public class InitializrMetadataTestBuilder {
catch (MalformedURLException ex) {
throw new IllegalArgumentException("Cannot create URL", ex);
}
repo.setReleasesEnabled(releasesEnabled);
repo.setSnapshotsEnabled(snapshotsEnabled);
it.getConfiguration().getEnv().getRepositories().put(id, repo);
});

View File

@@ -432,6 +432,14 @@ public class MavenBuildAssert extends AbstractTextAssert<MavenBuildAssert> {
throw new IllegalStateException("Cannot parse URL", ex);
}
}
NodeList releases = element.getElementsByTagName("releases");
if (releases.getLength() > 0) {
Element releasesElement = (Element) releases.item(0);
NodeList releasesEnabled = releasesElement.getElementsByTagName("enabled");
if (releasesEnabled.getLength() > 0) {
repository.setReleasesEnabled("true".equals(releasesEnabled.item(0).getTextContent()));
}
}
NodeList snapshots = element.getElementsByTagName("snapshots");
if (snapshots.getLength() > 0) {
Element snapshotsElement = (Element) snapshots.item(0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ package io.spring.initializr.generator.buildsystem;
* A Maven repository.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public class MavenRepository {
@@ -27,7 +28,8 @@ public class MavenRepository {
* Maven Central.
*/
public static final MavenRepository MAVEN_CENTRAL = MavenRepository
.withIdAndUrl("maven-central", "https://repo.maven.apache.org/maven2").name("Maven Central").build();
.withIdAndUrl("maven-central", "https://repo.maven.apache.org/maven2").name("Maven Central").onlyReleases()
.build();
private final String id;
@@ -35,12 +37,15 @@ public class MavenRepository {
private final String url;
private final boolean releasesEnabled;
private final boolean snapshotsEnabled;
protected MavenRepository(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.url = builder.url;
this.releasesEnabled = builder.releasesEnabled;
this.snapshotsEnabled = builder.snapshotsEnabled;
}
@@ -79,6 +84,14 @@ public class MavenRepository {
return this.url;
}
/**
* Return whether releases are enabled on the repository.
* @return {@code true} to enable releases, {@code false} otherwise
*/
public boolean isReleasesEnabled() {
return this.releasesEnabled;
}
/**
* Return whether snapshots are enabled on the repository.
* @return {@code true} to enable snapshots, {@code false} otherwise
@@ -95,6 +108,8 @@ public class MavenRepository {
private String url;
private boolean releasesEnabled = true;
private boolean snapshotsEnabled;
public Builder(String id, String url) {
@@ -133,6 +148,16 @@ public class MavenRepository {
return this;
}
/**
* Specify whether releases are enabled.
* @param releasesEnabled whether releases are served by the repository
* @return this for method chaining
*/
public Builder releasesEnabled(boolean releasesEnabled) {
this.releasesEnabled = releasesEnabled;
return this;
}
/**
* Specify whether snapshots are enabled.
* @param snapshotsEnabled whether snapshots are served by the repository
@@ -143,6 +168,22 @@ public class MavenRepository {
return this;
}
/**
* Specify that the repository should only be used for releases.
* @return this for method chaining
*/
public Builder onlyReleases() {
return releasesEnabled(true).snapshotsEnabled(false);
}
/**
* Specify that the repository should only be used for snapshots.
* @return this for method chaining
*/
public Builder onlySnapshots() {
return snapshotsEnabled(true).releasesEnabled(false);
}
/**
* Build a {@link MavenRepository} with the current state of this builder.
* @return a {@link MavenRepository}

View File

@@ -442,8 +442,11 @@ public class MavenBuildWriter {
writeSingleElement(writer, "id", repository.getId());
writeSingleElement(writer, "name", repository.getName());
writeSingleElement(writer, "url", repository.getUrl());
if (repository.isSnapshotsEnabled()) {
writeElement(writer, "snapshots", () -> writeSingleElement(writer, "enabled", Boolean.toString(true)));
if (!repository.isReleasesEnabled()) {
writeElement(writer, "releases", () -> writeSingleElement(writer, "enabled", Boolean.toString(false)));
}
if (!repository.isSnapshotsEnabled()) {
writeElement(writer, "snapshots", () -> writeSingleElement(writer, "enabled", Boolean.toString(false)));
}
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@ class MavenRepositoryContainerTests {
assertThat(repository).isNotNull();
assertThat(repository.getName()).isEqualTo("my repo");
assertThat(repository.getUrl()).isEqualTo("https://example.com/releases");
assertThat(repository.isReleasesEnabled()).isTrue();
assertThat(repository.isSnapshotsEnabled()).isFalse();
}
@@ -56,6 +57,7 @@ class MavenRepositoryContainerTests {
assertThat(repository).isNotNull();
assertThat(repository.getName()).isEqualTo("my repo");
assertThat(repository.getUrl()).isEqualTo("https://example.com/releases");
assertThat(repository.isReleasesEnabled()).isTrue();
assertThat(repository.isSnapshotsEnabled()).isFalse();
}
@@ -63,7 +65,7 @@ class MavenRepositoryContainerTests {
void addMavenRepositoryWithSnapshotsEnabled() {
MavenRepositoryContainer container = createTestContainer();
container.add(MavenRepository.withIdAndUrl("custom", "https://example.com/snapshots").name("custom-snapshots")
.snapshotsEnabled(true));
.onlySnapshots());
assertThat(container.ids()).containsOnly("custom");
assertThat(container.items()).hasSize(1);
assertThat(container.isEmpty()).isFalse();
@@ -72,6 +74,7 @@ class MavenRepositoryContainerTests {
assertThat(repository).isNotNull();
assertThat(repository.getName()).isEqualTo("custom-snapshots");
assertThat(repository.getUrl()).isEqualTo("https://example.com/snapshots");
assertThat(repository.isReleasesEnabled()).isFalse();
assertThat(repository.isSnapshotsEnabled()).isTrue();
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.buildsystem;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link MavenRepository}.
*
* @author Stephane Nicoll
*/
class MavenRepositoryTests {
@Test
void repositoryWithDetails() {
MavenRepository repo = MavenRepository.withIdAndUrl("test", "https://repo.example.com").name("Test repository")
.build();
assertThat(repo.getId()).isEqualTo("test");
assertThat(repo.getUrl()).isEqualTo("https://repo.example.com");
assertThat(repo.getName()).isEqualTo("Test repository");
}
@Test
void repositoryByDefaultOnlyUseReleases() {
MavenRepository repo = MavenRepository.withIdAndUrl("test", "https://repo.example.com").build();
assertThat(repo.isReleasesEnabled()).isTrue();
assertThat(repo.isSnapshotsEnabled()).isFalse();
}
@Test
void repositoryWithOnlyReleases() {
MavenRepository repo = MavenRepository.withIdAndUrl("test", "https://repo.example.com").onlyReleases().build();
assertThat(repo.isReleasesEnabled()).isTrue();
assertThat(repo.isSnapshotsEnabled()).isFalse();
}
@Test
void repositoryWithOnlySnapshots() {
MavenRepository repo = MavenRepository.withIdAndUrl("test", "https://repo.example.com").onlySnapshots().build();
assertThat(repo.isReleasesEnabled()).isFalse();
assertThat(repo.isSnapshotsEnabled()).isTrue();
}
@Test
void repositoryWithReleasesAndSnapshots() {
MavenRepository repo = MavenRepository.withIdAndUrl("test", "https://repo.example.com").releasesEnabled(true)
.snapshotsEnabled(true).build();
assertThat(repo.isReleasesEnabled()).isTrue();
assertThat(repo.isSnapshotsEnabled()).isTrue();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -124,8 +124,8 @@ class GroovyDslGradleBuildWriterTests {
@Test
void gradleBuildWithSnapshotRepository() {
GradleBuild build = new GradleBuild();
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.snapshotsEnabled(true));
build.repositories().add(
MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot").onlySnapshots());
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("repositories {", " maven { url 'https://repo.spring.io/snapshot' }",
"}");

View File

@@ -68,7 +68,7 @@ class GroovyDslGradleSettingsWriterTests {
GradleBuild build = new GradleBuild();
build.pluginRepositories()
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.name("Spring Snapshots").snapshotsEnabled(true));
.name("Spring Snapshots").onlySnapshots());
List<String> lines = generateSettings(build);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" maven { url 'https://repo.spring.io/snapshot' }", " gradlePluginPortal()", " }",

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -150,8 +150,8 @@ class KotlinDslGradleBuildWriterTests {
@Test
void gradleBuildWithSnapshotRepository() {
GradleBuild build = new GradleBuild();
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.snapshotsEnabled(true));
build.repositories().add(
MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot").onlySnapshots());
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("repositories {",
" maven { url = uri(\"https://repo.spring.io/snapshot\") }", "}");

View File

@@ -67,7 +67,7 @@ class KotlinDslGradleSettingsWriterTests {
GradleBuild build = new GradleBuild();
build.pluginRepositories()
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.name("Spring Snapshots").snapshotsEnabled(true));
.name("Spring Snapshots").onlySnapshots());
List<String> lines = generateSettings(build);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" maven { url = uri(\"https://repo.spring.io/snapshot\") }", " gradlePluginPortal()",

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -638,70 +638,104 @@ class MavenBuildWriterTests {
}
@Test
void pomWithRepository() {
void pomWithReleasesOnlyRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")
.name("Spring Milestones"));
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/repositories/repository/id").isEqualTo("spring-milestones");
assertThat(pom).textAtPath("/project/repositories/repository/name").isEqualTo("Spring Milestones");
assertThat(pom).textAtPath("/project/repositories/repository/url")
.isEqualTo("https://repo.spring.io/milestone");
assertThat(pom).nodeAtPath("/project/repositories/repository/snapshots").isNull();
NodeAssert repository = pom.nodeAtPath("/project/repositories/repository");
assertThat(repository).textAtPath("id").isEqualTo("spring-milestones");
assertThat(repository).textAtPath("name").isEqualTo("Spring Milestones");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.spring.io/milestone");
assertThat(repository).nodeAtPath("releases").isNull();
assertThat(repository).textAtPath("snapshots/enabled").isEqualTo("false");
assertThat(pom).nodeAtPath("/project/pluginRepositories").isNull();
});
}
@Test
void pomWithPluginRepository() {
void pomWithReleasesOnlyPluginRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.pluginRepositories().add(MavenRepository
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones"));
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/id")
.isEqualTo("spring-milestones");
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/name")
.isEqualTo("Spring Milestones");
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/url")
.isEqualTo("https://repo.spring.io/milestone");
assertThat(pom).nodeAtPath("/project/repositories/repository/snapshots").isNull();
NodeAssert repository = pom.nodeAtPath("/project/pluginRepositories/pluginRepository");
assertThat(repository).textAtPath("id").isEqualTo("spring-milestones");
assertThat(repository).textAtPath("name").isEqualTo("Spring Milestones");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.spring.io/milestone");
assertThat(repository).nodeAtPath("releases").isNull();
assertThat(repository).textAtPath("snapshots/enabled").isEqualTo("false");
assertThat(pom).nodeAtPath("/project/repositories").isNull();
});
}
@Test
void pomWithSnapshotRepository() {
void pomWithSnapshotsOnlyRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.name("Spring Snapshots").snapshotsEnabled(true));
.name("Spring Snapshots").onlySnapshots());
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/repositories/repository/id").isEqualTo("spring-snapshots");
assertThat(pom).textAtPath("/project/repositories/repository/name").isEqualTo("Spring Snapshots");
assertThat(pom).textAtPath("/project/repositories/repository/url")
.isEqualTo("https://repo.spring.io/snapshot");
assertThat(pom).textAtPath("/project/repositories/repository/snapshots/enabled").isEqualTo("true");
NodeAssert repository = pom.nodeAtPath("/project/repositories/repository");
assertThat(repository).textAtPath("id").isEqualTo("spring-snapshots");
assertThat(repository).textAtPath("name").isEqualTo("Spring Snapshots");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.spring.io/snapshot");
assertThat(repository).textAtPath("releases/enabled").isEqualTo("false");
assertThat(repository).nodeAtPath("snapshots").isNull();
assertThat(pom).nodeAtPath("/project/pluginRepositories").isNull();
});
}
@Test
void pomWithSnapshotPluginRepository() {
void pomWithSnapshotsOnlyPluginRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.pluginRepositories()
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.name("Spring Snapshots").snapshotsEnabled(true));
.name("Spring Snapshots").onlySnapshots());
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/id").isEqualTo("spring-snapshots");
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/name")
.isEqualTo("Spring Snapshots");
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/url")
.isEqualTo("https://repo.spring.io/snapshot");
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/snapshots/enabled")
.isEqualTo("true");
NodeAssert repository = pom.nodeAtPath("/project/pluginRepositories/pluginRepository");
assertThat(repository).textAtPath("id").isEqualTo("spring-snapshots");
assertThat(repository).textAtPath("name").isEqualTo("Spring Snapshots");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.spring.io/snapshot");
assertThat(repository).textAtPath("releases/enabled").isEqualTo("false");
assertThat(repository).nodeAtPath("snapshots").isNull();
assertThat(pom).nodeAtPath("/project/repositories").isNull();
});
}
@Test
void pomWithReleasesAndSnapshotsRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.repositories().add(MavenRepository.withIdAndUrl("example", "https://repo.example.com/")
.name("Example Repo").releasesEnabled(true).snapshotsEnabled(true));
generatePom(build, (pom) -> {
NodeAssert repository = pom.nodeAtPath("/project/repositories/repository");
assertThat(repository).textAtPath("id").isEqualTo("example");
assertThat(repository).textAtPath("name").isEqualTo("Example Repo");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.example.com/");
assertThat(repository).nodeAtPath("releases").isNull();
assertThat(repository).nodeAtPath("snapshots").isNull();
assertThat(pom).nodeAtPath("/project/pluginRepositories").isNull();
});
}
@Test
void pomWithReleasesAndSnapshotsPluginRepository() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.pluginRepositories().add(MavenRepository.withIdAndUrl("example", "https://repo.example.com/")
.name("Example Repo").releasesEnabled(true).snapshotsEnabled(true));
generatePom(build, (pom) -> {
NodeAssert repository = pom.nodeAtPath("/project/pluginRepositories/pluginRepository");
assertThat(repository).textAtPath("id").isEqualTo("example");
assertThat(repository).textAtPath("name").isEqualTo("Example Repo");
assertThat(repository).textAtPath("url").isEqualTo("https://repo.example.com/");
assertThat(repository).nodeAtPath("releases").isNull();
assertThat(repository).nodeAtPath("snapshots").isNull();
assertThat(pom).nodeAtPath("/project/repositories").isNull();
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -250,9 +250,9 @@ public class InitializrConfiguration {
public Env() {
try {
this.repositories.put("spring-snapshots",
new Repository("Spring Snapshots", new URL("https://repo.spring.io/snapshot"), true));
new Repository("Spring Snapshots", new URL("https://repo.spring.io/snapshot"), false, true));
this.repositories.put("spring-milestones",
new Repository("Spring Milestones", new URL("https://repo.spring.io/milestone"), false));
new Repository("Spring Milestones", new URL("https://repo.spring.io/milestone"), true, false));
}
catch (MalformedURLException ex) {
throw new IllegalStateException("Cannot parse URL", ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package io.spring.initializr.metadata;
import java.net.URL;
import java.util.StringJoiner;
/**
* Define a repository to be represented in the generated project if a dependency refers
@@ -30,14 +31,21 @@ public class Repository {
private URL url;
private boolean releasesEnabled = true;
private boolean snapshotsEnabled;
public Repository() {
}
public Repository(String name, URL url, boolean snapshotsEnabled) {
public Repository(String name, URL url) {
this(name, url, true, false);
}
public Repository(String name, URL url, boolean releasesEnabled, boolean snapshotsEnabled) {
this.name = name;
this.url = url;
this.releasesEnabled = releasesEnabled;
this.snapshotsEnabled = snapshotsEnabled;
}
@@ -57,6 +65,14 @@ public class Repository {
this.url = url;
}
public boolean isReleasesEnabled() {
return this.releasesEnabled;
}
public void setReleasesEnabled(boolean releasesEnabled) {
this.releasesEnabled = releasesEnabled;
}
public boolean isSnapshotsEnabled() {
return this.snapshotsEnabled;
}
@@ -85,6 +101,9 @@ public class Repository {
else if (!this.name.equals(other.name)) {
return false;
}
if (this.releasesEnabled != other.releasesEnabled) {
return false;
}
if (this.snapshotsEnabled != other.snapshotsEnabled) {
return false;
}
@@ -104,6 +123,7 @@ public class Repository {
final int prime = 31;
int result = 1;
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
result = prime * result + (this.releasesEnabled ? 1231 : 1237);
result = prime * result + (this.snapshotsEnabled ? 1231 : 1237);
result = prime * result + ((this.url == null) ? 0 : this.url.hashCode());
return result;
@@ -111,9 +131,9 @@ public class Repository {
@Override
public String toString() {
return "Repository [" + ((this.name != null) ? "name=" + this.name + ", " : "")
+ ((this.url != null) ? "url=" + this.url + ", " : "") + "snapshotsEnabled=" + this.snapshotsEnabled
+ "]";
return new StringJoiner(", ", Repository.class.getSimpleName() + "[", "]").add("name='" + this.name + "'")
.add("url=" + this.url).add("releasesEnabled=" + this.releasesEnabled)
.add("snapshotsEnabled=" + this.snapshotsEnabled).toString();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -96,7 +96,8 @@ public final class MetadataBuildItemMapper {
}
return io.spring.initializr.generator.buildsystem.MavenRepository
.withIdAndUrl(id, repository.getUrl().toExternalForm()).name(repository.getName())
.snapshotsEnabled(repository.isSnapshotsEnabled()).build();
.releasesEnabled(repository.isReleasesEnabled()).snapshotsEnabled(repository.isSnapshotsEnabled())
.build();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ class InitializrMetadataTests {
foo.setRepository("foo-repo");
addTestDependencyGroup(metadata, foo);
metadata.getConfiguration().getEnv().getRepositories().put("my-repo",
new Repository("repo", new URL("https://example.com/repo"), true));
new Repository("repo", new URL("https://example.com/repo")));
assertThatExceptionOfType(InvalidInitializrMetadataException.class).isThrownBy(metadata::validate)
.withMessageContaining("foo-repo").withMessageContaining("my-repo");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ class MetadataBuildItemResolverTests {
bom.getMappings().add(BillOfMaterials.Mapping.create("2.0.0.RELEASE", "1.1.0"));
metadata.getConfiguration().getEnv().getBoms().put("test-bom", bom);
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
new Repository("test", new URL("https://example.com/repo"), false));
new Repository("test", new URL("https://example.com/repo")));
metadata.validate();
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata, VERSION_2_0_0);
io.spring.initializr.generator.buildsystem.BillOfMaterials resolvedBom = resolver.resolveBom("test-bom");
@@ -132,24 +132,40 @@ class MetadataBuildItemResolverTests {
}
@Test
void resoleRepositoryWithMatchingEntry() throws MalformedURLException {
void resoleRepositoryWithMatchingReleasesOnlyRepository() throws MalformedURLException {
InitializrMetadata metadata = new InitializrMetadata();
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
new Repository("test", new URL("https://example.com/repo"), false));
new Repository("test", new URL("https://example.com/repo")));
metadata.validate();
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata, VERSION_2_0_0);
MavenRepository repository = resolver.resolveRepository("test-repo");
assertThat(repository.getId()).isEqualTo("test-repo");
assertThat(repository.getName()).isEqualTo("test");
assertThat(repository.getUrl()).isEqualTo("https://example.com/repo");
assertThat(repository.isReleasesEnabled()).isTrue();
assertThat(repository.isSnapshotsEnabled()).isFalse();
}
@Test
void resoleRepositoryWithMatchingSnapshotsOnlyRepository() throws MalformedURLException {
InitializrMetadata metadata = new InitializrMetadata();
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
new Repository("test", new URL("https://example.com/repo"), false, true));
metadata.validate();
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata, VERSION_2_0_0);
MavenRepository repository = resolver.resolveRepository("test-repo");
assertThat(repository.getId()).isEqualTo("test-repo");
assertThat(repository.getName()).isEqualTo("test");
assertThat(repository.getUrl()).isEqualTo("https://example.com/repo");
assertThat(repository.isReleasesEnabled()).isFalse();
assertThat(repository.isSnapshotsEnabled()).isTrue();
}
@Test
void resoleRepositoryWithNonMatchingEntry() throws MalformedURLException {
InitializrMetadata metadata = new InitializrMetadata();
metadata.getConfiguration().getEnv().getRepositories().put("test-repo",
new Repository("test", new URL("https://example.com/repo"), false));
new Repository("test", new URL("https://example.com/repo")));
metadata.validate();
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata, VERSION_2_0_0);
assertThat(resolver.resolveRepository("does-not-exist")).isNull();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ class DefaultDependencyMetadataProviderTests {
Dependency third = Dependency.withId("third", "org.foo", "third");
third.setRepository("repo-foo");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addRepository("repo-foo", "my-repo", "http://localhost", false)
.addReleasesRepository("repo-foo", "my-repo", "http://localhost")
.addDependencyGroup("test", first, second, third).build();
DependencyMetadata dependencyMetadata = this.provider.get(metadata, Version.parse("1.1.5.RELEASE"));
assertThat(dependencyMetadata.getDependencies()).hasSize(3);
@@ -155,9 +155,9 @@ class DefaultDependencyMetadataProviderTests {
"repo-foo", "repo-bar"));
bom.getMappings().add(BillOfMaterials.Mapping.create("1.1.0.RELEASE", "3.0.0.RELEASE", "repo-biz"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addBom("bom-foo", bom)
.addRepository("repo-foo", "foo", "http://localhost", false)
.addRepository("repo-bar", "bar", "http://localhost", false)
.addRepository("repo-biz", "biz", "http://localhost", false)
.addReleasesRepository("repo-foo", "foo", "http://localhost")
.addReleasesRepository("repo-bar", "bar", "http://localhost")
.addReleasesRepository("repo-biz", "biz", "http://localhost")
.addDependencyGroup("test", first, second, third).build();
return this.provider.get(metadata, Version.parse(bootVersion));
}

View File

@@ -74,27 +74,32 @@
"my-api-repo-1": {
"name": "repo1",
"url": "https://example.com/repo1",
"releasesEnabled": true,
"snapshotsEnabled": false
},
"my-api-repo-2": {
"name": "repo2",
"url": "https://example.com/repo2",
"releasesEnabled": true,
"snapshotsEnabled": false
},
"my-api-repo-3": {
"name": "repo3",
"url": "https://example.com/repo3",
"releasesEnabled": true,
"snapshotsEnabled": false
},
"spring-milestones": {
"name": "Spring Milestones",
"snapshotsEnabled": false,
"url": "https://repo.spring.io/milestone"
"url": "https://repo.spring.io/milestone",
"releasesEnabled": true,
"snapshotsEnabled": false
},
"spring-snapshots": {
"name": "Spring Snapshots",
"snapshotsEnabled": true,
"url": "https://repo.spring.io/snapshot"
"url": "https://repo.spring.io/snapshot",
"releasesEnabled": false,
"snapshotsEnabled": true
}
},
"boms": {