我们必须在 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 年的旧插件,这看起来不是实现此 bundle jar 的正确方法。
您知道创建此 bundle jar 的 Spring Boot 方式是什么吗?我的意思是在 Spring Boot 中的 jar 根级别添加目录/文件?请记住,我们将其捆绑在通过 AWS Beanstalk maven 插件部署的管道作业时间。
最佳答案
Build Helper Maven Plugin可以用于此目的。创建文件夹.ebextensions
在项目的根目录中并将插件添加到 <build><plugins>
pom.xml
部分:
<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>
关于java - 通过 maven for Spring Boot 应用程序为 AWS Beanstalk 创建 .ebextensions 的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53211066/