Using Maven 'exec:exec' with Arguments

房东的猫 提交于 2020-01-09 13:58:12

问题


I have a project configured to build and run with Maven. The project depends on platform specific native libraries, and I'm using the strategy found here to manage those dependencies.

Essentially, the .dll or .so files for a particular platform are packaged into a jar, and pushed to the Maven server with a classifier identifying the target platform. The maven-dependency-plugin then unpacks the platform specific jar, and copies the native libraries to the target folder.

Normally I would use mvn exec:java to run a Java program, but exec:java runs applications in the same JVM as Maven, which prevents me from modifying the classpath. Since the native dependencies must be added to the classpath, I am forced to use mvn exec:exec instead. This is the relevant snippet of the pom:

...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Djava.library.path=target/lib</argument>
            <argument>-classpath</argument>
            <classpath />
            <argument>com.example.app.MainClass</argument>
        </arguments>
    </configuration>
</plugin>
...

This works fine for the default configuration of the application, but I want to be able to specify some optional parameters at the command line. Ideally, I'd like to do something like this:

mvn exec:exec -Dexec.args="-a <an argument> -b <another argument>"

Unfortunately, specifying the exec.args variable overwrites the arguments I have in the pom (which are required to set up the classpath and run the application). Is there a way around this? What's the best way to specify some optional arguments at the command line without overwriting what I have in the pom?


回答1:


I managed to find a reasonably elegant solution to my problem using Maven environment variables.

The default values are defined as properties in the pom, and added to the exec plugin as arguments:

...
<properties>
    <argumentA>defaultA</argumentA>
    <argumentB>defaultB</argumentB>
</properties>
...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Djava.library.path=${project.build.directory}/lib</argument>
            <argument>-classpath</argument>
            <classpath />
            <argument>com.example.app.MainClass</argument>
            <argument>-a</argument>
            <argument>${argumentA}</argument>
            <argument>-b</argument>
            <argument>${argumentB}</argument>
        </arguments>
    </configuration>
</plugin>
...

Now I can run with default parameters exactly as I did before:

mvn exec:exec

And I can easily override the defaults for each argument at the command line using:

mvn exec:exec -DargumentA=alternateA -DargumentB=alternateB


来源:https://stackoverflow.com/questions/15013651/using-maven-execexec-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!