Add missing properties to RepackageTask and improve test coverage

Closes gh-4978
This commit is contained in:
Andy Wilkinson
2016-01-21 17:04:25 +00:00
parent 36133d93fe
commit 9dc858cdf5
6 changed files with 311 additions and 9 deletions

View File

@@ -0,0 +1,182 @@
/*
* Copyright 2012-2015 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 java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.gradle.tooling.ProjectConnection;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Integration tests for creating a fully executable jar with Gradle.
*
* @author Andy Wilkinson
*/
public class FullyExecutableJarTests {
private static final String BOOT_VERSION = Versions.getBootVersion();
private static ProjectConnection project;
@BeforeClass
public static void createProject() throws IOException {
project = new ProjectCreator().createProject("executable-jar");
}
@Test
public void jarIsNotExecutableByDefault() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION).run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(false));
}
@Test
public void madeExecutableViaExtension() throws IOException {
project.newBuild().forTasks("clean", "build").withArguments(
"-PbootVersion=" + BOOT_VERSION, "-PextensionExecutable=true").run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
}
@Test
public void madeExecutableViaTask() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskExecutable=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
}
@Test
public void taskTakesPrecedenceForMakingJarExecutable() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION,
"-PextensionExecutable=false", "-PtaskExecutable=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
}
@Test
public void scriptPropertiesFromTask() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskProperties=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
assertThat(containsLine("# Provides:.*__task__", executableJar), is(true));
}
@Test
public void scriptPropertiesFromExtension() throws IOException {
project.newBuild().forTasks("clean", "build").withArguments(
"-PbootVersion=" + BOOT_VERSION, "-PextensionProperties=true").run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
assertThat(containsLine("# Provides:.*__extension__", executableJar), is(true));
}
@Test
public void taskTakesPrecedenceForScriptProperties() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION,
"-PextensionProperties=true", "-PtaskProperties=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(isFullyExecutable(executableJar), is(true));
assertThat(containsLine("# Provides:.*__task__", executableJar), is(true));
}
@Test
public void customScriptFromTask() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PtaskScript=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(containsLine("Custom task script", executableJar), is(true));
}
@Test
public void customScriptFromExtension() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PextensionScript=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(containsLine("Custom extension script", executableJar), is(true));
}
@Test
public void taskTakesPrecedenceForCustomScript() throws IOException {
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PextensionScript=true",
"-PtaskScript=true")
.run();
File buildLibs = new File("target/executable-jar/build/libs");
File executableJar = new File(buildLibs, "executable-jar.jar");
assertThat(containsLine("Custom task script", executableJar), is(true));
}
private boolean isFullyExecutable(File file) throws IOException {
return containsLine("#!/bin/bash", file);
}
private boolean containsLine(String toMatch, File file) throws IOException {
Pattern pattern = Pattern.compile(toMatch);
for (String line : readLines(file)) {
if (pattern.matcher(line).matches()) {
return true;
}
}
return false;
}
private List<String> readLines(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
List<String> lines = new ArrayList<String>();
try {
while ((line = reader.readLine()) != null && lines.size() < 50) {
lines.add(line);
}
}
finally {
reader.close();
}
return lines;
}
}

View File

@@ -0,0 +1,53 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}"
}
}
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: 'spring-boot'
apply plugin: 'java'
dependencies {
compile 'org.springframework.boot:spring-boot-starter-freemarker'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-devtools'
compile files("foo.jar")
}
springBoot {
mainClass = 'foo.bar.Baz'
}
if (project.properties['taskExecutable']) {
bootRepackage.executable = Boolean.valueOf(project.taskExecutable)
}
if (project.properties['extensionExecutable']) {
springBoot.executable = Boolean.valueOf(project.extensionExecutable)
}
if (project.properties['taskProperties']) {
bootRepackage.executable = true
bootRepackage.embeddedLaunchScriptProperties = ['initInfoProvides': '__task__']
}
if (project.properties['extensionProperties']) {
bootRepackage.executable = true
springBoot.embeddedLaunchScriptProperties = ['initInfoProvides': '__extension__']
}
if (project.properties['taskScript']) {
bootRepackage.embeddedLaunchScript = file('task.script')
}
if (project.properties['extensionScript']) {
springBoot.embeddedLaunchScript = file('extension.script')
}