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.
@@ -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();