|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 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 org.springframework.boot.build.docs; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.gradle.api.Action; |
| 25 | +import org.gradle.api.file.FileCollection; |
| 26 | +import org.gradle.api.tasks.javadoc.Javadoc; |
| 27 | +import org.gradle.external.javadoc.StandardJavadocDocletOptions; |
| 28 | + |
| 29 | +import org.springframework.boot.build.bom.ResolvedBom; |
| 30 | +import org.springframework.boot.build.bom.ResolvedBom.JavadocLink; |
| 31 | + |
| 32 | +/** |
| 33 | + * An {@link Action} to configure the links option of a {@link Javadoc} task. |
| 34 | + * |
| 35 | + * @author Andy Wilkinson |
| 36 | + */ |
| 37 | +public class ConfigureJavadocLinks implements Action<Javadoc> { |
| 38 | + |
| 39 | + private final FileCollection resolvedBoms; |
| 40 | + |
| 41 | + private final Collection<String> includedLibraries; |
| 42 | + |
| 43 | + public ConfigureJavadocLinks(FileCollection resolvedBoms, Collection<String> includedLibraries) { |
| 44 | + this.resolvedBoms = resolvedBoms; |
| 45 | + this.includedLibraries = includedLibraries; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void execute(Javadoc javadoc) { |
| 50 | + javadoc.options((options) -> { |
| 51 | + if (options instanceof StandardJavadocDocletOptions standardOptions) { |
| 52 | + configureLinks(standardOptions); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private void configureLinks(StandardJavadocDocletOptions options) { |
| 58 | + ResolvedBom resolvedBom = ResolvedBom.readFrom(this.resolvedBoms.getSingleFile()); |
| 59 | + List<String> links = new ArrayList<>(); |
| 60 | + links.add("https://docs.oracle.com/en/java/javase/17/docs/api/"); |
| 61 | + links.add("https://jakarta.ee/specifications/platform/9/apidocs/"); |
| 62 | + resolvedBom.libraries() |
| 63 | + .stream() |
| 64 | + .filter((candidate) -> this.includedLibraries.contains(candidate.name())) |
| 65 | + .flatMap((library) -> library.links().javadoc().stream()) |
| 66 | + .map(JavadocLink::uri) |
| 67 | + .map(URI::toString) |
| 68 | + .forEach(links::add); |
| 69 | + options.setLinks(links); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments