Passing command line arguments from Maven as properties in pom.xml

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.

230713 次浏览

对于您的属性示例,请做:

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

注意整个属性定义的引号。如果属性包含空格,就需要引号。

在 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>

.....

在本例中,当您运行没有任何参数的 pom 时,将执行 mvn clean install默认配置文件。

使用 mvn -Denv=win64 clean install执行时

将执行 win64配置文件。

请参阅 http://maven.apache.org/guides/introduction/introduction-to-profiles.html

可以将变量名称作为项目文件

<projectFile>${projectName}</projectFile>

然后在命令行上,您可以将项目名称作为参数传递:-

mvn [your-command] -DprojectName=[name of project]

我使用属性插件来解决这个问题。

属性在 pom 中定义,并写入 my.Properties 文件,然后可以从 Java 代码访问它们。

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>

如果您希望通过应用程序代码访问属性,请使用 outputDirectory:

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

对于那些想要一个更完整的例子的人来说(我花了一些时间才让它工作起来,因为我不明白属性标签的命名是如何影响在 pom 文件的其他地方检索它们的能力的) ,我的 pom 看起来如下:

<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>

在命令行上:

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

因此,可以从 Java 代码访问这些属性:

 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");
mvn clean package -DpropEnv=PROD

然后像这样在 POM.xml 中使用

<properties>
<myproperty>${propEnv}</myproperty>
</properties>