从Maven传递命令行参数作为pom.xml中的属性
是否可以从命令行将参数传递给pom.xml
文件中的属性? 比如我跑mvn ... argument
从Maven传递命令行参数作为pom.xml中的属性
,并在pom.xml中
<properties>
<myproperty> here should add argument from command line</myproperty>
</properties>
谢谢你的帮助。
为了您的财产例如做:围绕全属性定义
mvn install "-Dmyproperty=my property from command line"
注意引号。如果你的财产包含空间,你将需要他们。
还要注意,如果你在pom和命令行中都有一个属性,那么命令行需要优先。这对提供可覆盖的默认值很有用。 –
里面的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而没有任何参数将执行10个默认配置文件。
当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]
我想在mvn命令中提供浏览器名称和环境。如果我不提供它会选择默认。怎么做? – paul
我使用的属性插件来解决这个问题。
属性在pom中定义,并写入到my.properties文件中,然后可以从Java代码访问它们。
在我的情况下,测试代码需要访问这个属性文件,所以在POM属性文件写入到Maven的testOutputDirectory:如果你想要的属性是通过您的访问
<configuration>
<outputFile>${project.build.testOutputDirectory}/my.properties</outputFile>
</configuration>
使用输出目录应用代码:
<configuration>
<outputFile>${project.build.outputDirectory}/my.properties</outputFile>
</configuration>
对于那些寻找一个更全面的例子(我花了一点摆弄得到这个工作,因为我不明白的属性标签的命名是如何影响到在POM文件的其他地方获取这些能力),我的pom看起来像f ollows:
<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");
不能直接你问什么,但[行家配置文件](http://maven.apache.org/guides/introduction/introduction-to-profiles.html)可能对这个 – Sig
是有用的,我知道配置文件。我正在使用maven-soapui-plugin,其中 ... projectFile>是项目的定义名称。我有大约10个项目,我不想为每个项目提供新的配置文件。我想用参数来运行mvn ... project1运行project1和mvn ... project2运行project2 –
hudi