压缩文件太大 AWS Lambda Java Gradle

huangapple go评论126阅读模式
英文:

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

压缩文件太大 AWS Lambda Java Gradle

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:

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**") // example, untested.
}

See the Javadoc for more details: https://docs.gradle.org/current/javadoc/org/gradle/api/file/CopySpec.html

huangapple
  • 本文由 发表于 2020年5月4日 04:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61581119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定