Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + Elastic Beanstalk .ebextensions in JAR

I have a very standard Spring Boot application (with a application.properties properties file located in standard /src/main/resources folder) which I'm deploying on AWS Elastic Beanstalk as a "fat JAR". It works quite nicely but there is an issue with image uploading on the server. After some investigation it seems that the NGINX configuration needs to be tweaked (increase client_max_body_size to something so it can accept uploads up to 10MB) and therefore I have added an .ebextensions folder under /src/main/resources with a file with the following content (taken from this answer): -

files:
    "/etc/nginx/conf.d/proxy.conf":
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;

However, when I run mvn on my build it doesn't create .ebextensions in the root folder and I'm wondering what is the best solution for this. My pom.xml file is pretty minimal and currently contains the following:

    ...

    <packaging>jar</packaging>

    ....

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.6.RELEASE</version>
                </dependency>
            </dependencies>

        </plugin>

Thanks in advance!


Update 1

@Lorena when I insert <resources> ... XML into my pom.xml and then start the server it crashes out with the following: -

2017-03-20 21:40:29.504  WARN 10784 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailApiSpringBootMail': Unsatisfied dependency expressed through field 'javaMailSender'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-03-20 21:40:29.507  INFO 10784 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-20 21:40:29.533  WARN 10784 --- [           main] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-03-20 21:40:29.637 ERROR 10784 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field javaMailSender in com.myapp.server.api.impl.EmailApiSpringBootMail required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
    - Bean method 'mailSender' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.JndiNameProperty @ConditionalOnProperty (spring.mail.jndi-name) did not find property 'jndi-name'; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.HostProperty @ConditionalOnProperty (spring.mail.host) did not find property 'host'

Removing the XML again fixes the issue so unfortunately this won't work.


Update 2

The issues described in the previous section seemed to be that the new <resources> pointing to the .ebextentions was overridding the <resources> block defined in: -

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

To get everything working I copied it across and appended to the end as follows: -

    <resources>

        <resource>
            <directory>src/main/resources/ebextensions</directory>
            <targetPath>.ebextensions</targetPath>
            <filtering>true</filtering>
        </resource>

        <!-- Followed is copied from `spring-boot-starter-parent.pom` -->

        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/application*.yml</include>
                <include>**/application*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>**/application*.yml</exclude>
                <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>

    </resources>

Thanks everyone for your help!

like image 430
bobmarksie Avatar asked Mar 17 '17 15:03

bobmarksie


People also ask

Where do Ebextensions go in spring boot?

ebextensions should sit next to your jar file in your application source bundle.

Where does Elastic Beanstalk store configuration files?

Saved configurations are stored in the Elastic Beanstalk S3 bucket in a folder named after your application. For example, configurations for an application named my-app in the us-west-2 region for account number 123456789012 can be found at s3://elasticbeanstalk-us-west-2-123456789012/resources/templates/my-app/ .


1 Answers

I believe in this case that standalone JAR is used, it's easier to go with Procfile-based configuration and then bundle your JAR and .ebextensions into a zip file.

First create a file, called Procfile in the root of the project with following content:

web: java -jar sample_app-1.0.0.jar

Then create a zip file, containing the JAR and Procfile file and .ebextensions directory:

sample_app.zip
|
|_.ebextensions
|   |_ nginx
|      |_ conf.d
|         |_ proxy.conf
|
|_ sample_app-1.0.0.jar
|_ Procfile
like image 89
Khashayar Avatar answered Sep 22 '22 07:09

Khashayar