125

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.

Sergio
  • 3,317
  • 5
  • 32
  • 51
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Not directly what you're asking for but [maven profiles](http://maven.apache.org/guides/introduction/introduction-to-profiles.html) may be useful for this – Sig Sep 22 '11 at 10:42
  • yea I know about profiles. I am using maven-soapui-plugin where in ... 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 '11 at 11:26

5 Answers5

167

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.

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • 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 '15 at 01:53
  • 4
    We can also pass multiple arguments like this too, something like: `mvn clean install "-Dprop1=value1" "-Dprop2=value2"` – Sumit Feb 20 '19 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");
Wiggle
  • 281
  • 3
  • 8
  • 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} – Sujith Aug 28 '19 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

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Abhishek2k6
  • 425
  • 5
  • 9
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]
Hariom Tomar
  • 111
  • 1
  • 6
  • 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 01 '17 at 15:23
7
mvn clean package -DpropEnv=PROD

Then using like this in POM.xml

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