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

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

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

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFileFilter;
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.IOException;
import java.nio.file.Path;

@Mojo(
	name = "copy-runtime-agent",
	defaultPhase = LifecyclePhase.PROCESS_RESOURCES
)
public class CopyRuntimeAgentMojo extends AbstractMojo {

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

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		try {
			final DynatraceProperties dynatraceProperties = new DynatraceProperties(project.getProperties());
			final Path preparedAgentDir = Path.of(dynatraceProperties.getPreparedAgentDir().orElseThrow(() -> new MojoExecutionException(
				String.format(
					"Property '%s' is missing: copy-runtime-agent goal cannot be executed on its own as it depends on the setup-build-agent goal; use the process-resources phase instead",
					DynatraceProperties.DYNATRACE_PREPARED_AGENT_DIR_PROPERTY_NAME
				)
			)));
			copyRuntimeAgent(preparedAgentDir, project.getBuild().getDirectory());
		} catch (IOException e) {
			throw new MojoExecutionException("Failed to copy runtime agent", e);
		}
	}

	private static void copyRuntimeAgent(final Path preparedAgentDir, final String targetDir) throws IOException {
		final Path sourceRuntimeDir = preparedAgentDir.resolve("graalnative/runtime");
		final Path targetAgentDir = Path.of(targetDir, "dynatrace", "agent");

		FileUtils.copyDirectory(
			sourceRuntimeDir.toFile(),
			targetAgentDir.resolve("lib64").toFile(),
			FileFileFilter.INSTANCE
		);

		copyDirectory(
			sourceRuntimeDir.resolve("any"),
			targetAgentDir.resolve("any")
		);

		copyDirectory(
			sourceRuntimeDir.resolve("conf"),
			targetAgentDir.resolve("conf")
		);

		FileUtils.copyFileToDirectory(
			preparedAgentDir.resolve("agent/installer.version").toFile(),
			targetAgentDir.toFile()
		);

		targetAgentDir.resolve("dt_fips_disabled.flag").toFile().createNewFile();
	}

	private static void copyDirectory(final Path source, final Path destination) throws IOException {
		if (!source.toFile().exists()) {
			return;
		}

		FileUtils.copyDirectory(
			source.toFile(),
			destination.toFile()
		);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy