java - Maven过滤没有选择好的文件

标签 java maven

我的项目结构:

- 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.xmllog4J.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/

相关文章:

java - 用homebrew安装bazel的问题

java - SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? AND password=?' 附近使用的正确语法

java - 如何计算字符串出现的次数

java - 如何在WebSphere中创建共享库来共享jar文件

java - Maven 无法在指定范围内解析项目 : No versions available for org. owasp.esapi :esapi:jar:[2. 0,3) 的依赖项

java - maven-jar-plugin 生成带有错误 MANIFEST.MF 的 jar

java - onStartLoading() 和forceLoad() 是在哪里调用和实现的?

java - 我想在 java applet 中使用 Robot 类来让网络浏览器移动并单击鼠标

java.lang.RuntimeException : Error starting org. neo4j.kernel.EmbeddedGraphDatabase

python - 使用 maven exec-maven-plugin 运行 "python -m unittest"失败