是否可以從命令行將參數傳遞給pom.xml
文件中的屬性? 比如我跑mvn ... argument
從Maven傳遞命令行參數作爲pom.xml中的屬性
,並在pom.xml中
<properties>
<myproperty> here should add argument from command line</myproperty>
</properties>
謝謝你的幫助。
是否可以從命令行將參數傳遞給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