All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dynatrace.buildtools.graalnative.DynatraceNativeImageExtension Maven / Gradle / Ivy

The newest version!
package com.dynatrace.buildtools.graalnative;


import com.dynatrace.buildtools.graalnative.shared.BuildArgs;
import com.dynatrace.buildtools.graalnative.shared.Constants;
import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.nio.file.Path;
import java.util.List;

@Component(role = AbstractMavenLifecycleParticipant.class, hint = "dynatrace-native-build-tools")
public class DynatraceNativeImageExtension extends AbstractMavenLifecycleParticipant implements LogEnabled {

	private DynatraceLogger logger;

	@Override
	public void enableLogging(Logger logger) {
		this.logger = DynatraceLogger.of(logger);
	}

	@Override
	public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
		for (final MavenProject project : session.getProjects()) {
			final Plugin graalNativePlugin = PluginUtils.getGraalVmPlugin(project);
			if (graalNativePlugin != null) {
				logger.info("Detected GraalVM native-image plugin");
			}

			final Plugin quarkusPlugin = PluginUtils.getQuarkusPlugin(project);
			if (quarkusPlugin != null) {
				logger.info("Detected Quarkus plugin");
			}

			if (graalNativePlugin == null && quarkusPlugin == null) {
				throw new MavenExecutionException("Failed to configure project for Dynatrace agent: project doesn't use GraalVM or Quarkus plugin", project.getFile());
			}

			if (graalNativePlugin != null) {
				final String preparedAgentDirProperty = String.format("${%s}", DynatraceProperties.DYNATRACE_PREPARED_AGENT_DIR_PROPERTY_NAME);
				final Path buildAgentLib = Path.of(preparedAgentDirProperty, Constants.BUILD_AGENT_LIB_PATH);
				final Path oneagentGraalNativeFeatureJar = Path.of(preparedAgentDirProperty, Constants.ONEAGENT_FEATURE_JAR_PATH);
				final String agentOptions = String.format("${%s}", DynatraceProperties.DYNATRACE_AGENT_OPTIONS_PROPERTY_NAME);

				Xpp3Dom graalNativePluginConfig = (Xpp3Dom) graalNativePlugin.getConfiguration();
				if (graalNativePluginConfig == null) {
					graalNativePluginConfig = new Xpp3Dom("configuration");
					graalNativePlugin.setConfiguration(graalNativePluginConfig);
				}
				addAgentConfiguration(graalNativePluginConfig, buildAgentLib, oneagentGraalNativeFeatureJar, agentOptions);

				for (final PluginExecution pluginExecution : graalNativePlugin.getExecutions()) {
					Xpp3Dom pluginExecutionConfig = (Xpp3Dom) pluginExecution.getConfiguration();
					if (pluginExecutionConfig == null) {
						pluginExecutionConfig = new Xpp3Dom("configuration");
						pluginExecution.setConfiguration(pluginExecutionConfig);
					}
					addAgentConfiguration(pluginExecutionConfig, buildAgentLib, oneagentGraalNativeFeatureJar, agentOptions);
				}
			}
		}
	}

	private static void addAgentConfiguration(final Xpp3Dom configuration, final Path buildAgentLib, final Path oneagentGraalNativeFeatureJar, final String agentOptions) {
		final List buildArgs = BuildArgs.getBuildArgs(oneagentGraalNativeFeatureJar.toString());
		final Xpp3Dom buildArgsDom = Xpp3DomUtils.getOrAddChild(configuration, "buildArgs");

		for (final String buildArg : buildArgs) {
			final Xpp3Dom buildArgDom = new Xpp3Dom("buildArg");
			buildArgDom.setValue(buildArg);
			buildArgsDom.addChild(buildArgDom);
		}

		final String agentPath = BuildArgs.getAgentPath(buildAgentLib.toString(), agentOptions);
		final Xpp3Dom jvmArgs = Xpp3DomUtils.getOrAddChild(configuration, "jvmArgs");
		final Xpp3Dom agentPathArg = new Xpp3Dom("jvmArg");
		agentPathArg.setValue(agentPath);
		jvmArgs.addChild(agentPathArg);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy