Skip to content

Commit b235939

Browse files
committed
Fixes 1182 and 1184: Buges fix (update scope determination) + slight refactoring, fixed
1 parent 8a98bef commit b235939

File tree

16 files changed

+486
-311
lines changed

16 files changed

+486
-311
lines changed

versions-common/src/main/java/org/codehaus/mojo/versions/utils/CoreExtensionUtils.java

-65
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

versions-common/src/test/java/org/codehaus/mojo/versions/utils/CoreExtensionUtilsTest.java versions-common/src/test/java/org/codehaus/mojo/versions/utils/ExtensionUtilsTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
import static org.mockito.Mockito.when;
3636

3737
/**
38-
* Unit tests for {@link CoreExtensionUtils}
38+
* Unit tests for {@link ExtensionUtils}
3939
*
4040
* @author Andrzej Jarmoniuk
4141
*/
42-
class CoreExtensionUtilsTest {
42+
class ExtensionUtilsTest {
4343

4444
@Test
4545
void testNoExtensions() throws IOException, XMLStreamException {
@@ -49,7 +49,7 @@ void testNoExtensions() throws IOException, XMLStreamException {
4949
new File("src/test/resources/org/codehaus/mojo/versions/utils/core-extensions/no-extensions"));
5050
MavenSession session = mock(MavenSession.class);
5151
when(session.getCurrentProject()).thenReturn(project);
52-
assertThat(CoreExtensionUtils.getCoreExtensions(project).findAny(), is(Optional.empty()));
52+
assertThat(ExtensionUtils.getCoreExtensions(project).findAny(), is(Optional.empty()));
5353
}
5454

5555
@Test
@@ -59,8 +59,7 @@ void testExtensionsFound() throws IOException, XMLStreamException {
5959
.thenReturn(new File("src/test/resources/org/codehaus/mojo/versions/utils/core-extensions"));
6060
MavenSession session = mock(MavenSession.class);
6161
when(session.getCurrentProject()).thenReturn(project);
62-
Set<Extension> extensions =
63-
CoreExtensionUtils.getCoreExtensions(project).collect(Collectors.toSet());
62+
Set<Extension> extensions = ExtensionUtils.getCoreExtensions(project).collect(Collectors.toSet());
6463
assertThat(
6564
extensions,
6665
hasItems(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates
2+
invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>localhost</groupId>
6+
<artifactId>it-display-extension-updates-002</artifactId>
7+
8+
<version>1.0</version>
9+
<packaging>pom</packaging>
10+
11+
<build>
12+
<extensions>
13+
<extension>
14+
<groupId>localhost</groupId>
15+
<artifactId>dummy-api</artifactId>
16+
<version>1.0</version>
17+
</extension>
18+
</extensions>
19+
</build>
20+
21+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def output = new File( basedir, "output.txt").text
2+
assert !(output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*\Q1.0\E/)
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.1.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.1.0 https://maven.apache.org/xsd/core-extensions-1.1.0.xsd">
5+
<extension>
6+
<groupId>localhost</groupId>
7+
<artifactId>dummy-api</artifactId>
8+
<version>1.0</version>
9+
</extension>
10+
</extensions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates
2+
invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>localhost</groupId>
6+
<artifactId>it-display-extension-updates-002</artifactId>
7+
8+
<version>1.0</version>
9+
<packaging>pom</packaging>
10+
11+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def output = new File( basedir, "output.txt").text
2+
assert !(output =~ /\Qlocalhost:dummy-api\E\s*\.*\s*\Q1.0\E/)
3+

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public abstract class AbstractVersionsDisplayMojo extends AbstractVersionsUpdate
4646

4747
private static final int DEFAULT_OUTPUT_LINE_WIDTH = 80;
4848

49+
/**
50+
* The width to pad info messages.
51+
*
52+
* @since 1.0-alpha-1
53+
*/
54+
static final int INFO_PAD_SIZE = 72;
55+
4956
/**
5057
* If specified then the display output will be sent to the specified file.
5158
*

0 commit comments

Comments
 (0)