|
| 1 | +package org.codehaus.mojo.versions.utils; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright MojoHaus and Contributors |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import javax.xml.stream.XMLStreamException; |
| 20 | +import javax.xml.transform.TransformerException; |
| 21 | + |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.Reader; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import org.apache.commons.lang3.tuple.Pair; |
| 35 | +import org.apache.maven.model.Build; |
| 36 | +import org.apache.maven.model.Extension; |
| 37 | +import org.apache.maven.model.Model; |
| 38 | +import org.apache.maven.plugin.logging.Log; |
| 39 | +import org.apache.maven.project.MavenProject; |
| 40 | +import org.codehaus.mojo.versions.api.PomHelper; |
| 41 | +import org.codehaus.mojo.versions.model.io.stax.CoreExtensionsStaxReader; |
| 42 | +import org.codehaus.mojo.versions.rewriting.MutableXMLStreamReader; |
| 43 | + |
| 44 | +import static java.util.Optional.ofNullable; |
| 45 | + |
| 46 | +/** |
| 47 | + * Utilities for reading and handling extensions. |
| 48 | + */ |
| 49 | +public final class ExtensionUtils { |
| 50 | + /** |
| 51 | + * Reads the core extensions configured for the given project |
| 52 | + * from the {@code ${project}/.mvn/extensions.xml} file. |
| 53 | + * |
| 54 | + * @param project {@link MavenProject} instance |
| 55 | + * @return stream of core extensions defined in the {@code ${project}/.mvn/extensions.xml} file |
| 56 | + * @throws IOException thrown if a file I/O operation fails |
| 57 | + * @throws XMLStreamException thrown if the file cannot be parsed |
| 58 | + * @since 2.15.0 |
| 59 | + */ |
| 60 | + public static Stream<Extension> getCoreExtensions(MavenProject project) throws IOException, XMLStreamException { |
| 61 | + Path extensionsFile = project.getBasedir().toPath().resolve(".mvn/extensions.xml"); |
| 62 | + if (!Files.isRegularFile(extensionsFile)) { |
| 63 | + return Stream.empty(); |
| 64 | + } |
| 65 | + |
| 66 | + try (Reader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(extensionsFile)))) { |
| 67 | + return new CoreExtensionsStaxReader() |
| 68 | + .read(reader).getExtensions().stream().map(ex -> ExtensionBuilder.newBuilder() |
| 69 | + .withGroupId(ex.getGroupId()) |
| 70 | + .withArtifactId(ex.getArtifactId()) |
| 71 | + .withVersion(ex.getVersion()) |
| 72 | + .build()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns a stream of build extensions configured for the given project |
| 78 | + * @param project {@link MavenProject} instance |
| 79 | + * @param log {@link Log} instance |
| 80 | + * @param interpolateProperties when {@code false}, will return extensions based on raw model, otherwise will |
| 81 | + * process the interpolated model |
| 82 | + * @return stream of build extensions |
| 83 | + * @throws IOException if the model file can't be read |
| 84 | + * @throws XMLStreamException if the model file can't be parsed |
| 85 | + * @throws TransformerException if the model file can't be parsed |
| 86 | + */ |
| 87 | + public static Stream<Extension> getBuildExtensions(MavenProject project, Log log, boolean interpolateProperties) |
| 88 | + throws XMLStreamException, IOException, TransformerException { |
| 89 | + if (interpolateProperties) { |
| 90 | + return getInterpolatedBuildExtensions(project, log); |
| 91 | + } else { |
| 92 | + return PomHelper.getChildModels(project, log).values().stream() |
| 93 | + .map(Model::getBuild) |
| 94 | + .filter(Objects::nonNull) |
| 95 | + .map(Build::getExtensions) |
| 96 | + .map(List::stream) |
| 97 | + .reduce(Stream::concat) |
| 98 | + .orElse(Stream.empty()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static Stream<Extension> getInterpolatedBuildExtensions(MavenProject project, Log log) |
| 103 | + throws IOException, XMLStreamException, TransformerException { |
| 104 | + MutableXMLStreamReader pomReader = |
| 105 | + new MutableXMLStreamReader(project.getFile().toPath()); |
| 106 | + ModelNode rootNode = new ModelNode(PomHelper.getRawModel(pomReader.getSource(), project.getFile()), pomReader); |
| 107 | + List<ModelNode> rawModels = PomHelper.getRawModelTree(rootNode, log); |
| 108 | + return rawModels.stream() |
| 109 | + .filter(node -> Objects.nonNull(node.getModel())) |
| 110 | + .filter(node -> ofNullable(node.getModel().getBuild()) |
| 111 | + .map(Build::getExtensions) |
| 112 | + .map(list -> !list.isEmpty()) |
| 113 | + .orElse(false)) |
| 114 | + .map(node -> Pair.of(node.getModel().getBuild().getExtensions(), getNodeProperties(node))) |
| 115 | + .flatMap(pair -> pair.getLeft().stream().map(e -> Pair.of(e, pair.getRight()))) |
| 116 | + .map(pair -> ExtensionBuilder.newBuilder() |
| 117 | + .withGroupId(PomHelper.evaluate(pair.getLeft().getGroupId(), pair.getRight(), log)) |
| 118 | + .withArtifactId(PomHelper.evaluate(pair.getLeft().getArtifactId(), pair.getRight(), log)) |
| 119 | + .withVersion(PomHelper.evaluate(pair.getLeft().getVersion(), pair.getRight(), log)) |
| 120 | + .build()); |
| 121 | + } |
| 122 | + |
| 123 | + private static Map<String, String> getNodeProperties(ModelNode node) { |
| 124 | + Map<String, String> properties = new HashMap<>(); |
| 125 | + for (ModelNode p = node; p != null; p = p.getParent().orElse(null)) { |
| 126 | + p.getModel() |
| 127 | + .getProperties() |
| 128 | + .forEach((key, value) -> properties.putIfAbsent(String.valueOf(key), String.valueOf(value))); |
| 129 | + } |
| 130 | + return properties; |
| 131 | + } |
| 132 | +} |
0 commit comments