问题
We have to customize hosts
file in our dynamically generated Elastic Beanstalk instance of our Spring Boot application during our GitLab CI/CD pipeline. To do this, we need to provide a .ebextensions
directory with a config file that looks like this:
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. So, basically we unzip the jar, add the ebextensions directory and zip it back. This way we successfully achieve the Beanstalk customization in our Gitlab pipelines.
However, to make this process we use maven-antrun-plugin
like this:
<!-- 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.
Do you how or what it is the spring boot way to create this bundle jar? I mean add directories/files at the root level of the jar in spring boot? Bear in mind that we bundle this at our pipeline job time that deploys through AWS Beanstalk maven plugin.
回答1:
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. This way the ebextensions get applied and the Procfile contains the command to start the java process.
So for example you could upload hello-world.zip containing:
- .ebextensions:
- hosts.config
- hello-world.jar
- Procfile
With Procfile being a text file containing:
web: 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.
Procfile documentation
回答2:
The Build Helper Maven Plugin can be used for this. Create a folder .ebextensions
in the project's root and add the plugin to the <build><plugins>
section of the 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>
来源:https://stackoverflow.com/questions/53211066/what-is-the-proper-way-to-create-ebextensions-for-aws-beanstalk-through-maven-f