Scripting, maven: use getArtifacts to get the plugin's runtime classpath

..to correctly pass the dependencies in the ExecuteKotlinScriptMojo.
#KT-50306 fixed

Also add a test that a script has access to plugin-configured
dependencies.
If plugin/dependencies is populated, the extra artifacts should be
made available when the script is run. This test uses 'junit' simply
because it's commonly-used, but not already present, or shaded as part
of another artifact.
This commit is contained in:
Joseph Walton
2021-12-16 13:03:36 +11:00
committed by teamcity
parent 2bd53e3dea
commit f38e1b218d
4 changed files with 57 additions and 1 deletions
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-executeKotlinScriptWithDependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>execute-kotlin-script</id>
<phase>compile</phase>
<goals>
<goal>script</goal>
</goals>
<configuration>
<scriptFile>script.kts</scriptFile>
</configuration>
</execution>
</executions>
<dependencies>
<!-- This is an arbitrary artifact that's not otherwise in the script's classpath -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,3 @@
import org.junit.Assert
println("Dependency jar is: ${Assert::class.java.getProtectionDomain().getCodeSource().getLocation().getPath().replace(Regex(".*/"), "")}")
@@ -0,0 +1,3 @@
source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath());
assertBuildLogHasLine("Dependency jar is: junit-4.13.1.jar");
@@ -247,7 +247,7 @@ public class ExecuteKotlinScriptMojo extends AbstractMojo {
}
private List<File> getThisPluginDependencies() {
return plugin.getDependencies().stream().map(this::getDependencyFile).collect(Collectors.toList());
return plugin.getArtifacts().stream().map(this::getArtifactFile).collect(Collectors.toList());
}
private File getThisPluginAsDependency() {