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



我从命令行传递以下参数,并希望在我的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"

当调用Main类时,希望在Main类中加载三个属性文件。

 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插件的特定exec.arguments参数,以便将这些参数直接输入到您的主方法中。请参阅此处的插件文档:http://www.mojohaus.org/exec-maven-plugin/java-mojo.html

作为pom.xml 构建部分插件配置的一部分,这很容易配置

这里有一个例子:

        <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>

最新更新