How to override variable defined in the POM with specific value at the time of mvn execution?

5

In an environment with multiple Web Container settings we sometimes need to correctly point to the application deploy directory that may be in different directories depending on the Operating System used and the package manager (apt-get on Ubuntu, homebrew on Mac OS, etc.). In addition we may need to install more than one instance of the application on a machine (Test, Homologation, Production, etc.). For this we need to override a certain property defined in the POM that points to the deploy directory.

What is the best way to do this?

    
asked by anonymous 09.03.2014 / 19:04

1 answer

3

To override a property defined in the POM use the parâmetro -D typical of any Java application when invoking mvn

For example to deploy assets shared by applications to a TEST directory:

mvn -P deployShared package  -Dshared_dir=/usr/local/Cellar/tomcat/7.0.47/shared

In this example we are overwriting shared_dir during the package phase. This variable is used by the deployShared profile defined in the POM to copy the files to the Tomcat environment (version 7.0.47) used for Test .

The same POM can be used to deploy to the Homologation environment as follows:

mvn -P deployShared package -Dshared_dir=/usr/local/tomcat-7.0.47-homolog/shared

And so on.

The profile deployShared uses maven-dependency-plugin and looks like this:

    <profile>
        <id>deployShared</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.8</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>runtime</includeScope>
                                <outputDirectory>${shared_dir}</outputDirectory>
                                <overWriteReleases>false</overWriteReleases>
                                <overWriteSnapshots>false</overWriteSnapshots>
                                <overWriteIfNewer>true</overWriteIfNewer>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <outputDirectory>${shared_dir}</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    
09.03.2014 / 19:17