From a1517de6dbfb5398ff8dd193a242d80cb6abfa9a Mon Sep 17 00:00:00 2001 From: dave noel Date: Fri, 27 Sep 2013 16:16:26 -0400 Subject: [PATCH] Add gradle runJar --- .../boot/gradle/SpringBootPlugin.java | 61 +++++++++++-------- .../boot/gradle/task/RunJar.java | 56 +++++++++++++++++ 2 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunJar.java diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPlugin.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPlugin.java index fa36b9f486..df1c37a47f 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPlugin.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/SpringBootPlugin.java @@ -22,6 +22,7 @@ import org.gradle.api.artifacts.Dependency; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; import org.springframework.boot.gradle.task.Repackage; +import org.springframework.boot.gradle.task.RunJar; /** * Gradle 'Spring Boot' {@link Plugin}. @@ -30,31 +31,37 @@ import org.springframework.boot.gradle.task.Repackage; */ public class SpringBootPlugin implements Plugin { - private static final String REPACKAGE_TASK_NAME = "repackage"; - - @Override - public void apply(Project project) { - project.getPlugins().apply(BasePlugin.class); - project.getPlugins().apply(JavaPlugin.class); - project.getExtensions().create("springBoot", SpringBootPluginExtension.class); - Repackage packageTask = addRepackageTask(project); - ensureTaksRunsOnAssembly(project, packageTask); - } - - private Repackage addRepackageTask(Project project) { - Repackage packageTask = project.getTasks().create(REPACKAGE_TASK_NAME, - Repackage.class); - packageTask.setDescription("Repackage existing JAR and WAR " - + "archives so that they can be executed from the command " - + "line using 'java -jar'"); - packageTask.setGroup(BasePlugin.BUILD_GROUP); - packageTask.dependsOn(project.getConfigurations() - .getByName(Dependency.ARCHIVES_CONFIGURATION).getAllArtifacts() - .getBuildDependencies()); - return packageTask; - } - - private void ensureTaksRunsOnAssembly(Project project, Repackage task) { - project.getTasks().getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(task); - } + private static final String REPACKAGE_TASK_NAME = "repackage"; + private static final String RUN_JAR_TASK_NAME = "runJar"; + + @Override + public void apply(Project project) { + project.getPlugins().apply(BasePlugin.class); + project.getPlugins().apply(JavaPlugin.class); + project.getExtensions().create("springBoot", SpringBootPluginExtension.class); + Repackage packageTask = addRepackageTask(project); + ensureTaskRunsOnAssembly(project, packageTask); + addRunJarTask(project); + } + + private void addRunJarTask(Project project) { + RunJar runJarTask = project.getTasks().create(RUN_JAR_TASK_NAME, RunJar.class); + runJarTask.setDescription("Run the executable JAR/WAR"); + runJarTask.setGroup("Execution"); + runJarTask.dependsOn(REPACKAGE_TASK_NAME); + } + + private Repackage addRepackageTask(Project project) { + Repackage packageTask = project.getTasks().create(REPACKAGE_TASK_NAME, Repackage.class); + packageTask.setDescription("Repackage existing JAR and WAR " + + "archives so that they can be executed from the command " + "line using 'java -jar'"); + packageTask.setGroup(BasePlugin.BUILD_GROUP); + packageTask.dependsOn(project.getConfigurations().getByName(Dependency.ARCHIVES_CONFIGURATION) + .getAllArtifacts().getBuildDependencies()); + return packageTask; + } + + private void ensureTaskRunsOnAssembly(Project project, Repackage task) { + project.getTasks().getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(task); + } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunJar.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunJar.java new file mode 100644 index 0000000000..3ff8bf3b07 --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/task/RunJar.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.gradle.task; + +import java.io.File; + +import org.gradle.api.Action; +import org.gradle.api.DefaultTask; +import org.gradle.api.Project; +import org.gradle.api.internal.file.FileResolver; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.bundling.Jar; +import org.gradle.process.internal.DefaultExecAction; +import org.gradle.process.internal.ExecAction; + +/** + * Run Jar task. Run the built jar file from Gradle. + * + * @author Dave Noel + */ +public class RunJar extends DefaultTask { + + private File file; + + @TaskAction + public void runJar() { + Project project = getProject(); + project.getTasks().withType(Jar.class, new Action() { + @Override + public void execute(Jar archive) { + file = archive.getArchivePath(); + } + }); + if (file != null && file.exists()) { + ExecAction action = new DefaultExecAction(getServices().get( + FileResolver.class)); + action.setExecutable(System.getProperty("java.home") + "/bin/java"); + action.args("-jar", file); + action.execute(); + } + } +}