Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Java类访问maven-Dexec.args参数_Java_Maven - Fatal编程技术网

如何从Java类访问maven-Dexec.args参数

如何从Java类访问maven-Dexec.args参数,java,maven,Java,Maven,我从命令行传递以下参数,并希望在java文件中访问它们。任何伪代码都将非常有用 mvn clean package exec:java -Dexec.mainClass="com.test.trial.properties.Main" -Dexec.args="classpath:resources/jdbc.properties file:///tmp/system.properties http://localhost:8080/global.properties" 调用我的主类时,希望在

我从命令行传递以下参数,并希望在java文件中访问它们。任何伪代码都将非常有用

mvn clean package exec:java -Dexec.mainClass="com.test.trial.properties.Main" -Dexec.args="classpath:resources/jdbc.properties file:///tmp/system.properties http://localhost:8080/global.properties"
调用我的主类时,希望在我的主类中加载三个属性文件

 public static void main(String[] args) throws URISyntaxException, IOException {

   try{
       ##########what code should be here to access three argumets passed with maven command################
       System.out.println("here - " + args[0]) ;

 }

非常感谢您的帮助。

您可以使用
System.getProperty(“exec.args”)
在命令行上设置带有-D标志的任何环境变量


您还可以配置maven exec plugin的特定
exec.arguments
参数,以便将这些参数直接输入到主方法中。请参阅此处的插件文档:

这很容易配置为
pom.xml
build部分插件配置的一部分

下面是一个例子:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>${exec-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                 <arguments>
                        <argument>-Dmyproperty=myvalue</argument>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>com.MyMainClass</argument>
                        <argument>-a</argument>
                        <argument>${argumentA}</argument>        
                    </arguments>
            </configuration>
        </plugin>

org.codehaus.mojo
execmaven插件
${exec maven plugin.version}
执行官
JAVA
-Dmyproperty=myvalue
-类路径
com.MyMainClass
-a
${argumentA}

那很好。但是如何在Java中引用它。我在maven命令中传递了三个空格分隔的值,作为arg0 arg1 arg2。如何在java主类中访问它。一个示例代码将是救命稻草。