|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 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 | + * http://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 org.springframework.boot.aether; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.net.URI; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.apache.maven.settings.Profile; |
| 25 | +import org.apache.maven.settings.Repository; |
| 26 | +import org.codehaus.plexus.interpolation.InterpolationException; |
| 27 | +import org.codehaus.plexus.interpolation.Interpolator; |
| 28 | +import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; |
| 29 | +import org.codehaus.plexus.interpolation.RegexBasedInterpolator; |
| 30 | + |
| 31 | +import org.springframework.boot.aether.maven.MavenSettings; |
| 32 | +import org.springframework.boot.aether.maven.MavenSettingsReader; |
| 33 | +import org.springframework.util.StringUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * Factory used to create {@link RepositoryConfiguration}s. |
| 37 | + * |
| 38 | + * @author Andy Wilkinson |
| 39 | + * @author Dave Syer |
| 40 | + */ |
| 41 | +public final class RepositoryConfigurationFactory { |
| 42 | + |
| 43 | + private static final RepositoryConfiguration MAVEN_CENTRAL = new RepositoryConfiguration( |
| 44 | + "central", URI.create("http://repo1.maven.org/maven2/"), false); |
| 45 | + |
| 46 | + private static final RepositoryConfiguration SPRING_MILESTONE = new RepositoryConfiguration( |
| 47 | + "spring-milestone", URI.create("http://repo.spring.io/milestone"), false); |
| 48 | + |
| 49 | + private static final RepositoryConfiguration SPRING_SNAPSHOT = new RepositoryConfiguration( |
| 50 | + "spring-snapshot", URI.create("http://repo.spring.io/snapshot"), true); |
| 51 | + |
| 52 | + private RepositoryConfigurationFactory() { |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Create a new default repository configuration. |
| 57 | + * @return the newly-created default repository configuration |
| 58 | + */ |
| 59 | + public static List<RepositoryConfiguration> createDefaultRepositoryConfiguration() { |
| 60 | + MavenSettings mavenSettings = new MavenSettingsReader().readSettings(); |
| 61 | + List<RepositoryConfiguration> repositoryConfiguration = new ArrayList<RepositoryConfiguration>(); |
| 62 | + repositoryConfiguration.add(MAVEN_CENTRAL); |
| 63 | + if (!Boolean.getBoolean("disableSpringSnapshotRepos")) { |
| 64 | + repositoryConfiguration.add(SPRING_MILESTONE); |
| 65 | + repositoryConfiguration.add(SPRING_SNAPSHOT); |
| 66 | + } |
| 67 | + addDefaultCacheAsRepository(mavenSettings.getLocalRepository(), |
| 68 | + repositoryConfiguration); |
| 69 | + addActiveProfileRepositories(mavenSettings.getActiveProfiles(), |
| 70 | + repositoryConfiguration); |
| 71 | + return repositoryConfiguration; |
| 72 | + } |
| 73 | + |
| 74 | + private static void addDefaultCacheAsRepository(String localRepository, |
| 75 | + List<RepositoryConfiguration> repositoryConfiguration) { |
| 76 | + RepositoryConfiguration repository = new RepositoryConfiguration("local", |
| 77 | + getLocalRepositoryDirectory(localRepository).toURI(), true); |
| 78 | + if (!repositoryConfiguration.contains(repository)) { |
| 79 | + repositoryConfiguration.add(0, repository); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void addActiveProfileRepositories(List<Profile> activeProfiles, |
| 84 | + List<RepositoryConfiguration> configurations) { |
| 85 | + for (Profile activeProfile : activeProfiles) { |
| 86 | + Interpolator interpolator = new RegexBasedInterpolator(); |
| 87 | + interpolator.addValueSource( |
| 88 | + new PropertiesBasedValueSource(activeProfile.getProperties())); |
| 89 | + for (Repository repository : activeProfile.getRepositories()) { |
| 90 | + configurations.add(getRepositoryConfiguration(interpolator, repository)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static RepositoryConfiguration getRepositoryConfiguration( |
| 96 | + Interpolator interpolator, Repository repository) { |
| 97 | + String name = interpolate(interpolator, repository.getId()); |
| 98 | + String url = interpolate(interpolator, repository.getUrl()); |
| 99 | + boolean snapshotsEnabled = false; |
| 100 | + if (repository.getSnapshots() != null) { |
| 101 | + snapshotsEnabled = repository.getSnapshots().isEnabled(); |
| 102 | + } |
| 103 | + return new RepositoryConfiguration(name, URI.create(url), snapshotsEnabled); |
| 104 | + } |
| 105 | + |
| 106 | + private static String interpolate(Interpolator interpolator, String value) { |
| 107 | + try { |
| 108 | + return interpolator.interpolate(value); |
| 109 | + } |
| 110 | + catch (InterpolationException ex) { |
| 111 | + return value; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static File getLocalRepositoryDirectory(String localRepository) { |
| 116 | + if (StringUtils.hasText(localRepository)) { |
| 117 | + return new File(localRepository); |
| 118 | + } |
| 119 | + return new File(getM2HomeDirectory(), "repository"); |
| 120 | + } |
| 121 | + |
| 122 | + private static File getM2HomeDirectory() { |
| 123 | + String mavenRoot = System.getProperty("maven.home"); |
| 124 | + if (StringUtils.hasLength(mavenRoot)) { |
| 125 | + return new File(mavenRoot); |
| 126 | + } |
| 127 | + return new File(System.getProperty("user.home"), ".m2"); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments