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

Asked

Viewed 933 times

5

In an environment with multiple configurations of Web Container sometimes we need to correctly point the deploy directory of the application 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 application instance on a machine (Test, Approval, Production, etc). For this we need to override a certain property defined in the POM that points to the deployment directory.

What’s the best way to do that ?

  • 1

    hi @Joao-Parana I am not required to use Profile in POM, right ? In case I use as it would in this your example ?

  • I edited the answer and put up the profile. It is not necessary to use profile but in a group work can help in factorizing the Build via Maven configuration files (POM parent, POM child, Settings.xml, etc)

1 answer

3

To overwrite a property defined in the POM use the parâmetro -D typical of any Java application when invoking the 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 Testing.

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

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

And so on and so forth.

The profile deployShared uses the maven-dependency-plugin and would look 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.