Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3b0a511

Browse files
committedSep 18, 2020
Eliminate hard-coded JAR path
1 parent f3cf963 commit 3b0a511

File tree

6 files changed

+68
-29
lines changed

6 files changed

+68
-29
lines changed
 

‎cdk/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ repositories {
1212

1313
application {
1414
mainClassName = 'com.davidagood.cdkjava.CdkJavaApp'
15+
applicationDefaultJvmArgs = ['-Dgreeting.language=en', "-DlambdaJar=${project(':lambda').name}"] //"
1516
}
1617

1718
dependencies {
18-
implementation project(':lambda')
19+
runtimeOnly project(':lambda')
1920
implementation 'software.amazon.awscdk:core:1.62.0'
2021
implementation 'software.amazon.awscdk:dynamodb:1.62.0'
2122
implementation 'software.amazon.awscdk:lambda:1.62.0'
@@ -24,7 +25,7 @@ dependencies {
2425

2526
group = 'com.davidagood'
2627
version = '0.1'
27-
sourceCompatibility = '1.8'
28+
sourceCompatibility = '11'
2829

2930
tasks.withType(JavaCompile) {
3031
options.encoding = 'UTF-8'

‎cdk/src/main/java/com/davidagood/cdkjava/CdkJavaStack.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
import software.amazon.awscdk.services.lambda.Function;
1111
import software.amazon.awscdk.services.lambda.Runtime;
1212

13+
import java.net.URI;
14+
import java.net.URISyntaxException;
15+
import java.net.URL;
16+
17+
import static java.util.Objects.isNull;
18+
1319
public class CdkJavaStack extends Stack {
1420
public CdkJavaStack(final Construct parent, final String id) {
1521
this(parent, id, null);
@@ -23,12 +29,33 @@ public CdkJavaStack(final Construct parent, final String id, final StackProps pr
2329
.sortKey(Attribute.builder().name("SK").type(AttributeType.STRING).build())
2430
.build();
2531

26-
Code codeFromLocalPath = Code.fromAsset("/Users/davidgood/dev/cdk-java-gradle-multi-module/lambda/build/libs/lambda-uber.jar");
32+
URI uri;
33+
34+
try {
35+
URL url = getClass().getResource("/liba");
36+
37+
if (isNull(url)) {
38+
throw new RuntimeException("Executable JAR not found in classpath resources");
39+
}
2740

28-
Function function = Function.Builder.create(this, "test-func-cdk")
29-
.code(codeFromLocalPath)
41+
uri = url.toURI();
42+
43+
System.out.println("Full URI for classpath resource \"/lib\": " + uri);
44+
} catch (URISyntaxException e) {
45+
throw new RuntimeException(e);
46+
}
47+
48+
// We expect the URI to be in this format
49+
// jar:file:/home/dgood/IdeaProjects/cdk-java-starter/lambda/build_lambda/lambda.jar!/lib
50+
// Not sure of a better way to get the absolute path of the JAR
51+
String lambdaExecutableJarPath = uri.toString().split("jar:file:")[1].split("!")[0];
52+
System.out.println("Lambda executable JAR path: " + lambdaExecutableJarPath);
53+
54+
Function function = Function.Builder.create(this, "ExampleJavaLambda")
55+
.code(Code.fromAsset(lambdaExecutableJarPath))
3056
.handler("com.davidagood.cdkjava.ExampleHandler::handleRequest")
31-
.runtime(Runtime.JAVA_8)
57+
.runtime(Runtime.JAVA_11)
3258
.build();
3359
}
60+
3461
}

‎lambda/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
build_lambda/
2+
13
# IntelliJ
24
.idea/
35
*.iml

‎lambda/build.gradle

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,41 @@ test {
2121
useJUnitPlatform()
2222
}
2323

24+
def buildLambdaPath = "${projectDir}/build_lambda"
25+
2426
jar {
2527
manifest {
2628
attributes 'Main-Class': 'com.davidagood.cdkjava.ExampleHandler'
2729
}
30+
31+
destinationDir file(buildLambdaPath)
32+
33+
from compileJava
34+
from processResources
35+
36+
// AWS Lambda seems to require the code to be in the lib directory
37+
into('lib') {
38+
from configurations.runtimeClasspath
39+
}
40+
2841
}
2942

30-
// https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example
31-
task uberJar(type: Jar) {
32-
archiveClassifier = 'uber'
43+
// Based on https://github.com/xword/java-npm-gradle-integration-example
44+
// declare a dedicated scope for publishing the packaged JAR
45+
configurations {
46+
lambdaResources
47+
}
3348

34-
from sourceSets.main.output
49+
configurations.default.extendsFrom(configurations.lambdaResources)
3550

36-
dependsOn configurations.runtimeClasspath
37-
from {
38-
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
51+
// expose the artifact created by the packaging task
52+
artifacts {
53+
lambdaResources(jar.archivePath) {
54+
builtBy jar
55+
type 'jar'
3956
}
57+
}
58+
59+
clean {
60+
delete buildLambdaPath
4061
}

‎lambda/src/main/java/com/davidagood/cdkjava/ExampleHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ public class ExampleHandler implements RequestHandler<Map<String, String>, Strin
1414

1515
@Override
1616
public String handleRequest(Map<String, String> event, Context context) {
17-
1817
LambdaLogger logger = context.getLogger();
19-
String response = new String("200 OK");
20-
// log execution details
18+
logger.log("Processed by CDK using a runtime module dependency");
2119
logger.log("ENVIRONMENT VARIABLES: " + gson.toJson(System.getenv()));
2220
logger.log("CONTEXT: " + gson.toJson(context));
23-
// process event
2421
logger.log("EVENT: " + gson.toJson(event));
2522
logger.log("EVENT TYPE: " + event.getClass().toString());
26-
return response;
23+
return "200 OK";
2724
}
2825
}

‎settings.gradle

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
/*
2-
* This file was generated by the Gradle 'init' task.
3-
*
4-
* The settings file is used to specify which projects to include in your build.
5-
*
6-
* Detailed information about configuring a multi-project build in Gradle can be found
7-
* in the user manual at https://docs.gradle.org/6.2.2/userguide/multi_project_builds.html
8-
*/
9-
101
rootProject.name = 'cdk-java-gradle-multi-module'
112

12-
include 'lambda'
13-
include 'cdk'
3+
include 'cdk'
4+
include 'lambda'

0 commit comments

Comments
 (0)
Please sign in to comment.