首页 > 解决方案 > 如何在 Maven 构建中包含随机文件,以便在 AWS EBS 上部署 WAR 文件?

问题描述

我找到了这个答案https://serverfault.com/a/822596/123651

我需要\.ebextensions\nginx\conf.d\elasticbeanstalk\force-https.conf在我的 WAR 中包含一个随机文件以部署在 AWS ElasticBeanstalk 控制台上。如何在mvn package命令中包含此文件?ElasticBeanstalk 将如何在 WAR 中读取此文件?

我尝试使用本指南并添加到我的pom.xml

<build>
  ...
    <resources>
      <resource>
        <directory>.ebextensions/nginx/conf.d/elasticbeanstalk/</directory>
        <includes>
            <include>force-https.conf</include>
        </includes>
      </resource>
    </resources>
</build>

然后跑了mvn package -DskipTeststar tvf target\app-0.0.3-SNAPSHOT.war | less但它把文件放在错误的地方!

-rw-rw-r--  0 0      0          94 Nov 14 12:40 WEB-INF/classes/force-https.conf

标签: javaamazon-web-servicesmaventomcatamazon-elastic-beanstalk

解决方案


这行得通。仅制作 2 个文件的 ZIP 似乎过于冗长。

pom.xml
        <plugin> <!-- To add .ebextensions/ Nginx config for ElasticBeanstalk -->
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <descriptors>
              <descriptor>assembly.xml</descriptor>
            </descriptors>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>           
assembly.xml
<assembly 
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*-SNAPSHOT.war</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/.ebextensions/nginx/conf.d/elasticbeanstalk/</outputDirectory>
      <includes>
        <include>force-https.conf</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

并且配置文件就在项目根目录下。我不知道该放在哪里——它不是源代码。

force-ssl.conf
if ($http_x_forwarded_proto = 'http') {
    return 301 https://$host$request_uri;
}

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html


推荐阅读