Search code examples
mavenmaven-resources-plugin

How to instruct Maven to copy compiled EAR on linux OR windows to a OS-specific directory?


I'm having a POM file which is currently copying the compiled EAR file to a linux directory.

I mean to do the same for my Windows developers, without having them to manually adapt the output directory. Thus, the maven resource plugin should determine the path (/projects/shared_ears for Linux and D:\projects_bin\shared_ears for Windows) by the operating system. Can it be done with the maven-resources-plugin? Saying, without using the maven ant run plugin?

Below the existing copy command applicable for linux OS only.

Thanks for any smart idea.

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>/projects/shared_ears</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}</directory>
                        <includes>
                            <include>project-${project.version}.ear</include>
                        </includes>
                    </resource>
                </resources>
                <overwrite>true</overwrite>
            </configuration>
        </execution>

    </executions>
</plugin>

Solution

  • The simplest way to do this is to use maven profiles

    One way to activate a profile is to use the os (operating system). You have to define a property. Let's say <ear.directory>/projects/shared_ears</ear.directory> and use this property in your plugin configuration:

            <configuration>
                <outputDirectory>${ear.directory}</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}</directory>
                        <includes>
                            <include>project-${project.version}.ear</include>
                        </includes>
                    </resource>
                </resources>
                <overwrite>true</overwrite>
            </configuration>
    

    Now, you can specify this property in 2 distinct profiles and activate one profile or the other based on the current operating system. Here is how to do it:

    <profiles>
      <profile>
        <id>WinProfile</id>
        <activation>
          <os>
            <family>windows</family>
          </os>
        </activation>
        <properties>
          <ear.directory>D:\projects_bin\shared_ears</ear.directory>
        </properties>
        ...
      </profile>
      <profile>
        <id>UnixProfile</id>
        <activation>
          <os>
            <family>unix</family>
          </os>
        </activation>
        <properties>
          <ear.directory>/projects/shared_ears</ear.directory>
        </properties>
        ...
      </profile>
    </profiles>
    

    Additionnal info about profile and os detection here