Clean way of adding .ebextensions to Spring Boot Jar using Gradle
Asked Answered
L

7

10

Is there a clean way of adding additional root folders to a Spring Boot Jar file generated using the default bootRepackage. In my case I need the .ebextenions folder for AWS beanstalk.

I know I can hack it -- for example add another task after bootRepackage to unzip, repackage (again), and re-zip. Is there a cleaner way ?

Thanks

.. the 2 ways that I've tried (that don't work) :

jar {
    from('src/main/ebextensions') {
        into('ebextensions')
    }
}

bootRepackage {
    from('src/main/ebextensions') {
        into('ebextensions')
    }
}
Libava answered 29/8, 2016 at 13:43 Comment(0)
R
4

I'm still working on deploying Spring Boot to EBS myself...

I think the folder has to be called .ebextensions (notice the leading dot). So you would say into('./.ebextensions') instead of into('ebextensions').

Alternatively, you might try uploading a ZIP file containing your JAR and your .ebextensions folder:

task zip(type: Zip, dependsOn: bootRepackage) {
    from ('./.ebextensions') {
        into '.ebextensions'
    }
    from (jar.outputs.files) {
        into '.'
    }
    destinationDir project.buildDir
}
Regelate answered 9/9, 2016 at 3:15 Comment(2)
Hi, I have my .ebextensions folder in my Spring Boot project's root folder, and I copy-pasted the code above into my build.gradle, but I don't see my .ebextensions folder show up in my jar file's root folder. What did I miss? ThanksDenys
It's a hidden folder and may not be displayed right away.Valenevalenka
G
7

For Spring Boot 2 (Gradle) if .ebextensions is located at the root of your project, use the following task:

bootJar {
    from('./.ebextensions') { into '.ebextensions' }
}

or

bootWar {
    from('./.ebextensions') { into '.ebextensions' }
}

This way Gradle will copy .ebextensions into the root of the application package.

But if you prefer convention over configuration, move .ebextensions folder inside src/main/resources. The content of resources directory is packaged automatically in /BOOT-INF/classes/ (no scripting required). And the .ebextensions directory will be discovered by EB deployment scripts when unpacked.

Goodloe answered 2/3, 2019 at 18:28 Comment(2)
(SO poll) please leave a comment here: which of 2 solutions did you pick?Goodloe
Newer platform "Amazon Linux 2" does not parse files inside Jar anymore docs.aws.amazon.com/elasticbeanstalk/latest/dg/…Relaxation
R
4

I'm still working on deploying Spring Boot to EBS myself...

I think the folder has to be called .ebextensions (notice the leading dot). So you would say into('./.ebextensions') instead of into('ebextensions').

Alternatively, you might try uploading a ZIP file containing your JAR and your .ebextensions folder:

task zip(type: Zip, dependsOn: bootRepackage) {
    from ('./.ebextensions') {
        into '.ebextensions'
    }
    from (jar.outputs.files) {
        into '.'
    }
    destinationDir project.buildDir
}
Regelate answered 9/9, 2016 at 3:15 Comment(2)
Hi, I have my .ebextensions folder in my Spring Boot project's root folder, and I copy-pasted the code above into my build.gradle, but I don't see my .ebextensions folder show up in my jar file's root folder. What did I miss? ThanksDenys
It's a hidden folder and may not be displayed right away.Valenevalenka
G
2

With Grails 3, I use gradle clean dist to create a .zip file containing a .war for EB distribution, and use a Procfile to describe the Spring Boot command line. An .ebextensions folder is in the base directory of my project, and projectName and projectVersion are variables defined in the build.gradle file:

task dist(type: Zip) {
    from war.outputs.files
    from "src/staging/Procfile" // this file allows us to control how ElasticBeanstalk starts up our app on its Java SE platform
    from('./.ebextensions') {
        into '.ebextensions'
    }
    rename { String fileName ->
        if (fileName == "${projectName}-${projectVersion}.war".toString()) {
            fileName.replace("${projectName}-${projectVersion}", "application")
        } else {
            fileName
        }
    }
}
dist.dependsOn assemble

where the contents of the Procfile in src/staging looks like this:

web: java -jar application.war
Gynecocracy answered 18/2, 2017 at 4:16 Comment(0)
S
2

If you move your src/main/ebextensions folder to src/main/resources/.ebextensions, it will be automatically copied by the jar task to the root of the .jar (along with any other files in /resources), where EBS expects it, without any additional scripting.

That's about as clean as you can get!

Schwitzer answered 10/5, 2017 at 22:30 Comment(0)
P
2

I have my .ebextensions at the root of my project. This seems to work for me.

war { from ('./.ebextensions') { into '.ebextensions' } }

Party answered 30/10, 2017 at 15:31 Comment(0)
A
2

Sept 2021

I tried to add the .ebextensions folder in my jar but is seems elastic beanstalk doesn't like it anymore.

Instead, I had to create a zip file with the jar and the .ebextensions folder inside.

Like this

~/myapp.zip
|-- myapp.jar
|-- .ebextensions/

Doc

Here is my gradle kts code:

val jarFilename = "myapp.jar"

val packageZip by tasks.register<Zip>("packageZip") {
    dependsOn("bootJar")
    archiveFileName.set("myapp.zip")
    destinationDirectory.set(layout.buildDirectory.dir("libs"))

    from("$buildDir/libs/$jarFilename")

    from("../.ebextensions") {
        into(".ebextensions")
    }
    from("../.platform") {
        into(".platform")
    }
}

tasks.register<Delete>("cleanLibs") {
    delete("$buildDir/libs/")
}

tasks.named<BootJar>("bootJar") {
    dependsOn("cleanLibs")

    this.archiveFileName.set(jarFilename)

    finalizedBy(packageZip)
}
Ache answered 13/9, 2021 at 11:12 Comment(0)
S
0

As all the supplied answers didn't fix the problem, I found another solution to this problem. Just add the .ebextensions to the list of artifacts which come out of the buildspec.yml

artifacts:
  files:
    - app.jar
    - .ebextensions/**/*
Shearer answered 30/3, 2023 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.