Skip to content

Commit

Permalink
Renamed class, formatter can be made functional with maven 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
juulhobert committed Mar 15, 2024
1 parent 0d03b43 commit 3073624
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package org.apache.maven.plugins.help;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.help;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
Expand All @@ -26,21 +25,18 @@
/**
* Maven 3.x-based implementation of {@link InputLocation.StringFormatter}.
*/
public class DefaultInputLocationFormatter extends InputLocation.StringFormatter
{
public class DefaultInputLocationFormatter extends InputLocation.StringFormatter {
@Override
public String toString( InputLocation location )
{
public String toString(InputLocation location) {
InputSource source = location.getSource();

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

if ( StringUtils.isBlank( s ) || s.contains( "[unknown-version]" ) )
{
if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
// unless it is blank or does not provide version information
s = source.toString();
}

return '}' + s + ( ( location.getLineNumber() >= 0 ) ? ", line " + location.getLineNumber() : "" ) + ' ';
return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + ' ';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import java.util.List;
import java.util.Properties;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.io.xpp3.MavenXpp3WriterEx;
Expand Down Expand Up @@ -175,7 +173,7 @@ private void writeEffectivePom(MavenProject project, XMLWriter writer) throws Mo
try {
if (verbose) {
MavenXpp3WriterEx mavenXpp3WriterEx = new MavenXpp3WriterEx();
mavenXpp3WriterEx.setStringFormatter(new InputLocationStringFormatter());
mavenXpp3WriterEx.setStringFormatter(InputLocationFormatterFactory.produce(getLog(), project));
mavenXpp3WriterEx.write(sWriter, pom);
} else {
new MavenXpp3Writer().write(sWriter, pom);
Expand All @@ -202,20 +200,4 @@ private static void cleanModel(Model pom) {
properties.putAll(pom.getProperties());
pom.setProperties(properties);
}

private static class InputLocationStringFormatter extends InputLocation.StringFormatter {
@Override
public String toString(InputLocation location) {
InputSource source = location.getSource();

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

if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
// unless it is blank or does not provide version information
s = source.toString();
}

return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + ' ';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.help;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;

/**
* Implementation of {@link InputLocation.StringFormatter}. Enhances the default implementation with support for
* following "references" (caused by e.g. dependency management imports).
*/
public class ImportedFromLocationFormatter extends InputLocation.StringFormatter {
private final Method getImportedFromMethod;
private final MavenProject project;

public ImportedFromLocationFormatter(final Method getImportedFromMethod, final MavenProject project) {
this.getImportedFromMethod = getImportedFromMethod;
this.project = project;
}

@Override
public String toString(InputLocation location) {
InputSource source = location.getSource();

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

if (StringUtils.isBlank(s) || s.contains("[unknown-version]")) {
// unless it is blank or does not provide version information
s = source.toString();
}

InputLocation importedFrom = getImportedFrom(location);

StringBuilder p = new StringBuilder();

while (importedFrom != null) {
p.append(" from ").append(importedFrom.getSource().getModelId());
importedFrom = getImportedFrom(importedFrom);
}

return '}' + s + ((location.getLineNumber() >= 0) ? ", line " + location.getLineNumber() : "") + p;
}

protected InputLocation getImportedFrom(final InputLocation location) {
try {
InputLocation result = (InputLocation) getImportedFromMethod.invoke(location);

if (result == null && project != null) {
for (Dependency dependency : project.getDependencyManagement().getDependencies()) {
// Until a new maven api model is released, we need to use reflection to access the locations
Set<?> locationKeys = getLocationKeys(dependency);
for (Object key : locationKeys) {
if (!(key instanceof String)) {
throw new RuntimeException(
"Expected a String, got " + key.getClass().getName());
}

InputLocation dependencyLocation = dependency.getLocation(key);
if (dependencyLocation != null
&& dependencyLocation.toString().equals(location.toString())) {
result = (InputLocation) Dependency.class
.getMethod("getImportedFrom")
.invoke(dependency);
break;
}
}
}
}

return result;
} catch (IllegalAccessException
| InvocationTargetException
| NoSuchMethodException
| NoSuchFieldException
| ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

private Set<?> getLocationKeys(Dependency dependency)
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
Field delegateField = Class.forName("org.apache.maven.model.BaseObject").getDeclaredField("delegate");
delegateField.setAccessible(true);
Object delegate = delegateField.get(dependency);
delegateField.setAccessible(false);

Field locationsField = delegate.getClass().getDeclaredField("locations");
locationsField.setAccessible(true);
Object locations = locationsField.get(delegate);
locationsField.setAccessible(false);

if (!(locations instanceof Map)) {
throw new RuntimeException(
"Expected a Map, got " + locations.getClass().getName());
}

return ((Map<?, ?>) locations).keySet();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package org.apache.maven.plugins.help;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand All @@ -18,33 +16,29 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.help;

import java.lang.reflect.Method;

import org.apache.maven.model.InputLocation;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import java.lang.reflect.Method;

/**
* Selects the most suitable implementation for {@link InputLocation.StringFormatter}.
*/
public class InputLocationFormatterFactory
{
public class InputLocationFormatterFactory {
static Class<?> inputLocationClass = InputLocation.class;

public static InputLocation.StringFormatter produce( final Log log, final MavenProject project )
{
try
{
public static InputLocation.StringFormatter produce(final Log log, final MavenProject project) {
try {
// This method was introduced in Maven 4.
Method getImportedFromMethod = inputLocationClass.getDeclaredMethod( "getImportedFrom" );
return new Maven4InputLocationFormatter( getImportedFromMethod, project );
}
catch ( NoSuchMethodException nsme )
{
Method getImportedFromMethod = inputLocationClass.getDeclaredMethod("getImportedFrom");
return new ImportedFromLocationFormatter(getImportedFromMethod, project);
} catch (NoSuchMethodException nsme) {
// Fallback to pre-Maven 4 implementation.
log.info( "Unable to print chain of POM imports, falling back to printing the source POM "
+ "without import information. This feature is available in Maven 4.0.0+." );
log.info("Unable to print chain of POM imports, falling back to printing the source POM "
+ "without import information. This feature is available in Maven 4.0.0+.");
return new DefaultInputLocationFormatter();
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 3073624

Please sign in to comment.