Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tycho-4.0.x] Improve PluginConfigurationHelper by using a dedicated object #2628

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public BndManifestProcessor(MavenSession mavenSession) {
@Override
public void processManifest(MavenProject mavenProject, Manifest manifest) {

if (configurationHelper.getBooleanOption("deriveHeaderFromSource")
// don't be confused here, we use FALSE als default because it means no such
if (configurationHelper.getConfiguration().getBoolean("deriveHeaderFromSource")
// don't be confused here, we use FALSE as default because it means no such
// configuration option defined in the mojo (probably called from different
// context) but the default in the PackagePluginMojo defines the real default
// (what is TRUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@
*******************************************************************************/
package org.eclipse.tycho.helper;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.xml.Xpp3Dom;

/**
Expand All @@ -24,26 +37,101 @@
@Component(role = PluginConfigurationHelper.class)
public class PluginConfigurationHelper {

public Optional<Xpp3Dom> getDomOption(String name) {
Optional<MojoExecution> execution = MojoExecutionHelper.getExecution();
Optional<Xpp3Dom> configuration = execution.map(ex -> ex.getConfiguration());
Optional<Xpp3Dom> child = configuration.map(cfg -> cfg.getChild(name));
return child;
@Requirement
ProjectHelper projectHelper;

@Requirement
LegacySupport legacySupport;

private Map<URL, PluginDescriptor> descriptorCache = new ConcurrentHashMap<>();

public Configuration getConfiguration() {
MojoExecution execution = MojoExecutionHelper.getExecution().orElse(null);
if (execution == null) {
return new Configuration(null);
}
Xpp3Dom configuration = execution.getConfiguration();
return getConfiguration(configuration);
}

public Configuration getConfiguration(Xpp3Dom configuration) {
return new Configuration(configuration);
}

public Optional<String> getStringOption(String name) {
return getDomOption(name).map(child -> {
String value = child.getValue();
if (value == null) {
String attribute = child.getAttribute("default-value");
return attribute;
public <M extends Mojo> Configuration getConfiguration(Class<M> mojo) {
URL resource = mojo.getResource("/META-INF/maven/plugin.xml");
if (resource == null) {
throw new IllegalStateException("can't find plugin descriptor of mojo " + mojo.getName());
}
PluginDescriptor pluginDescriptor = descriptorCache.computeIfAbsent(resource, url -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()))) {
InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader(reader, Map.of());
return new PluginDescriptorBuilder().build(interpolationFilterReader);
} catch (Exception e) {
}
return value;
return null;
});
if (pluginDescriptor == null) {
throw new IllegalStateException("can't load plugin descriptor of mojo " + mojo.getName());
}
for (MojoDescriptor mojoDescriptor : pluginDescriptor.getMojos()) {
if (mojo.getName().equals(mojoDescriptor.getImplementation())) {
Xpp3Dom configuration = projectHelper.getPluginConfiguration(pluginDescriptor.getGroupId(),
pluginDescriptor.getArtifactId(), mojoDescriptor.getGoal());
return getConfiguration(configuration);

}
}
throw new IllegalArgumentException("can't find mojo " + mojo.getName()
+ " goal in descriptor, possible goals are "
+ pluginDescriptor.getMojos().stream().map(MojoDescriptor::getGoal).collect(Collectors.joining(",")));
}

public Optional<Boolean> getBooleanOption(String name) {
return getStringOption(name).map(s -> Boolean.valueOf(s));
public static final class Configuration {

private Xpp3Dom configuration;

Configuration(Xpp3Dom configuration) {
this.configuration = configuration;
}

public Optional<Configuration> getChild(String name) {
if (configuration == null) {
return Optional.empty();
}
Xpp3Dom child = configuration.getChild(name);
if (child == null) {
return Optional.empty();
}
return Optional.of(new Configuration(child));
}

public Optional<String> getString(String name) {
return getChild(name).map(child -> {
String value = child.configuration.getValue();
if (value == null) {
return child.configuration.getAttribute("default-value");
}
return value;
});
}

public Optional<Boolean> getBoolean(String name) {
return getString(name).map(Boolean::valueOf);
}

public <E extends Enum<E>> Optional<E> getEnum(String name, Class<E> type) {
return getString(name).map(value -> {

for (E e : type.getEnumConstants()) {
if (e.name().equals(value)) {
return e;
}
}
return null;
});
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.tycho.helper;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -21,19 +23,25 @@
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

@Component(role = ProjectHelper.class)
public class ProjectHelper {

@Requirement
private MojoDescriptorCreator mojoDescriptorCreator;

@Requirement
private LegacySupport legacySupport;

private Map<String, Plugin> cliPlugins = new ConcurrentHashMap<String, Plugin>();

/**
Expand Down Expand Up @@ -97,6 +105,18 @@ public boolean hasPluginExecution(String pluginGroupId, String pluginArtifactId,
return false;
}

public Xpp3Dom getPluginConfiguration(String pluginGroupId, String pluginArtifactId, String goal) {
MavenSession currentSession = legacySupport.getSession();
if (currentSession == null) {
return null;
}
MavenProject currentProject = currentSession.getCurrentProject();
if (currentProject == null) {
return null;
}
return getPluginConfiguration(pluginGroupId, pluginArtifactId, goal, currentProject, currentSession);
}

public Xpp3Dom getPluginConfiguration(String pluginGroupId, String pluginArtifactId, String goal,
MavenProject project, MavenSession mavenSession) {
MavenSession clone = mavenSession.clone();
Expand All @@ -105,15 +125,39 @@ public Xpp3Dom getPluginConfiguration(String pluginGroupId, String pluginArtifac
for (Plugin plugin : plugins) {
if (plugin.getGroupId().equals(pluginGroupId) && plugin.getArtifactId().equals(pluginArtifactId)) {
if (goal == null) {
return (Xpp3Dom) plugin.getConfiguration();
return getDom(plugin.getConfiguration());
}
for (PluginExecution execution : plugin.getExecutions()) {
if (execution.getGoals().contains(goal)) {
return (Xpp3Dom) execution.getConfiguration();
return getDom(execution.getConfiguration());
}
}
}
}
return null;
}

public MavenProject getCurrentProject() {
MavenSession session = legacySupport.getSession();
if (session == null) {
return null;
}
return session.getCurrentProject();
}

public Xpp3Dom getDom(Object object) {
if (object == null) {
return null;
}
if (object instanceof Xpp3Dom xpp3) {
return xpp3;
}
try {
return Xpp3DomBuilder.build(new StringReader(object.toString()));
} catch (XmlPullParserException e) {
return null;
} catch (IOException e) {
return null;
}
}
}