Added support for deploying uber JARs with no main/start-class to new deployer
This commit is contained in:
@@ -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>
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -154,23 +154,29 @@ class ExternalFunctionJarLauncher extends JarLauncher {
|
|||||||
return functionRegistration;
|
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 {
|
private void launch(ApplicationContext deployerContext, String[] args) throws Exception {
|
||||||
JarFile.registerUrlProtocolHandler();
|
JarFile.registerUrlProtocolHandler();
|
||||||
Thread.currentThread().setContextClassLoader(createClassLoader(getClassPathArchives()));
|
Thread.currentThread().setContextClassLoader(createClassLoader(getClassPathArchives()));
|
||||||
evalContext.setTypeLocator(new StandardTypeLocator(Thread.currentThread().getContextClassLoader()));
|
evalContext.setTypeLocator(new StandardTypeLocator(Thread.currentThread().getContextClassLoader()));
|
||||||
|
|
||||||
String mainClassName = getMainClass();
|
if (this.isBootApplicationWithMain()) {
|
||||||
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(mainClassName);
|
String mainClassName = getMainClass();
|
||||||
|
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(mainClassName);
|
||||||
|
|
||||||
Class<?> bootAppClass = Thread.currentThread().getContextClassLoader()
|
Class<?> bootAppClass = Thread.currentThread().getContextClassLoader()
|
||||||
.loadClass(SpringApplication.class.getName());
|
.loadClass(SpringApplication.class.getName());
|
||||||
Method runMethod = bootAppClass.getDeclaredMethod("run", Class.class, String[].class);
|
Method runMethod = bootAppClass.getDeclaredMethod("run", Class.class, String[].class);
|
||||||
Object applicationContext = runMethod.invoke(null, mainClass, (Object) args);
|
Object applicationContext = runMethod.invoke(null, mainClass, (Object) args);
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger.info("Application context for archive '" + archive.getUrl() + "' is created.");
|
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 {
|
private void setBeanFactory(Object applicationContext) throws Exception {
|
||||||
@@ -190,13 +196,15 @@ class ExternalFunctionJarLauncher extends JarLauncher {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Map<String, Object> discoverFunctions() throws Exception {
|
private Map<String, Object> discoverFunctions() throws Exception {
|
||||||
Map<String, Object> allFunctions = new HashMap<String, Object>();
|
Map<String, Object> allFunctions = new HashMap<String, Object>();
|
||||||
Expression parsed = new SpelExpressionParser()
|
if (evalContext.lookupVariable("context") != null) { // no start0class uber jars
|
||||||
.parseExpression("#context.getBeansOfType(T(java.util.function.Function))");
|
Expression parsed = new SpelExpressionParser()
|
||||||
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
|
.parseExpression("#context.getBeansOfType(T(java.util.function.Function))");
|
||||||
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Supplier))");
|
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
|
||||||
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
|
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Supplier))");
|
||||||
parsed = new SpelExpressionParser().parseExpression("#context.getBeansOfType(T(java.util.function.Consumer))");
|
allFunctions.putAll((Map<String, Object>) parsed.getValue(evalContext));
|
||||||
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;
|
return allFunctions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,18 @@ public class ApplicationContainerTests {
|
|||||||
assertThat(invokerByClass.uppercaseSimple("stacy")).isEqualTo("STACY");
|
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
|
@Test
|
||||||
public void testCustomApplicationContainerWithBootAppSimpleTypes() throws Exception {
|
public void testCustomApplicationContainerWithBootAppSimpleTypes() throws Exception {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user