【问题标题】:Passing command line arguments from Maven as properties in pom.xml从 Maven 传递命令行参数作为 pom.xml 中的属性
【发布时间】:2011-11-22 17:18:55
【问题描述】:

是否可以将参数从命令行传递到pom.xml 文件中的属性? 例如我运行mvn ... argument

在 pom.xml 中

<properties>
   <myproperty> here should add argument from command line</myproperty>
</properties>

感谢您的帮助。

【问题讨论】:

  • 不是你想要的,但maven profiles 可能对此有用
  • 是的,我知道配置文件。我正在使用 maven-soapui-plugin 其中 ... 是项目的定义名称。我有大约 10 个项目,我不想为每个项目新的配置文件。我想使用参数运行 mvn ... project1 运行 project1 和 mvn ... project2 运行 project2

标签: maven


【解决方案1】:

对于您的财产示例,请执行以下操作:

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

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

【讨论】:

  • 还要注意,如果你在 pom 和命令行中都有一个属性,那么命令行优先。这对于提供可覆盖的默认值很有用。
  • 我们也可以像这样传递多个参数,例如:mvn clean install "-Dprop1=value1" "-Dprop2=value2"
【解决方案2】:

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

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

在我的例子中是测试代码需要访问这个属性文件,所以在 pom 中属性文件被写入 maven 的 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");

【讨论】:

  • 我在 java 中的属性文件给出的值与 ${app.env} 相同,它不是从 maven 命令行中选择的,属性名称是否应该等于这样的值? ${app.env}
【解决方案3】:

在 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

【讨论】:

  • 既然使用了哪个配置文件,应该是 mvn clean -Pwin64 吗?
【解决方案4】:

您可以将变量名称作为项目文件。例如,在您的插件配置中,只给出一个标签,如下所示:-

<projectFile>${projectName}</projectFile>

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

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

【讨论】:

  • 我想在 mvn 命令中提供浏览器名称和环境。如果我不提供它将选择默认值。该怎么做?
【解决方案5】:
mvn clean package -DpropEnv=PROD

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

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

【讨论】:

  • 如果我想通过这样的东西有没有办法? ${anypoint-platform-client-id-${suffix}} 并使用以下命令; mvn clean package -Dsuffix=dev -Danypoint-platform-client-id-dev=abc
猜你喜欢
  • 2016-05-14
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
相关资源
最近更新 更多