Commit 2752177f authored by Phillip Webb's avatar Phillip Webb

Add spring-boot-gradle-plugin

Develop gradle plugin that can repackage JAR/WAR archives so that
they can be launched using 'java -jar'

Issue: #53129653
parent 0db6799d
......@@ -19,6 +19,7 @@
<spring.integration.version>2.2.4.RELEASE</spring.integration.version>
<spring.batch.version>2.2.0.RELEASE</spring.batch.version>
<groovy.version>2.1.6</groovy.version>
<gradle.version>1.6</gradle.version>
<tomcat.version>7.0.42</tomcat.version>
<jetty.version>8.1.9.v20130131</jetty.version>
<aspectj.version>1.7.3</aspectj.version>
......@@ -42,11 +43,34 @@
<module>spring-boot-loader</module>
<module>spring-boot-loader-tools</module>
<module>spring-boot-maven-plugin</module>
<module>spring-boot-gradle-plugin</module>
<module>spring-boot-ops</module>
<module>spring-boot-ups</module>
<module>spring-boot-cli</module>
<module>spring-boot-integration-tests</module>
</modules>
<repositories>
<repository>
<id>spring-ext</id>
<url>http://repo.springsource.org/ext-release-local/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>gradle</id>
<url>http://repo.gradle.org/gradle/libs-releases-local</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<pluginManagement>
<plugins>
......@@ -368,6 +392,11 @@
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-templates</artifactId>
......@@ -409,6 +438,26 @@
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-core</artifactId>
<version>${gradle.version}</version>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-base-services</artifactId>
<version>${gradle.version}</version>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-base-services-groovy</artifactId>
<version>${gradle.version}</version>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-plugins</artifactId>
<version>${gradle.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
......
# Spring Boot - Gradle Plugin
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-gradle-plugin</artifactId>
<properties>
<main.basedir>${basedir}/..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-base-services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-base-services-groovy</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-plugins</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/groovy</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.5-03</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
/*
* 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;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
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;
/**
* Gradle 'Spring Boot' {@link Plugin}.
*
* @author Phillip Webb
*/
public class SpringBootPlugin implements Plugin<Project> {
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);
}
}
/*
* 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
/**
* Gradle DSL Extension for 'Spring Boot'.
*
* @author Phillip Webb
*/
public class SpringBootPluginExtension {
/**
* The main class that should be run. If not specified the value from the
* MANIFEST will be used, or if no manifest entry is the archive will be
* searched for a suitable class.
*/
String mainClass
/**
* The name of the provided configuration. If not specified 'providedRuntime' will
* be used.
*/
String providedConfiguration
/**
* If the original source archive should be backed-up before being repackaged.
*/
boolean backupSource = true;
}
package org.springframework.boot.gradle.task;
import java.io.File;
import java.io.IOException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection;
import org.springframework.boot.loader.tools.Libraries;
import org.springframework.boot.loader.tools.LibraryCallback;
import org.springframework.boot.loader.tools.LibraryScope;
/**
* Expose Gradle {@link Configuration}s as {@link Libraries}.
*
* @author Phillip Webb
*/
class ProjectLibraries implements Libraries {
private final Project project;
private String providedConfigurationName = "providedRuntime";
/**
* Create a new {@link ProjectLibraries} instance of the specified {@link Project}.
* @param project the gradle project
*/
public ProjectLibraries(Project project) {
this.project = project;
}
/**
* Set the name of the provided configuration. Defaults to 'providedRuntime'.
* @param providedConfigurationName the providedConfigurationName to set
*/
public void setProvidedConfigurationName(String providedConfigurationName) {
this.providedConfigurationName = providedConfigurationName;
}
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
FileCollection compile = this.project.getConfigurations().getByName("compile");
FileCollection runtime = this.project.getConfigurations().getByName("runtime");
runtime = runtime.minus(compile);
FileCollection provided = this.project.getConfigurations().findByName(
this.providedConfigurationName);
if (provided != null) {
compile = compile.minus(provided);
runtime = compile.minus(provided);
}
libraries(LibraryScope.COMPILE, compile, callback);
libraries(LibraryScope.RUNTIME, runtime, callback);
libraries(LibraryScope.PROVIDED, provided, callback);
}
private void libraries(LibraryScope scope, FileCollection files,
LibraryCallback callback) throws IOException {
if (files != null) {
for (File file : files) {
callback.library(file, scope);
}
}
}
}
/*
* 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 java.io.IOException;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.bundling.Jar;
import org.springframework.boot.gradle.SpringBootPluginExtension;
import org.springframework.boot.loader.tools.Repackager;
/**
* Repackage task.
*
* @author Phillip Webb
*/
public class Repackage extends DefaultTask {
@TaskAction
public void repackage() {
Project project = getProject();
final SpringBootPluginExtension extension = project.getExtensions().getByType(
SpringBootPluginExtension.class);
final ProjectLibraries libraries = new ProjectLibraries(project);
if (extension.getProvidedConfiguration() != null) {
libraries.setProvidedConfigurationName(extension.getProvidedConfiguration());
}
project.getTasks().withType(Jar.class, new Action<Jar>() {
@Override
public void execute(Jar archive) {
File file = archive.getArchivePath();
if (file.exists()) {
Repackager repackager = new Repackager(file);
repackager.setMainClass(extension.getMainClass());
repackager.setBackupSource(extension.isBackupSource());
try {
repackager.repackage(libraries);
}
catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}
}
});
}
}
implementation-class=org.springframework.boot.gradle.SpringBootPlugin
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment