diff --git a/pom.xml b/pom.xml
index 22ed04b..f220796 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,11 @@
io.jenkins.plugins
commons-lang3-api
+
+ org.jenkinsci.plugins
+ pipeline-model-definition
+ test
+
diff --git a/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java b/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java
index 20be400..401ac23 100755
--- a/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java
+++ b/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java
@@ -29,6 +29,7 @@
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
+import org.jenkinsci.Symbol;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
@@ -357,7 +358,7 @@ public SbtInstallation(String name, String home, String sbtArguments, List ext
}
private static String launderHome(String home) {
- if (home.endsWith("/") || home.endsWith("\\")) {
+ if (home != null && (home.endsWith("/") || home.endsWith("\\"))) {
// see https://issues.apache.org/bugzilla/show_bug.cgi?id=26947
// Ant doesn't like the trailing slash, especially on Windows
return home.substring(0, home.length() - 1);
@@ -383,11 +384,21 @@ public SbtInstallation forNode(Node node, TaskListener log) throws IOException,
return new SbtInstallation(getName(), translateFor(node, log), sbtArguments, getProperties().toList());
}
+ @Override
+ public void buildEnvVars(EnvVars env) {
+ String home = getHome();
+ if (home == null) {
+ return;
+ }
+ // see EnvVars javadoc for why this adds PATH.
+ env.put("PATH+SBT", home + "/bin");
+ }
+
public String getSbtArguments() {
return sbtArguments;
}
- @Extension
+ @Extension @Symbol("sbt")
public static class DescriptorImpl extends ToolDescriptor {
public SbtInstallation[] getInstallations() {
diff --git a/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java b/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java
new file mode 100644
index 0000000..d8f762c
--- /dev/null
+++ b/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java
@@ -0,0 +1,86 @@
+package org.jvnet.hudson.plugins;
+
+import java.util.List;
+
+import hudson.model.Cause;
+import hudson.model.CauseAction;
+import hudson.model.DownloadService;
+import hudson.model.Result;
+import hudson.tools.InstallSourceProperty;
+import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
+import org.jenkinsci.plugins.workflow.job.WorkflowJob;
+import org.jenkinsci.plugins.workflow.job.WorkflowRun;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.jvnet.hudson.test.JenkinsRule;
+import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
+
+@WithJenkins
+class SbtPluginBuilderTest {
+
+ @Test
+ public void testSbtInstallation(JenkinsRule jenkinsRule) throws Exception {
+ final var installationDescriptor = jenkinsRule.get(SbtPluginBuilder.SbtInstallation.DescriptorImpl.class);
+ Assertions.assertEquals(0, installationDescriptor.getInstallations().length);
+
+ final var installer = new SbtPluginBuilder.SbtInstaller("installer1");
+ final var installSourceProperty = new InstallSourceProperty(List.of(installer));
+
+ final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class);
+ final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty));
+ descriptor.setInstallations(sbtInstallation);
+
+ Assertions.assertEquals(1, installationDescriptor.getInstallations().length);
+ }
+
+ @Test
+ public void testFreestyle(JenkinsRule jenkinsRule) throws Exception {
+ DownloadService.Downloadable mvnDl =
+ DownloadService.Downloadable.get("org.jvnet.hudson.plugins.SbtPluginBuilder.SbtInstaller");
+
+ mvnDl.updateNow();
+
+ final var installer = new SbtPluginBuilder.SbtInstaller("1.10.5");
+ final var installSourceProperty = new InstallSourceProperty(List.of(installer));
+
+ final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class);
+ final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty));
+ descriptor.setInstallations(sbtInstallation);
+
+ final var freestyle1 = jenkinsRule.createFreeStyleProject("freestyle1");
+ final var sbtPluginBuilder = new SbtPluginBuilder("sbt1", null, null, "about", null);
+ freestyle1.getBuildersList().add(sbtPluginBuilder);
+
+ final var freeStyleBuildQueueTaskFuture = freestyle1.scheduleBuild2(0, new Cause.UserIdCause());
+
+ final var freeStyleBuild = freeStyleBuildQueueTaskFuture.get();
+
+ jenkinsRule.assertBuildStatus(Result.SUCCESS, freeStyleBuild);
+ jenkinsRule.assertLogContains("1.10.5", freeStyleBuild);
+ }
+
+ @Test
+ public void testPipeline(JenkinsRule jenkinsRule) throws Exception {
+ DownloadService.Downloadable mvnDl =
+ DownloadService.Downloadable.get("org.jvnet.hudson.plugins.SbtPluginBuilder.SbtInstaller");
+
+ mvnDl.updateNow();
+
+ final var installer = new SbtPluginBuilder.SbtInstaller("1.10.5");
+ final var installSourceProperty = new InstallSourceProperty(List.of(installer));
+
+ final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class);
+ final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty));
+ descriptor.setInstallations(sbtInstallation);
+
+ final var project = jenkinsRule.createProject(WorkflowJob.class, "project1");
+ project.setDefinition(new CpsFlowDefinition("pipeline { agent any \n tools { sbt 'sbt1' } \n stages { stage('build') { steps { sh 'sbt about' } } } }", true));
+
+ final var workflowRunQueueTaskFuture = project.scheduleBuild2(0, new CauseAction(new Cause.UserIdCause()));
+
+ final var workflowRun = workflowRunQueueTaskFuture.get();
+
+ jenkinsRule.assertBuildStatus(Result.SUCCESS, workflowRun);
+ jenkinsRule.assertLogContains("1.10.5", workflowRun);
+ }
+}