Quantcast
Channel: Gradle Forums - Latest posts
Viewing all articles
Browse latest Browse all 19859

How can a custom gradle plugin determine its own version

$
0
0

Hello,

You can obtain the artifact and its metadata added to the build classpath with the following source code (known to work with Gradle 6.5, both with plugins applied with legacy buildscript and plugins blocks:

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionIdentifier;

public class Foo implements Plugin<Project> {

	@Override
	public void apply(Project project) {
		final Configuration classpath = project.getBuildscript().getConfigurations().getByName("classpath");
		final String version = classpath.getResolvedConfiguration().getResolvedArtifacts().stream()
			.map(artifact -> artifact.getModuleVersion().getId())
			.filter(id -> "org.example".equals(id.getGroup()) && "foo".equals(id.getName()))
			.findAny()
			.map(ModuleVersionIdentifier::getVersion)
			.orElseThrow(() -> new IllegalStateException("foo plugin has been deployed with wrong coordinates: expected group to be 'org.example' and name to be 'foo'"));
		project.getLogger().lifecycle("applied plugin with version [{}]", version);
	}
}

Viewing all articles
Browse latest Browse all 19859

Trending Articles