How can I create a Maven build profile to conditionally copy files?

14,129

Solution 1

It worked after I added filtering= true

 <build>
<finalName>myacct_okc</finalName>
<resources>
 <resource>
  <directory>src/main/java</directory>
   <excludes>
    <exclude>**/*.java</exclude>
  </excludes>
 </resource>
 <resource>
   <filtering>true</filtering>
  <directory>src/main/resources</directory>
 </resource>
  <resource>
   <filtering>true</filtering>
    <directory>config/${environment}</directory>
  </resource>
  </resources>
 </build>

Solution 2

I believe you'll have to add your resources to the maven-resources-plugin, inside a copy-resources execution.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-config</id>
      <phase>copy-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
          <resource>
            <directory>src/main/resources</directory>
          </resource>
          <resource>
            <directory>config/${environment}</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>
Share:
14,129
NullPointerException
Author by

NullPointerException

Writing software code and developing apps. Supporter of open source technologies. There is nothing like spreading knowledge without charging !!

Updated on June 09, 2022

Comments

  • NullPointerException
    NullPointerException almost 2 years

    I new to maven and I am trying to create pom.xml to build the war files for different environment using profiles

    So I created the build target

    <build>
    <finalName>myacct_okc</finalName>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
      <resource>
        <directory>config/${environment}</directory>
      </resource>
     </resources>
    </build>
    

    And then created the profiles for each environment

    <profiles>
     <profile>
      <id>local</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <properties>
        <environment>local</environment>
      </properties>
    </profile>
    <profile>
      <id>jboss</id>
      <properties>
        <environment>jboss</environment>
      </properties>
    </profile>
    <profile>
      <id>dev</id>
      <properties>
        <environment>dev</environment>
      </properties>
    </profile>
    

    and I create a config folder for each env

    project root
    |-- src
    |   |-- main
    |   |   |-- java
    |   |   |-- resources
    |   |   |   |-- config.xml
    |   |   |   +-- config.properties
    |   |   |-- webapp
    |   |   |   |-- META-INF
    |   |   |   |   +--context.xml
    |   |   |-- config
    |   |   |   |-- local
    |   |   |   |   |--config.properties
    |   |   |   |   +--config.xml
    |   |   |   |-- jboss
    |   |   |   |   |--config.properties
    |   |   |   |   +--config.xml
    |   |   |   +-- dev
    |   |   |   |   |--config.properties
    |   |   |   |   +--config.xml
    +--pom.xml
    

    Now when I run this pom.xml with any profile for e.g. jboss, the files form config/jboss folder are not getting copied ( or I mean the files in src/main/resources are not getting replaced).

    when I enabled the debug on on maven build I can see the copy getting executed.

     [DEBUG] resource with targetPath null
     directory C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss
     excludes []
     includes []
     [DEBUG] ignoreDelta true
     [INFO] Copying 2 resources
     [DEBUG] file config.xml has a filtered file extension
     [DEBUG] copy C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss\config.xml to C:\Projects\workspace\myaccount_build_4\myaccount\target\classes\config.xml
     [DEBUG] file config.properties has a filtered file extension
     [DEBUG] copy C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss\META-INF\config.properties to    C:\Projects\workspace\myaccount_build_4\myaccount\target\classes\META-INF\config.properties
    

    But it is not replacing the files. The files are still the same as from src/main/resources.

    Maven version is 3.0.4

    Can someone help me what I am doing wrong ?

    I have looked at this question. This provides a solution but I want to override the files rather then excluding them and then copy.

  • NullPointerException
    NullPointerException about 11 years
    we are not using maven-resources plugin
  • bdkosher
    bdkosher about 11 years
    You may not be explicitly using it in your POM, but it's a core Maven plugin and it's being implicitly used during your build lifecycle. Configuring the plugin is a way to get the plugin to copy resources from your custom source locations.
  • JonnyRaa
    JonnyRaa almost 10 years
    filtering replaces $(something) in files with maven property values
  • JonnyRaa
    JonnyRaa almost 10 years
    absolute life-saver! I was thinking about this in terms of renaming based on profile but couldn't figure out how to do that. This is much more elegant. Thanks