Add LayoutFactory in spring.factories

Instead of a fixed enum of layout types, user can provide custom
layouts via implementations of LayoutFactory in spring.factories.
pull/6613/head
Dave Syer 8 years ago
parent 94da9ad6e6
commit 500a3df6e9

@ -20,13 +20,14 @@ import java.io.File;
import java.util.Map;
import java.util.Set;
import groovy.lang.Closure;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.springframework.boot.gradle.buildinfo.BuildInfo;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.Layouts;
import org.springframework.boot.loader.tools.LayoutType;
import groovy.lang.Closure;
/**
* Gradle DSL Extension for 'Spring Boot'. Most of the time Spring Boot can guess the
@ -88,7 +89,7 @@ public class SpringBootPluginExtension {
* the MANIFEST.MF 'Main-Class' to be PropertiesLauncher. Gradle will coerce literal
* String values to the correct type.
*/
LayoutType layout;
String layout;
/**
* Libraries that must be unpacked from fat jars in order to run. Use Strings in the
@ -145,7 +146,7 @@ public class SpringBootPluginExtension {
* @return the Layout to use or null if not explicitly set
*/
public Layout convertLayout() {
return (this.layout == null ? null : this.layout.layout);
return (this.layout == null ? null : LayoutType.layout(this.layout));
}
public String getMainClass() {
@ -188,11 +189,11 @@ public class SpringBootPluginExtension {
this.backupSource = backupSource;
}
public LayoutType getLayout() {
public String getLayout() {
return this.layout;
}
public void setLayout(LayoutType layout) {
public void setLayout(String layout) {
this.layout = layout;
}
@ -276,29 +277,4 @@ public class SpringBootPluginExtension {
}
}
/**
* Layout Types.
*/
enum LayoutType {
JAR(new Layouts.Jar()),
WAR(new Layouts.War()),
ZIP(new Layouts.Expanded()),
DIR(new Layouts.Expanded()),
MODULE(new Layouts.Module()),
NONE(new Layouts.None());
Layout layout;
LayoutType(Layout layout) {
this.layout = layout;
}
}
}

@ -0,0 +1,31 @@
/*
* 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.loader.tools;
/**
* Strategy for creating instances of {@link Layout}.
*
* @author Dave Syer
*
*/
public interface LayoutFactory {
Layout getLayout();
String getName();
}

@ -0,0 +1,99 @@
/*
* 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.loader.tools;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.core.io.support.SpringFactoriesLoader;
/**
* Archive layout types.
*/
public enum LayoutType {
/**
* Jar Layout.
*/
JAR(new Layouts.Jar()),
/**
* War Layout.
*/
WAR(new Layouts.War()),
/**
* Zip Layout.
*/
ZIP(new Layouts.Expanded()),
/**
* Dir Layout.
*/
DIR(new Layouts.Expanded()),
/**
* Module Layout.
*/
MODULE(new Layouts.Module()),
/**
* No Layout.
*/
NONE(new Layouts.None());
private static Map<String, Layout> customTypes;
private final Layout layout;
public Layout layout() {
return this.layout;
}
LayoutType(Layout layout) {
this.layout = layout;
}
public static Layout layout(String value) {
try {
return valueOf(value).layout();
}
catch (IllegalArgumentException e) {
if (customTypes == null) {
customTypes = new HashMap<String, Layout>();
lookupCustomTypes();
}
Layout layout = customTypes.get(value);
if (layout == null) {
throw new IllegalArgumentException(
"Cannot resolve custom layout type: " + value);
}
return layout;
}
}
private static void lookupCustomTypes() {
ClassLoader classLoader = LayoutType.class.getClassLoader();
List<LayoutFactory> factories = SpringFactoriesLoader
.loadFactories(LayoutFactory.class, classLoader);
for (LayoutFactory factory : factories) {
customTypes.put(factory.getName(), factory.getLayout());
}
}
}

@ -0,0 +1,51 @@
/*
* 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.loader.tools;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class LayoutTypeTests {
@Test
public void standardType() {
assertThat(LayoutType.layout("DIR")).isEqualTo(LayoutType.valueOf("DIR").layout());
}
@Test
public void customType() {
assertThat(LayoutType.layout("CUSTOM")).isNotNull();
}
public static class TestLayoutFactory implements LayoutFactory {
@Override
public Layout getLayout() {
return new Layouts.Jar();
}
@Override
public String getName() {
return "CUSTOM";
}}
}

@ -0,0 +1,2 @@
org.springframework.boot.loader.tools.LayoutFactory=\
org.springframework.boot.loader.tools.LayoutTypeTests.TestLayoutFactory

@ -42,8 +42,7 @@ import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.springframework.boot.loader.tools.DefaultLaunchScript;
import org.springframework.boot.loader.tools.LaunchScript;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.Layouts;
import org.springframework.boot.loader.tools.LayoutType;
import org.springframework.boot.loader.tools.Libraries;
import org.springframework.boot.loader.tools.Repackager;
@ -131,7 +130,7 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
* @since 1.0
*/
@Parameter
private LayoutType layout;
private String layout;
/**
* A list of the libraries that must be unpacked from fat jars in order to run.
@ -228,7 +227,6 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
repackager.setMainClass(this.mainClass);
if (this.layout != null) {
getLog().info("Layout: " + this.layout);
repackager.setLayout(this.layout.layout());
}
return repackager;
}
@ -309,53 +307,6 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
}
}
/**
* Archive layout types.
*/
public enum LayoutType {
/**
* Jar Layout.
*/
JAR(new Layouts.Jar()),
/**
* War Layout.
*/
WAR(new Layouts.War()),
/**
* Zip Layout.
*/
ZIP(new Layouts.Expanded()),
/**
* Dir Layout.
*/
DIR(new Layouts.Expanded()),
/**
* Module Layout.
*/
MODULE(new Layouts.Module()),
/**
* No Layout.
*/
NONE(new Layouts.None());
private final Layout layout;
public Layout layout() {
return this.layout;
}
LayoutType(Layout layout) {
this.layout = layout;
}
}
private static class LoggingRepackager extends Repackager {
private final Log log;

Loading…
Cancel
Save