我的项目结构:
- src
- main
- java
- resources
| -hibernate.cfg.xml
| -log4j.properties
- config
| -dev
| | -hibernate.cfg.xml
| | -log4j.properties
我使用 maven-war-plugin
通过 maven 进行过滤。
pom.xml:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileVersion>DEV</profileVersion>
<filterFile>src/main/filters/filter-dev.properties</filterFile>
<configFolder>src/main/config/dev/</configFolder>
</properties>
</profile>
</profiles>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<filters>
<filter>src/main/filters/filter.properties</filter>
<filter>${filterFile}</filter>
</filters>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<webResources>
<resource>
<directory>${configFolder}</directory>
<includes>
<include>log4j.properties</include>
<include>hibernate.cfg.xml</include>
</includes>
<targetPath>/WEB-INF/classes/</targetPath>
</resource>
</webResources>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>none</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
<execution>
<id>package-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
输出控制台:
[INFO] --- maven-war-plugin:2.6:war (package-war) @ with ---
[INFO] Packaging webapp
[INFO] Assembling webapp [with] in [D:\workspace\with\target\with-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp webResources [D:\workspace\with\src/main/config/dev/] to [D:\workspace\with\target\with-0.0.1-SNAPSHOT]
[INFO] Copying webapp webResources [D:\workspace\with\src/main/config/dev/] to [D:\workspace\with\target\with-0.0.1-SNAPSHOT]
[INFO] Copying webapp resources [D:\workspace\with\src\main\webapp]
[INFO] Webapp assembled in [13732 msecs]
[INFO] Building war: D:\workspace\with\target\with-0.0.1-SNAPSHOT.war
[INFO]
我的问题是当我在 war
文件中看到 hibernate.cfg.xml
和 log4J.properties
不是 dev
配置文件但是那些 resources
文件夹,为什么..?
最佳答案
在 maven-war-plugin
中过滤/包含/排除用于网络资源。对于主要资源使用 maven-resources-plugin
.将类似下面的内容放入您的个人资料中。包含自定义目录中的文件。
<profiles>
<profile>
<!-- ... -->
<build>
<resources>
<resource>
<directory>src/main/config/dev</directory>
<filtering>true</filtering>
<includes>
<include>hibernate.cfg.xml</include>
<include>log4j.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>hibernate.cfg.xml</exclude>
<exclude>log4j.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<!-- ... -->
</profile>
</profiles>
注意这里是<filtering>
指的是资源中的变量过滤。
关于java - Maven过滤没有选择好的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218384/