简体   繁体   English

如何使用 maven 在战争中包含特定文件

[英]How to include specific files in war using maven

I'm having some trouble trying to figure out a good way to control which resources to include in a war using maven. In my src/main/resources directory I have two files, logback.xml and logback-test.xml.我在尝试找出一种使用 maven 控制战争中包含哪些资源的好方法时遇到了一些麻烦。在我的 src/main/resources 目录中,我有两个文件,logback.xml 和 logback-test.xml。 What I want to be able to do is direct maven which of the two to include in the classpath based on the current maven profile.我希望能够做的是直接 maven 根据当前的 maven 配置文件将两者中的哪一个包含在类路径中。 My initial thought was to define a property called log.config and then use that property in the configuration of maven-war-plugin.我最初的想法是定义一个名为 log.config 的属性,然后在 maven-war-plugin 的配置中使用该属性。 Something like:就像是:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
            <configuration>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <includes>                          
                            <include>${log.config}</include>                                    
                        </includes>                             
                    </resource>
                </webResources>
            </configuration>
      </plugin>

and a profile like和像这样的个人资料

<profile>
        <id>test</id>
        <properties>
            <log.config>logback-test.xml</log.config>
        </properties>
</profile>

If I run the command如果我运行命令

mvn clean package -P test mvn clean package -P 测试

I still end up with both versions of the logback file in the resulting war.在由此产生的战争中,我仍然得到了两个版本的 logback 文件。 I also tried using我也试过使用

<packagingIncludes>${log.config}</packagingIncludes>

with the same results.结果相同。

What am I doing wrong here?我在这里做错了什么? Is there a better way to include/exclude files without having to copy/paste the maven-war-plugin definition into all my profiles?有没有更好的方法来包含/排除文件而不必将 maven-war-plugin 定义复制/粘贴到我的所有配置文件中?

You can try pushing resources in the build though the profile (or exclude some):您可以尝试通过配置文件在构建中推送资源(或排除一些资源):

<profiles>
      <profile>
         <activation>
            <activeByDefault>true</activeByDefault>
         </activation>
         <id>Acceptance</id>
         <properties>
            <myDatabase.url>jdbc:informix-sqli://Adbhost:Aservice/Adatabase:INFORMIXSERVER=Aifxserver;IFX_LOCK_MODE_WAIT=1</myDatabase.url>
            <myDatabase.username>usernamea</myDatabase.username>
            <myDatabase.password>pass</myDatabase.password>
         </properties>
         <build>
            <resources>
               <resource>
                  <directory>resources</directory>
                  <includes>
                     <include>keypair_rsa_acceptance.p12</include>
                     <include>logback-test.xml</include>
                  </includes>
                  <excludes>
                     <exclude>logback.xml</exclude>
                  </excludes>
               </resource>
            </resources>
         </build>
      </profile>
      <profile>
         <activation>
            <activeByDefault>false</activeByDefault>
         </activation>
         <id>Production</id>
         <properties>
            <myDatabase.url>jdbc:informix-sqli://Pdbhost:Pservice/Pdatabasep:INFORMIXSERVER=Pifxserver</myDatabase.url>
            <myDatabase.username>usernamep</myDatabase.username>
            <myDatabase.password>pass</myDatabase.password>
         </properties>
         <build>
            <resources>
               <resource>
                  <directory>resources</directory>
                  <includes>
                     <include>keypair_rsa_production.p12</include>
                     <include>logback.xml</include>
                  </includes>
                  <excludes>
                     <exclude>logback-test.xml</exclude>
                  </excludes>
               </resource>
            </resources>
         </build>
      </profile>
   </profiles>

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

相关问题 如何使用 maven 将 Java 项目的 python 文件包含到 .war 中? - How to include python files of Java project into .war with maven? 如何在使用maven构建的war中包含系统依赖项 - How to include system dependencies in war built using maven 如何使用Ant构建将Maven依赖库包含在war文件中 - How to include maven dependency libraries in war file using Ant build 如何使用Maven在GWT应用程序的war目录中生成javascript文件? - How to generate the javascript files in the war directory of GWT application using Maven? 如何使用Maven在WAR,WEB-INF / lib目录中包含特定的jar - How to Include specific jars in WAR, WEB-INF/lib directory with maven 在创建war / jat时,Maven会忽略资源文件。 如何迫使它包括他们? - Maven is ignoring resource files when creating the war/jat. How to force it to include them? 使用Maven自动执行自定义War文件 - Automate custom war files using maven 使用Eclipse和Maven的战争文件的不同版本 - Different versions of war files using eclipse and maven 如何使用yuicompressor-maven-plugin让maven与缩小的文件建立战争 - How to get maven to build a war with minified files using yuicompressor-maven-plugin 如何使用Maven在WAR档案WEB-INF / lib中包含JAR? - How to include JARs in WAR archive WEB-INF/lib using Maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM