127

Is it possible to pass arguments from command line to properties in pom.xml file ? for example I run mvn ... argument

and in pom.xml

<properties>
   <myproperty> here should add argument from command line</myproperty>
</properties>

Thank you for the help.

2
  • Not directly what you're asking for but maven profiles may be useful for this
    – Sig
    Sep 22, 2011 at 10:42
  • yea I know about profiles. I am using maven-soapui-plugin where in <projectFile> ... </projectFile> is defineted name of project. I have about 10 project and I dont wanna for every project new profile. I wanna use argument to run mvn ... project1 to run project1 and mvn ... project2 to run project2
    – hudi
    Sep 22, 2011 at 11:26

5 Answers 5

175

For your property example do:

mvn install "-Dmyproperty=my property from command line"

Note quotes around whole property definition. You'll need them if your property contains spaces.

2
  • 27
    Note also, that if you have both a property in the pom, and on the command-line, the command-line takes precedence. This can be useful for providing overridable defaults.
    – dan carter
    Jun 30, 2015 at 1:53
  • 4
    We can also pass multiple arguments like this too, something like: mvn clean install "-Dprop1=value1" "-Dprop2=value2"
    – Sumit
    Feb 20, 2019 at 22:18
20

I used the properties plugin to solve this.

Properties are defined in the pom, and written out to a my.properties file, where they can then be accessed from your Java code.

In my case it is test code that needs to access this properties file, so in the pom the properties file is written to maven's testOutputDirectory:

<configuration>
    <outputFile>${project.build.testOutputDirectory}/my.properties</outputFile>
</configuration>

Use outputDirectory if you want properties to be accessible by your app code:

<configuration>
    <outputFile>${project.build.outputDirectory}/my.properties</outputFile>
</configuration>

For those looking for a fuller example (it took me a bit of fiddling to get this working as I didn't understand how naming of properties tags affects ability to retrieve them elsewhere in the pom file), my pom looks as follows:

<dependencies>
     <dependency>
      ...
     </dependency>
</dependencies>

<properties>
    <app.env>${app.env}</app.env>
    <app.port>${app.port}</app.port>
    <app.domain>${app.domain}</app.domain>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.testOutputDirectory}/my.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

And on the command line:

mvn clean test -Dapp.env=LOCAL -Dapp.domain=localhost -Dapp.port=9901

So these properties can be accessed from the Java code:

 java.io.InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");
 java.util.Properties properties = new Properties();
 properties.load(inputStream);
 appPort = properties.getProperty("app.port");
 appDomain = properties.getProperty("app.domain");
1
  • My property file in java is giving the same value as ${app.env} it is not picking it from maven command line, does property name should be equals to value like this? <app.env>${app.env}</app.env>
    – Sujith
    Aug 28, 2019 at 21:20
17

Inside pom.xml

<project>

.....

<profiles>
    <profile>
        <id>linux64</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build_os>linux</build_os>
            <build_ws>gtk</build_ws>
            <build_arch>x86_64</build_arch>
        </properties>
    </profile>

    <profile>
        <id>win64</id>
        <activation>
            <property>
                <name>env</name>
                <value>win64</value>
            </property>
        </activation>
        <properties>
            <build_os>win32</build_os>
            <build_ws>win32</build_ws>
            <build_arch>x86_64</build_arch>
        </properties>
    </profile>
</profiles>

.....

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>target-platform-configuration</artifactId>
    <version>${tycho.version}</version>
    <configuration>
        <environments>
            <environment>
                <os>${build_os}</os>
                <ws>${build_ws}</ws>
                <arch>${build_arch}</arch>
            </environment>
        </environments>
    </configuration>
</plugin>

.....

In this example when you run the pom without any argument mvn clean install default profile will execute.

When executed with mvn -Denv=win64 clean install

win64 profile will executed.

Please refer http://maven.apache.org/guides/introduction/introduction-to-profiles.html

1
  • Since using which profile, should it be mvn clean -Pwin64 ?
    – sendon1982
    Jun 15, 2020 at 4:48
8
mvn clean package -DpropEnv=PROD

Then using like this in POM.xml

<properties>
    <myproperty>${propEnv}</myproperty>
</properties>
1
  • Is there a way if i want to pass something like this; <properties> <anypoint.platform.client_id>${anypoint-platform-client-id-${suffix}}</anypoint.platform.client_id> </properties> and use the below command; mvn clean package -Dsuffix=dev -Danypoint-platform-client-id-dev=abc
    – Kaushik
    Jul 29, 2021 at 14:41
7

You can give variable names as project files. For instance in you plugin configuration give only one tag as below:-

<projectFile>${projectName}</projectFile>

Then on command line you can pass the project name as parameter:-

mvn [your-command] -DprojectName=[name of project]
1
  • I want to provide browser name and environment in mvn command. If I dont provide it will pick default. How to do that?
    – paul
    Nov 1, 2017 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.