Is it possible to pass arguments from command line to properties in pom.xml
file ? for example I run mvn ... argument
是否可以将参数从命令行传递到pom.xml文件中的属性?例如,我运行mvn ...参数
and in pom.xml
并在pom.xml中
<properties>
<myproperty> here should add argument from command line</myproperty>
</properties>
Thank you for the help.
感谢您的帮助。
4 个解决方案
#1
94
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
12
Inside pom.xml
在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.
在此示例中,当您运行没有任何参数的pom时,将执行mvn clean install默认配置文件。
When executed with mvn -Denv=win64 clean install
用mvn -Denv = win64清理安装执行时
win64 profile will executed.
win64配置文件将被执行。
Please refer http://maven.apache.org/guides/introduction/introduction-to-profiles.html
请参阅http://maven.apache.org/guides/introduction/introduction-to-profiles.html
#3
5
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.
属性在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:
在我的例子中,它是需要访问此属性文件的测试代码,因此在pom中,属性文件被写入maven的testOutputDirectory:
<configuration>
<outputFile>${project.build.testOutputDirectory}/my.properties</outputFile>
</configuration>
Use outputDirectory if you want properties to be accessible by your app code:
如果您希望应用代码可以访问属性,请使用outputDirectory:
<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:
对于那些寻找更完整的例子的人(由于我不理解属性标签的命名如何影响在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>
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代码访问这些属性:
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");
#4
2
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
94
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
12
Inside pom.xml
在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.
在此示例中,当您运行没有任何参数的pom时,将执行mvn clean install默认配置文件。
When executed with mvn -Denv=win64 clean install
用mvn -Denv = win64清理安装执行时
win64 profile will executed.
win64配置文件将被执行。
Please refer http://maven.apache.org/guides/introduction/introduction-to-profiles.html
请参阅http://maven.apache.org/guides/introduction/introduction-to-profiles.html
#3
5
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.
属性在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:
在我的例子中,它是需要访问此属性文件的测试代码,因此在pom中,属性文件被写入maven的testOutputDirectory:
<configuration>
<outputFile>${project.build.testOutputDirectory}/my.properties</outputFile>
</configuration>
Use outputDirectory if you want properties to be accessible by your app code:
如果您希望应用代码可以访问属性,请使用outputDirectory:
<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:
对于那些寻找更完整的例子的人(由于我不理解属性标签的命名如何影响在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>
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代码访问这些属性:
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");
#4
2
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]