Java 将 Maven 文件过滤为 WEB-INF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1782352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:35:54  来源:igfitidea点击:

Filtering Maven files into WEB-INF

javamaven-2pom.xml

提问by Xetius

I am trying to add some filtering to the application context file, which resides in the WEB-INF directory.

我正在尝试向驻留在 WEB-INF 目录中的应用程序上下文文件添加一些过滤。

I have the file which is to be filtered (xmlgateway-context.xml) in the folder /src/main/resources.

我在文件夹/src/main/resources 中有要过滤的文件(xmlgateway-context.xml)。

I have the properties files (config-e05.properties) in the folder src/main/filters

我在文件夹 src/main/filters 中有属性文件 (config-e05.properties)

And I have the POM set up as follows:

我的 POM 设置如下:

<!-- environment profiles -->
<profiles>
 <profile>
  <id>e04</id>
  <properties>
   <targetenv>e04</targetenv>
  </properties>
 </profile>
 <profile>
  <id>e05</id>
  <properties>
   <targetenv>e05</targetenv>
  </properties>
 </profile>
</profiles>

<!-- build settings (filtering) -->
<build>
 <filters>
  <filter>src/main/filters/config-${targetenv}.properties</filter>
 </filters>
 <resources>
  <resource>
   <targetPath>WEB-INF</targetPath>
   <filtering>true</filtering>
   <directory>src/main/resources</directory>
  </resource>
 </resources>
</build>

This will mvn install correctly, but when I open the output war file, I was expecting the file xmlgateway-context.xml to be in the /WEB-INF directory, but it ends up in the folder /WEB-INF/classes/WEB-INF.

这将正确安装 mvn,但是当我打开输出 war 文件时,我希望文件 xmlgateway-context.xml 位于 /WEB-INF 目录中,但它最终位于 /WEB-INF/classes/WEB 文件夹中-INF。

How can I get this file into the right place.

我怎样才能把这个文件放到正确的地方。

Alternatively, can I put the application context into a different location and have it referenced there.

或者,我可以将应用程序上下文放在不同的位置并在那里引用它。

采纳答案by peakit

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <webResources>
                <resource>
                    <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                    <filtering>true</filtering>
                    <targetPath>WEB-INF</targetPath>
                    <includes>
                        <include>**/xmlgateway-context.xml</include>
                     </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>
</plugins>

Add the above to your pom.xml. EDIT:Just to explain what the above conf is doing. With this added, mvn is going to filter files under src/main/webapp/WEB-INFand in particular filter the included files xmlgateway-context.xmland after filtering it is going to push the files in WEB-INFfolder (thats what the targettag is saying).

将上述内容添加到您的 pom.xml 中。 编辑:只是为了解释上面的 conf 在做什么。添加后,mvn 将过滤文件src/main/webapp/WEB-INF,特别是过滤包含的文件xmlgateway-context.xml,过滤后它将推送文件WEB-INF夹中的文件(这就是target标签所说的)。

Update if something is not clear.

如果有不清楚的地方更新。

回答by dfa

you should configure filtering via the maven war plugin: checkout these examples.

您应该通过maven war 插件配置过滤:查看这些示例

回答by EJB

Put the file xmlgateway-context.xml in src/main/webapp/WEB-INF and configure like this:

将文件 xmlgateway-context.xml 放入 src/main/webapp/WEB-INF 并配置如下:

<build>
    <filters>
        <filter>src/main/filters/config-${targetenv}.properties</filter>
    </filters>
    <resources>
        <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp/WEB-INF</directory>
        </resource>
    </resources>
</build>

回答by Igor Vukovi?

With filteringDeploymentDescriptors set to true

将过滤部署描述符设置为 true

     <build>
        <finalName>web-project-name</finalName>
        <filters>
            <filter>src/main/resources/app.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>           
        </plugins>
    </build>