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

com.dynatrace.buildtools.graalnative.SetupBuildAgentMojo 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 com.dynatrace.buildtools.graalnative.shared.DownloadException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;

@Mojo(
	name = "setup-build-agent",
	defaultPhase = LifecyclePhase.GENERATE_RESOURCES
)
public class SetupBuildAgentMojo extends AbstractMojo {

	@Parameter(defaultValue = "${project}", required = true, readonly = true)
	private MavenProject project;

	@Parameter(defaultValue = "${session}", required = true, readonly = true)
	private MavenSession session;

	@Parameter(property = "agentDir")
	private String agentDir;

	@Parameter(property = "agentZip")
	private String agentZip;

	@Parameter(property = "agentDownload", defaultValue = "none")
	private AgentDownload agentDownload;

	@Parameter(property = "agentOptions")
	private String agentOptions;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		try {
			final DynatraceLogger logger = DynatraceLogger.of(getLog());
			final File preparedAgentDir = DynatraceAgentDirectory.setup(
				agentDir,
				agentZip,
				agentDownload,
				project,
				session.getTopLevelProject(),
				logger
			);

			final DynatraceProperties dynatraceProperties = new DynatraceProperties(project.getProperties());
			dynatraceProperties.setPreparedAgentDir(preparedAgentDir.getAbsolutePath());
			if (agentOptions != null) {
				dynatraceProperties.setAgentOptions(agentOptions);
			}

			session.getAllProjects().forEach(mavenProject -> {
				final DynatraceProperties dtProperties = new DynatraceProperties(mavenProject.getProperties());
				dtProperties.setPreparedAgentDir(preparedAgentDir.getAbsolutePath());
				if (agentOptions != null) {
					dtProperties.setAgentOptions(agentOptions);
				}
			});

			final Plugin quarkusPlugin = PluginUtils.getQuarkusPlugin(project);
			if (quarkusPlugin != null) {
				final File buildAgentLib = new File(preparedAgentDir, Constants.BUILD_AGENT_LIB_PATH);
				final File oneagentGraalNativeFeatureJar = new File(preparedAgentDir, Constants.ONEAGENT_FEATURE_JAR_PATH);

				final String existingAdditionalBuildArgs = project.getProperties().getProperty(Constants.QUARKUS_NATIVE_ADDITIONAL_BUILD_ARGS);
				final String additionalBuildArgs = BuildArgs.getQuarkusBuildArgs(
					existingAdditionalBuildArgs,
					buildAgentLib,
					oneagentGraalNativeFeatureJar,
					agentOptions
				);
				project.getProperties().setProperty(Constants.QUARKUS_NATIVE_ADDITIONAL_BUILD_ARGS, additionalBuildArgs);
			}
		} catch (IOException | ConfigurationException | DownloadException e) {
			throw new MojoExecutionException("Failed to set up the Dynatrace agent directory", e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy