java - 在 pom.xml 中指定默认参数并在命令行中覆盖它

标签 java xml maven pom.xml

我的主类的唯一功能是从我当前在命令行中指定的 xml 配置文件中读取。是否有可能在 pom.xml 中指定默认配置文件,因此如果程序从命令行运行而不传入参数,则读取该配置文件,但如果使用 -Dexec 传入参数.args 默认值被覆盖了吗?

最佳答案

先看exec插件文档here

在你的 maven 属性中指定你想要查看的默认属性 my.custom.property

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.debug>true</maven.compiler.debug>
        <!-- frameworks -->

        <!-- properties -->
        <my.custom.property>MAVEN</my.custom.property>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>test.Test</mainClass>
                    <arguments>
                        <argument>${my.custom.property}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这里是java类

package test;

import java.util.stream.Stream;


public class Test {

    public static void main(String[] args) {
        Stream.of(args)
                .forEach(arg -> System.out.println("ARGUMENT " + arg));
    }
}

如果您执行mvn compile exec:java,您应该会在某处看到outpu ARGUMENT MAVEN。如果您提供自己的论点,例如。 mvn compile exec:java -Dmy.custom.property=TEST 你应该在控制台的某处看到 ARGUMENT TEST。

关于java - 在 pom.xml 中指定默认参数并在命令行中覆盖它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45352887/

相关文章:

java - 将 maven 依赖项从 spring 3.0 更新到 3.1.1 并将 hibernate 3.6 更新到 4.0 后。很多错误即将到来

java - Java 是否允许您在 Mac OS X 上使用 native UI 小部件?

java - Ormlite Where 括号问题,构建元层查询库

java - 如何在 Groovy 代码中传递带有空格的字符串作为 JMeter 中的参数?

java - ClassCastException VectorDrawable 无法转换为 Animatable

JAVA 8 xml 漂亮输出无法正常工作

java - 如何让 OS X 识别驱动器盘符?

android - 开关元素未在分配的空间内水平居中对齐

java - Maven SonarQube 多模块

java - 复制资源在 Maven 资源插件中不起作用