Added support for deploying uber JARs with no main/start-class to new deployer

This commit is contained in:
Oleg Zhurakousky
2019-08-08 15:13:48 +02:00
parent bac9978972
commit 4e0e3fdc6d
4 changed files with 117 additions and 17 deletions

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>function.example</groupId>
<artifactId>bootjarnostart</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud-function.version>3.0.0.BUILD-SNAPSHOT</spring-cloud-function.version>
<wrapper.version>1.0.17.RELEASE</wrapper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>NONE</layout>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<classifier>exec</classifier>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package function.example;
import java.util.function.Function;
public class UpperCaseFunction implements Function<String, String> {
@Override
public String apply(String value) {
System.out.println("Uppercasing " + value);
return value.toUpperCase();
}
}

View File

@@ -154,23 +154,29 @@ class ExternalFunctionJarLauncher extends JarLauncher {
return functionRegistration;
}
protected boolean isBootApplicationWithMain() throws Exception {
return StringUtils.hasText(this.archive.getManifest().getMainAttributes().getValue("Start-Class"));
}
private void launch(ApplicationContext deployerContext, String[] args) throws Exception {
JarFile.registerUrlProtocolHandler();
Thread.currentThread().setContextClassLoader(createClassLoader(getClassPathArchives()));
evalContext.setTypeLocator(new StandardTypeLocator(Thread.currentThread().getContextClassLoader()));
String mainClassName = getMainClass();
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(mainClassName);
if (this.isBootApplicationWithMain()) {
String mainClassName = getMainClass();
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(mainClassName);
Class<?> bootAppClass = Thread.currentThread().getContextClassLoader()
.loadClass(SpringApplication.class.getName());
Method runMethod = bootAppClass.getDeclaredMethod("run", Class.class, String[].class);
Object applicationContext = runMethod.invoke(null, mainClass, (Object) args);
if (logger.isInfoEnabled()) {
logger.info("Application context for archive '" + archive.getUrl() + "' is created.");
Class<?> bootAppClass = Thread.currentThread().getContextClassLoader()
.loadClass(SpringApplication.class.getName());
Method runMethod = bootAppClass.getDeclaredMethod("run", Class.class, String[].class);
Object applicationContext = runMethod.invoke(null, mainClass, (Object) args);
if (logger.isInfoEnabled()) {
logger.info("Application context for archive '" + archive.getUrl() + "' is created.");
}
evalContext.setVariable("context", applicationContext);
setBeanFactory(applicationContext);
}
evalContext.setVariable("context", applicationContext);
setBeanFactory(applicationContext);
}
private void setBeanFactory(Object applicationContext) throws Exception {
@@ -190,13 +196,15 @@ class ExternalFunctionJarLauncher extends JarLauncher {
@SuppressWarnings("unchecked")
private Map<String, Object> discoverFunctions() throws Exception {
Map<String, Object> allFunctions = new HashMap<String, Object>();
Expression parsed = new SpelExpressionParser()
.parseExpression("#context.getBeansOfType(T(java.util.function.Function))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Supplier))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Consumer))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
if (evalContext.lookupVariable("context") != null) { // no start0class uber jars
Expression parsed = new SpelExpressionParser()
.parseExpression("#context.getBeansOfType(T(java.util.function.Function))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Supplier))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Consumer))");
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
}
return allFunctions;
}
}

View File

@@ -46,6 +46,18 @@ public class ApplicationContainerTests {
assertThat(invokerByClass.uppercaseSimple("stacy")).isEqualTo("STACY");
}
@Test
public void testCustomApplicationContainerWithBootJarNoStartClass() throws Exception {
String[] args = new String[] {"--spring.cloud.function.location=target/it/bootjarnostart/target/bootjarnostart-0.0.1.BUILD-SNAPSHOT-exec.jar",
"--spring.cloud.function.function-class=function.example.UpperCaseFunction"};
JavaInvoker invokerByClass =
FunctionDeployerBootstrap.instance(args).run(JavaInvoker.class, args);
assertThat(invokerByClass.uppercaseSimple("bob")).isEqualTo("BOB");
assertThat(invokerByClass.uppercaseSimple("stacy")).isEqualTo("STACY");
}
@Test
public void testCustomApplicationContainerWithBootAppSimpleTypes() throws Exception {