Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven filtering not select the good files

Tags:

java

maven

My project structure:

- src
    - main
    - java
    - resources
    |   -hibernate.cfg.xml
    |   -log4j.properties
    - config
    |   -dev
    |   |   -hibernate.cfg.xml
    |   |   -log4j.properties

I use maven-war-plugin for filtering with 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>

Output console:

[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]

My problem is when i see in the war file hibernate.cfg.xml and log4J.properties are not those of the dev profile but those resources folder, why ..?

like image 750
Mercer Avatar asked Mar 25 '16 10:03

Mercer


People also ask

Which file extensions are excluded for resource filtering by default in Maven?

The plugin will prevent binary files filtering without adding some excludes configuration for the following file extensions jpg , jpeg , gif , bmp and png . If you like to add supplemental file extensions this can simply achieved by using a configuration like the following: <project>

What does Maven filtering do?

Using Maven resource filtering you can reference Maven properties and then use Maven profiles to define different configuration values for different target deployment environments.

What is true about resource filter?

Resource filters are the first to handle a request after authorization. They can run code before the rest of the filter pipeline, and after the rest of the pipeline has completed. Resource filter is useful to implement caching or otherwise short-circuit the filter pipeline for performance reasons.

What are maven properties?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.


1 Answers

Filtering/Including/Excluding in maven-war-plugin is for web resources. For main resources use maven-resources-plugin. Put something like below into your profile. To include files from your custom directory.

<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>

Note that here <filtering> refers to variable filtering in your resources.

like image 120
Aleksandr M Avatar answered Oct 06 '22 03:10

Aleksandr M