|
| 1 | +/* |
| 2 | + * Copyright 2012 - present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.start.site; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import io.spring.initializr.generator.version.Version; |
| 26 | +import io.spring.initializr.generator.version.Version.Qualifier; |
| 27 | +import io.spring.initializr.generator.version.VersionParser; |
| 28 | +import io.spring.initializr.generator.version.VersionRange; |
| 29 | +import io.spring.initializr.metadata.BillOfMaterials; |
| 30 | +import io.spring.initializr.metadata.Dependency; |
| 31 | +import io.spring.initializr.metadata.InitializrMetadata; |
| 32 | +import io.spring.initializr.metadata.Repository; |
| 33 | +import org.eclipse.aether.repository.RemoteRepository; |
| 34 | + |
| 35 | +class Repositories { |
| 36 | + |
| 37 | + private static final VersionRange SPRING_BOOT_4_0_OR_LATER = VersionParser.DEFAULT.parseRange("4.0.0-M1"); |
| 38 | + |
| 39 | + private final InitializrMetadata metadata; |
| 40 | + |
| 41 | + private final Version springBootVersion; |
| 42 | + |
| 43 | + private final Set<String> bootRepositories; |
| 44 | + |
| 45 | + Repositories(InitializrMetadata metadata, Version springBootVersion) { |
| 46 | + this.metadata = metadata; |
| 47 | + this.springBootVersion = springBootVersion; |
| 48 | + this.bootRepositories = Set.copyOf(getBootRepositories()); |
| 49 | + } |
| 50 | + |
| 51 | + List<RemoteRepository> getRepositories(Dependency dependency, List<BillOfMaterials> boms) { |
| 52 | + Map<String, RemoteRepository> repositories = new HashMap<>(); |
| 53 | + repositories.put("central", DependencyResolver.mavenCentral); |
| 54 | + addDependencyRepositories(dependency, repositories); |
| 55 | + addBomRepositories(boms, repositories); |
| 56 | + addBootRepositories(repositories); |
| 57 | + return List.copyOf(repositories.values()); |
| 58 | + } |
| 59 | + |
| 60 | + private void addBootRepositories(Map<String, RemoteRepository> repositories) { |
| 61 | + for (String repository : this.bootRepositories) { |
| 62 | + repositories.computeIfAbsent(repository, this::repositoryForId); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void addBomRepositories(List<BillOfMaterials> boms, Map<String, RemoteRepository> repositories) { |
| 67 | + for (BillOfMaterials bom : boms) { |
| 68 | + for (String repository : bom.getRepositories()) { |
| 69 | + repositories.computeIfAbsent(repository, this::repositoryForId); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void addDependencyRepositories(Dependency dependency, Map<String, RemoteRepository> repositories) { |
| 75 | + String dependencyRepository = dependency.getRepository(); |
| 76 | + if (dependencyRepository != null) { |
| 77 | + repositories.computeIfAbsent(dependencyRepository, this::repositoryForId); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private RemoteRepository repositoryForId(String id) { |
| 82 | + Repository repository = this.metadata.getConfiguration().getEnv().getRepositories().get(id); |
| 83 | + return DependencyResolver.createRemoteRepository(id, repository.getUrl().toExternalForm(), |
| 84 | + repository.isSnapshotsEnabled()); |
| 85 | + } |
| 86 | + |
| 87 | + private Set<String> getBootRepositories() { |
| 88 | + Set<String> result = new HashSet<>(); |
| 89 | + switch (getReleaseType()) { |
| 90 | + case MILESTONE -> addMilestoneRepositoryIfNeeded(result); |
| 91 | + case SNAPSHOT -> { |
| 92 | + if (isMaintenanceRelease()) { |
| 93 | + addSnapshotRepository(result); |
| 94 | + } |
| 95 | + else { |
| 96 | + addMilestoneRepositoryIfNeeded(result); |
| 97 | + addSnapshotRepository(result); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + private boolean isMaintenanceRelease() { |
| 105 | + Integer patch = this.springBootVersion.getPatch(); |
| 106 | + return patch != null && patch > 0; |
| 107 | + } |
| 108 | + |
| 109 | + private void addSnapshotRepository(Set<String> repositories) { |
| 110 | + repositories.add("spring-snapshots"); |
| 111 | + } |
| 112 | + |
| 113 | + private void addMilestoneRepositoryIfNeeded(Set<String> repositories) { |
| 114 | + if (SPRING_BOOT_4_0_OR_LATER.match(this.springBootVersion)) { |
| 115 | + // Spring Boot 4.0 and up publishes milestones to Maven Central |
| 116 | + return; |
| 117 | + } |
| 118 | + repositories.add("spring-milestones"); |
| 119 | + } |
| 120 | + |
| 121 | + private ReleaseType getReleaseType() { |
| 122 | + Qualifier qualifier = this.springBootVersion.getQualifier(); |
| 123 | + if (qualifier == null) { |
| 124 | + return ReleaseType.GA; |
| 125 | + } |
| 126 | + String id = qualifier.getId(); |
| 127 | + if ("RELEASE".equals(id)) { |
| 128 | + return ReleaseType.GA; |
| 129 | + } |
| 130 | + if (id.contains("SNAPSHOT")) { |
| 131 | + return ReleaseType.SNAPSHOT; |
| 132 | + } |
| 133 | + return ReleaseType.MILESTONE; |
| 134 | + } |
| 135 | + |
| 136 | + private enum ReleaseType { |
| 137 | + |
| 138 | + GA, MILESTONE, SNAPSHOT |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments