Подтвердить что ты не робот

Фильтрация 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.xml и log4J.properties не те, которые имеют профиль dev, а те resources, почему..?

4b9b3361

Ответ 1

Фильтрация/включение/исключение в maven-war-plugin для ресурсов web. Для основных ресурсов используйте 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> ссылается на фильтрацию переменных в ваших ресурсах.

Ответ 2

Вы пропустили здесь 2 вещи.

Другое дело,

  1. Используя targetPath, вы переопределите каталог назначения по умолчанию до /WEB-INF/classes/. Но для выполнения вашей цели файлов в определенном каталоге, это не должно быть сделано. Поэтому удалите его в этом случае.

удалите его <targetPath>/WEB-INF/classes/</targetPath>

удалите эту часть из pom.xml

Связанная ссылка:

Я надеюсь, что это поможет вам решить вашу проблему.

В качестве полного примера вы можете следить за учебником