Skip to content

Commit 3073624

Browse files
committed
Renamed class, formatter can be made functional with maven 3.x
1 parent 0d03b43 commit 3073624

8 files changed

+187
-228
lines changed

src/main/java/org/apache/maven/plugins/help/DefaultInputLocationFormatter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.apache.maven.plugins.help;
2-
31
/*
42
* Licensed to the Apache Software Foundation (ASF) under one
53
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
1816
* specific language governing permissions and limitations
1917
* under the License.
2018
*/
19+
package org.apache.maven.plugins.help;
2120

2221
import org.apache.maven.model.InputLocation;
2322
import org.apache.maven.model.InputSource;
@@ -26,21 +25,18 @@
2625
/**
2726
* Maven 3.x-based implementation of {@link InputLocation.StringFormatter}.
2827
*/
29-
public class DefaultInputLocationFormatter extends InputLocation.StringFormatter
30-
{
28+
public class DefaultInputLocationFormatter extends InputLocation.StringFormatter {
3129
@Override
32-
public String toString( InputLocation location )
33-
{
30+
public String toString(InputLocation location) {
3431
InputSource source = location.getSource();
3532

3633
String s = source.getModelId(); // by default, display modelId
3734

38-
if ( StringUtils.isBlank( s ) || s.contains( "[unknown-version]" ) )
39-
{
35+
if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
4036
// unless it is blank or does not provide version information
4137
s = source.toString();
4238
}
4339

44-
return '}' + s + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" ) + ' ';
40+
return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + ' ';
4541
}
4642
}

src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.util.List;
2525
import java.util.Properties;
2626

27-
import org.apache.maven.model.InputLocation;
28-
import org.apache.maven.model.InputSource;
2927
import org.apache.maven.model.Model;
3028
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
3129
import org.apache.maven.model.io.xpp3.MavenXpp3WriterEx;
@@ -175,7 +173,7 @@ private void writeEffectivePom(MavenProject project, XMLWriter writer) throws Mo
175173
try {
176174
if (verbose) {
177175
MavenXpp3WriterEx mavenXpp3WriterEx = new MavenXpp3WriterEx();
178-
mavenXpp3WriterEx.setStringFormatter(new InputLocationStringFormatter());
176+
mavenXpp3WriterEx.setStringFormatter(InputLocationFormatterFactory.produce(getLog(), project));
179177
mavenXpp3WriterEx.write(sWriter, pom);
180178
} else {
181179
new MavenXpp3Writer().write(sWriter, pom);
@@ -202,20 +200,4 @@ private static void cleanModel(Model pom) {
202200
properties.putAll(pom.getProperties());
203201
pom.setProperties(properties);
204202
}
205-
206-
private static class InputLocationStringFormatter extends InputLocation.StringFormatter {
207-
@Override
208-
public String toString(InputLocation location) {
209-
InputSource source = location.getSource();
210-
211-
String s = source.getModelId(); // by default, display modelId
212-
213-
if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
214-
// unless it is blank or does not provide version information
215-
s = source.toString();
216-
}
217-
218-
return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + ' ';
219-
}
220-
}
221203
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. 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,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.help;
20+
21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.InvocationTargetException;
23+
import java.lang.reflect.Method;
24+
import java.util.Map;
25+
import java.util.Set;
26+
27+
import org.apache.maven.model.Dependency;
28+
import org.apache.maven.model.InputLocation;
29+
import org.apache.maven.model.InputSource;
30+
import org.apache.maven.project.MavenProject;
31+
import org.codehaus.plexus.util.StringUtils;
32+
33+
/**
34+
* Implementation of {@link InputLocation.StringFormatter}. Enhances the default implementation with support for
35+
* following "references" (caused by e.g. dependency management imports).
36+
*/
37+
public class ImportedFromLocationFormatter extends InputLocation.StringFormatter {
38+
private final Method getImportedFromMethod;
39+
private final MavenProject project;
40+
41+
public ImportedFromLocationFormatter(final Method getImportedFromMethod, final MavenProject project) {
42+
this.getImportedFromMethod = getImportedFromMethod;
43+
this.project = project;
44+
}
45+
46+
@Override
47+
public String toString(InputLocation location) {
48+
InputSource source = location.getSource();
49+
50+
String s = source.getModelId(); // by default, display modelId
51+
52+
if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
53+
// unless it is blank or does not provide version information
54+
s = source.toString();
55+
}
56+
57+
InputLocation importedFrom = getImportedFrom(location);
58+
59+
StringBuilder p = new StringBuilder();
60+
61+
while (importedFrom != null) {
62+
p.append(" from ").append(importedFrom.getSource().getModelId());
63+
importedFrom = getImportedFrom(importedFrom);
64+
}
65+
66+
return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + p;
67+
}
68+
69+
protected InputLocation getImportedFrom(final InputLocation location) {
70+
try {
71+
InputLocation result = (InputLocation) getImportedFromMethod.invoke(location);
72+
73+
if (result == null && project != null) {
74+
for (Dependency dependency : project.getDependencyManagement().getDependencies()) {
75+
// Until a new maven api model is released, we need to use reflection to access the locations
76+
Set<?> locationKeys = getLocationKeys(dependency);
77+
for (Object key : locationKeys) {
78+
if (!(key instanceof String)) {
79+
throw new RuntimeException(
80+
"Expected a String, got " + key.getClass().getName());
81+
}
82+
83+
InputLocation dependencyLocation = dependency.getLocation(key);
84+
if (dependencyLocation != null
85+
&& dependencyLocation.toString().equals(location.toString())) {
86+
result = (InputLocation) Dependency.class
87+
.getMethod("getImportedFrom")
88+
.invoke(dependency);
89+
break;
90+
}
91+
}
92+
}
93+
}
94+
95+
return result;
96+
} catch (IllegalAccessException
97+
| InvocationTargetException
98+
| NoSuchMethodException
99+
| NoSuchFieldException
100+
| ClassNotFoundException e) {
101+
throw new RuntimeException(e);
102+
}
103+
}
104+
105+
private Set<?> getLocationKeys(Dependency dependency)
106+
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
107+
Field delegateField = Class.forName("org.apache.maven.model.BaseObject").getDeclaredField("delegate");
108+
delegateField.setAccessible(true);
109+
Object delegate = delegateField.get(dependency);
110+
delegateField.setAccessible(false);
111+
112+
Field locationsField = delegate.getClass().getDeclaredField("locations");
113+
locationsField.setAccessible(true);
114+
Object locations = locationsField.get(delegate);
115+
locationsField.setAccessible(false);
116+
117+
if (!(locations instanceof Map)) {
118+
throw new RuntimeException(
119+
"Expected a Map, got " + locations.getClass().getName());
120+
}
121+
122+
return ((Map<?, ?>) locations).keySet();
123+
}
124+
}

src/main/java/org/apache/maven/plugins/help/InputLocationFormatterFactory.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.apache.maven.plugins.help;
2-
31
/*
42
* Licensed to the Apache Software Foundation (ASF) under one
53
* or more contributor license agreements. See the NOTICE file
@@ -18,33 +16,29 @@
1816
* specific language governing permissions and limitations
1917
* under the License.
2018
*/
19+
package org.apache.maven.plugins.help;
20+
21+
import java.lang.reflect.Method;
2122

2223
import org.apache.maven.model.InputLocation;
2324
import org.apache.maven.plugin.logging.Log;
2425
import org.apache.maven.project.MavenProject;
2526

26-
import java.lang.reflect.Method;
27-
2827
/**
2928
* Selects the most suitable implementation for {@link InputLocation.StringFormatter}.
3029
*/
31-
public class InputLocationFormatterFactory
32-
{
30+
public class InputLocationFormatterFactory {
3331
static Class<?> inputLocationClass = InputLocation.class;
3432

35-
public static InputLocation.StringFormatter produce( final Log log, final MavenProject project )
36-
{
37-
try
38-
{
33+
public static InputLocation.StringFormatter produce(final Log log, final MavenProject project) {
34+
try {
3935
// This method was introduced in Maven 4.
40-
Method getImportedFromMethod = inputLocationClass.getDeclaredMethod( "getImportedFrom" );
41-
return new Maven4InputLocationFormatter( getImportedFromMethod, project );
42-
}
43-
catch ( NoSuchMethodException nsme )
44-
{
36+
Method getImportedFromMethod = inputLocationClass.getDeclaredMethod("getImportedFrom");
37+
return new ImportedFromLocationFormatter(getImportedFromMethod, project);
38+
} catch (NoSuchMethodException nsme) {
4539
// Fallback to pre-Maven 4 implementation.
46-
log.info( "Unable to print chain of POM imports, falling back to printing the source POM "
47-
+ "without import information. This feature is available in Maven 4.0.0+." );
40+
log.info("Unable to print chain of POM imports, falling back to printing the source POM "
41+
+ "without import information. This feature is available in Maven 4.0.0+.");
4842
return new DefaultInputLocationFormatter();
4943
}
5044
}

src/main/java/org/apache/maven/plugins/help/Maven4InputLocationFormatter.java

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)