英文:
zip file too large aws lambda java gradle
问题
我想在 AWS Lambda 上部署我的 Java 函数。
我查看了这个文档 https://docs.aws.amazon.com/lambda/latest/dg/java-package.html。
这是我的 build.gradle
文件:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
}
group 'com.xxx'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains:annotations:16.0.2"
implementation 'com.amazonaws:aws-lambda-java-core:1.2.0'
implementation 'com.amazonaws:aws-lambda-java-events:2.2.7'
implementation 'com.google.code.gson:gson:2.8.6'
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.2'
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.1.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': archiveVersion,
'Main-Class': 'Main'
}
baseName = project.name + '-all'
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
with jar
}
task buildZip(type: Zip) {
manifest {
attributes (
'Implementation-Title': 'AWS Handler',
'Implementation-Version': archiveVersion,
'Main-Class': 'AWSHandler'
)
}
from compileJava
from processResources
into('lib') {
from configurations.runtimeClasspath
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
我运行 gradle buildZip
命令来构建我的压缩文件。实现了 RequestHandler
的 AWS 类位于 src/main/java/AWSHandler.java
。
一旦命令成功结束,我得到一个 710MB 的压缩文件,这太大了!!!
当我解压文件时,我可以看到有很多库来支持许多操作系统。
根据这个文档 https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html,似乎 Java 8 函数在 Amazon Linux
上运行。
所以我可以清理我的压缩文件,只保留这个操作系统。
我能用 Gradle 做到吗,还是应该创建一个额外的脚本来清理我的文件?
英文:
I would like to deploy my java function on aws lambda
I took this documentation https://docs.aws.amazon.com/lambda/latest/dg/java-package.html
This is my build.gradle
file
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
}
group 'com.xxx'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains:annotations:16.0.2"
implementation 'com.amazonaws:aws-lambda-java-core:1.2.0'
implementation 'com.amazonaws:aws-lambda-java-events:2.2.7'
implementation 'com.google.code.gson:gson:2.8.6'
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.2'
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.1.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': archiveVersion,
'Main-Class': 'Main'
}
baseName = project.name + '-all'
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
with jar
}
task buildZip(type: Zip) {
manifest {
attributes (
'Implementation-Title': 'AWS Handler',
'Implementation-Version': archiveVersion,
'Main-Class': 'AWSHandler'
)
}
from compileJava
from processResources
into('lib') {
from configurations.runtimeClasspath
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
So I am running gradle buildZip
to build my zip, my aws class implementing RequestHandler
is in src/main/java/AWSHandler.java
Once the command ended with success I have a 710MB zip file, this is huge !!!
When I am unzipping the file I can see that there are many libs to support many os
Flowing this doc https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html seems that java8 functions are running on Amazon Linux
So I could clean my zip to only get this os
Can I do it with gradle or should I create an extra script to clean my file ?
答案1
得分: 1
Zip
任务类型的into
方法可以接受一个CopySpec
,您可以使用该CopySpec
来添加排除项:
链接:https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:into(java.lang.Object,%20org.gradle.api.Action)
into('lib') {
from configurations.runtimeClasspath
exclude("**windows**") // 示例,未经测试。
}
有关更多详细信息,请参阅Javadoc:https://docs.gradle.org/current/javadoc/org/gradle/api/file/CopySpec.html
英文:
The into
method of the Zip
task type can accept a CopySpec
which you can use to add exclusions:
into('lib') {
from configurations.runtimeClasspath
exclude("**windows**") // example, untested.
}
See the Javadoc for more details: https://docs.gradle.org/current/javadoc/org/gradle/api/file/CopySpec.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论