繁体   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

在 GitLab CI/CD 管道期间,我们必须在 Spring Boot 应用程序的动态生成的 Elastic Beanstalk 实例中自定义hosts文件。 为此,我们需要提供一个.ebextensions目录,其中包含一个如下所示的配置文件:

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

由于我们有一个 spring boot 应用程序,我们必须在我们的 fat jar 的根级别打包.ebextensions 所以,基本上我们解压缩 jar,添加 ebextensions 目录并将其压缩回来。 这样我们就成功地在我们的 Gitlab 管道中实现了 Beanstalk 定制。

然而,为了完成这个过程,我们像这样使用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>

问题是maven-antrun-plugin是 2014 年的旧插件,这看起来不是实现此捆绑 jar 的正确方法。

你是如何或什么是 Spring Boot 方式来创建这个包 jar 的? 我的意思是在 Spring Boot 中在 jar 的根级别添加目录/文件? 请记住,我们在通过 AWS Beanstalk maven 插件部署的管道作业时间将其捆绑在一起。

是否要求您只将一个罐子上传到弹性豆茎? 我们这样做的方法是上传一个 zip 文件,其中包含我们的胖 jar、一个 .ebextensions 目录和一个 Procfile。 通过这种方式,ebextensions 得到应用并且 Procfile 包含启动 java 进程的命令。

例如,您可以上传包含以下内容的 hello-world.zip:

  • .eb 扩展名:
    • 主机配置文件
  • 你好-world.jar
  • 配置文件

Procfile 是一个文本文件,其中包含:

网络:java -jar hello-world.jar

如果你这样做,就没有必要将你的 ebextension 文件嵌入到你的应用程序 jar 中,在我看来这会让事情变得更容易。

Procfile 文档

Build Helper Maven Plugin可用于此目的。 在项目的根目录中创建一个文件夹.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM