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 cc7b95a

Browse files
committedOct 4, 2020
Upgrade Gradle; Fix lambda JAR hack
1 parent e291e94 commit cc7b95a

File tree

4 files changed

+31
-39
lines changed

4 files changed

+31
-39
lines changed
 

‎cdk/build.gradle

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ repositories {
1010
}
1111
}
1212

13+
group = 'com.davidagood'
14+
version = '1.0.0'
15+
sourceCompatibility = JavaVersion.VERSION_11
16+
targetCompatibility = JavaVersion.VERSION_11
17+
1318
application {
1419
mainClassName = 'com.davidagood.cdkjava.CdkJavaApp'
15-
applicationDefaultJvmArgs = ['-Dgreeting.language=en', "-DlambdaJar=${project(':lambda').name}"] //"
1620
}
1721

1822
dependencies {
@@ -23,9 +27,15 @@ dependencies {
2327
testImplementation 'junit:junit:4.12'
2428
}
2529

26-
group = 'com.davidagood'
27-
version = '0.1'
28-
sourceCompatibility = '11'
30+
run {
31+
project(':lambda').afterEvaluate {
32+
def files = project(':lambda').tasks.findByName('jar').outputs.files
33+
println "module=:lambda, task=jar has output files=${files.collect { it }}"
34+
def firstFile = files[0]
35+
println "Expecting first and only file to be executable JAR; path=${firstFile}"
36+
systemProperty 'lambdaJarAbsolutePath', firstFile.path
37+
}
38+
}
2939

3040
tasks.withType(JavaCompile) {
3141
options.encoding = 'UTF-8'

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
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;
13+
import java.nio.file.Path;
1814

1915
public class CdkJavaStack extends Stack {
2016
public CdkJavaStack(final Construct parent, final String id) {
@@ -29,29 +25,15 @@ public CdkJavaStack(final Construct parent, final String id, final StackProps pr
2925
.sortKey(Attribute.builder().name("SK").type(AttributeType.STRING).build())
3026
.build();
3127

32-
URI uri;
33-
34-
try {
35-
URL url = getClass().getResource("/lib");
36-
37-
if (isNull(url)) {
38-
throw new LambdaDeploymentPackageNotFoundException("Lambda deployment package not found in classpath" +
39-
" resources; Expected to find \"/lib\"");
40-
}
28+
String lambdaExecutableJarPath = System.getProperty("lambdaJarAbsolutePath");
4129

42-
uri = url.toURI();
43-
44-
System.out.println("Full URI for classpath resource \"/lib\": " + uri);
45-
} catch (URISyntaxException e) {
46-
throw new RuntimeException(e);
30+
if (!Path.of(lambdaExecutableJarPath).toFile().exists()) {
31+
throw new LambdaDeploymentPackageNotFoundException("Lambda deployment package not at path="
32+
+ lambdaExecutableJarPath);
33+
} else {
34+
System.out.println("Found lambda executable JAR at paht=" + lambdaExecutableJarPath);
4735
}
4836

49-
// We expect the URI to be in this format
50-
// jar:file:/home/dgood/IdeaProjects/cdk-java-starter/lambda/build_lambda/lambda.jar!/lib
51-
// Not sure of a better way to get the absolute path of the JAR
52-
String lambdaExecutableJarPath = uri.toString().split("jar:file:")[1].split("!")[0];
53-
System.out.println("Lambda executable JAR path: " + lambdaExecutableJarPath);
54-
5537
Function function = Function.Builder.create(this, "ExampleJavaLambda")
5638
.code(Code.fromAsset(lambdaExecutableJarPath))
5739
.handler("com.davidagood.cdkjava.ExampleHandler::handleRequest")
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Thu Sep 17 09:26:57 EDT 2020
2-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip
31
distributionBase=GRADLE_USER_HOME
42
distributionPath=wrapper/dists
5-
zipStorePath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
64
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

‎lambda/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ repositories {
66
jcenter()
77
}
88

9-
dependencies {
9+
group = 'com.davidagood'
10+
version = '1.0.0'
11+
sourceCompatibility = JavaVersion.VERSION_11
12+
targetCompatibility = JavaVersion.VERSION_11
1013

14+
dependencies {
1115
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
1216
implementation 'com.google.code.gson:gson:2.8.6'
1317
testImplementation 'org.apache.logging.log4j:log4j-api:2.13.0'
@@ -28,7 +32,7 @@ jar {
2832
attributes 'Main-Class': 'com.davidagood.cdkjava.ExampleHandler'
2933
}
3034

31-
destinationDir file(buildLambdaPath)
35+
destinationDirectory = file(buildLambdaPath)
3236

3337
from compileJava
3438
from processResources
@@ -50,12 +54,9 @@ configurations.default.extendsFrom(configurations.lambdaResources)
5054

5155
// expose the artifact created by the packaging task
5256
artifacts {
53-
lambdaResources(jar.archivePath) {
54-
builtBy jar
55-
type 'jar'
56-
}
57+
lambdaResources(jar)
5758
}
5859

5960
clean {
6061
delete buildLambdaPath
61-
}
62+
}

0 commit comments

Comments
 (0)
Please sign in to comment.