Browse Source
Develop gradle plugin that can repackage JAR/WAR archives so that they can be launched using 'java -jar' Issue: #53129653pull/9/head
8 changed files with 357 additions and 0 deletions
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
# Spring Boot - Gradle Plugin |
||||
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
<?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> |
||||
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* 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); |
||||
} |
||||
} |
||||
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* 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; |
||||
} |
||||
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
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); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
/* |
||||
* 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); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue