简体   繁体   English

通过 maven for Spring Boot 应用程序为 AWS Beanstalk 创建 .ebextensions 的正确方法是什么

[英]What is the proper way to create .ebextensions for AWS Beanstalk through maven for Spring Boot app

We have to customize hosts file in our dynamically generated Elastic Beanstalk instance of our Spring Boot application during our GitLab CI/CD pipeline.在 GitLab CI/CD 管道期间,我们必须在 Spring Boot 应用程序的动态生成的 Elastic Beanstalk 实例中自定义hosts文件。 To do this, we need to provide a .ebextensions directory with a config file that looks like this:为此,我们需要提供一个.ebextensions目录,其中包含一个如下所示的配置文件:

commands:
  01_add_hosts:
    command: echo 123.12.12.123 myhost.com >> /etc/hosts

Since we have a spring boot application, we have to package .ebextensions at the root level of our fat jar.由于我们有一个 spring boot 应用程序,我们必须在我们的 fat jar 的根级别打包.ebextensions So, basically we unzip the jar, add the ebextensions directory and zip it back.所以,基本上我们解压缩 jar,添加 ebextensions 目录并将其压缩回来。 This way we successfully achieve the Beanstalk customization in our Gitlab pipelines.这样我们就成功地在我们的 Gitlab 管道中实现了 Beanstalk 定制。

However, to make this process we use maven-antrun-plugin like this:然而,为了完成这个过程,我们像这样使用maven-antrun-plugin

<!-- Bundle .ebextensions inside jar -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>prepare</id>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <unzip src="${project.build.directory}/${project.build.finalName}.jar" dest="${project.build.directory}/${project.build.finalName}" />
                    <copy todir="${project.build.directory}/${project.build.finalName}/" overwrite="false">
                        <fileset dir="./" includes=".ebextensions/**"/>
                    </copy>
                    <zip compress="false" destfile="${project.build.directory}/${project.build.finalName}.jar" basedir="${project.build.directory}/${project.build.finalName}"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The problem is that maven-antrun-plugin is and old plugin from 2014 and this looks like not the proper way to achieve this bundle jar.问题是maven-antrun-plugin是 2014 年的旧插件,这看起来不是实现此捆绑 jar 的正确方法。

Do you how or what it is the spring boot way to create this bundle jar?你是如何或什么是 Spring Boot 方式来创建这个包 jar 的? I mean add directories/files at the root level of the jar in spring boot?我的意思是在 Spring Boot 中在 jar 的根级别添加目录/文件? Bear in mind that we bundle this at our pipeline job time that deploys through AWS Beanstalk maven plugin.请记住,我们在通过 AWS Beanstalk maven 插件部署的管道作业时间将其捆绑在一起。

Is it required that you upload only a jar to elastic beanstalk?是否要求您只将一个罐子上传到弹性豆茎? The way we do it is that we upload a zipfile, containing our fat jar, a .ebextensions directory and a Procfile.我们这样做的方法是上传一个 zip 文件,其中包含我们的胖 jar、一个 .ebextensions 目录和一个 Procfile。 This way the ebextensions get applied and the Procfile contains the command to start the java process.通过这种方式,ebextensions 得到应用并且 Procfile 包含启动 java 进程的命令。

So for example you could upload hello-world.zip containing:例如,您可以上传包含以下内容的 hello-world.zip:

  • .ebextensions: .eb 扩展名:
    • hosts.config主机配置文件
  • hello-world.jar你好-world.jar
  • Procfile配置文件

With Procfile being a text file containing: Procfile 是一个文本文件,其中包含:

web: java -jar hello-world.jar网络:java -jar hello-world.jar

If you do it like this, there's no need to embed your ebextension files in your application jar, which makes things a whole lot easier in my opinion.如果你这样做,就没有必要将你的 ebextension 文件嵌入到你的应用程序 jar 中,在我看来这会让事情变得更容易。

Procfile documentation Procfile 文档

The Build Helper Maven Plugin can be used for this. Build Helper Maven Plugin可用于此目的。 Create a folder .ebextensions in the project's root and add the plugin to the <build><plugins> section of the pom.xml :在项目的根目录中创建一个文件夹.ebextensions并将插件添加到pom.xml<build><plugins>部分:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-resource-ebextensions</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>add-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${basedir}/.ebextensions</directory>
                        <targetPath>.ebextensions</targetPath>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AWS Elastic Beanstalk上部署Spring Boot应用程序 - 获得502错误 - Deploying Spring Boot app on AWS Elastic Beanstalk — getting 502 error AWS Elastic Beanstalk.ebextensions 未更新默认配置 - AWS Elastic Beanstalk .ebextensions not updating the default configuration Spring Boot 2 + AWS Beanstalk设置堆大小 - Spring boot 2 + AWS Beanstalk setting heapsize AWS Beanstalk上的Spring Boot抛出404 - Spring Boot on AWS Beanstalk throwing 404 AWS Beanstalk + Kotlin + Spring 启动:NoSuchMethodException main() - AWS Beanstalk + Kotlin + Spring boot: NoSuchMethodException main() 用于 Spring 启动项目的 AWS beanstalk 上的 Java 11 - Java 11 on AWS beanstalk for Spring boot project 将初始化代码添加到 Spring 引导应用程序的正确方法是什么? - What's the proper way to add initialization code to a Spring Boot application? 应用依赖注入的正确的Spring Boot方式是什么 - What is the proper spring boot way to apply dependency injection 将 spring 引导与 mongo 连接并阅读文档的正确方法是什么? - What is the proper way to connect spring boot with mongo and read document? 使用 Jasypt 在 AWS Elastic Beanstalk 上为 Spring Boot 应用程序传递环境变量 - Pass environment variable on AWS Elastic Beanstalk for Spring Boot App using Jasypt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM