使用Maven构建jar时如何添加命令行参数

ECH*_*ECH 5 java maven

我可以在命令行中通过运行以下命令来添加命令行参数:

java --my-command-line-argument argumentValue -jar myJarFile.jar
Run Code Online (Sandbox Code Playgroud)

我首先是mvn clean package用来构建.jar的,是否可以在pom.xml中更改配置值,以便在我刚运行时添加此参数java -jar myJarFile.jar?我尝试使用以下方法在pom.xml中添加属性:

<properties>
  <my-command-line-argument>argumentValue</my-command-line-argument>
<properties>
Run Code Online (Sandbox Code Playgroud)

此外,仅在使用Java 9时才需要此参数(称为Add-Exports)。我还尝试添加:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
        <execution>
            <id>name</id>               
            <configuration>
                <goals>
                    <goal>compile</goal>
                </goals>
                <source>9</source>
                <target>9</target>
                <compilerArgs>
                    <arg>--my-command-line-argument</arg>
                    <arg>argumentValue</arg>
                </compilerArgs>
            </configuration>
        </execution>
    </executions>           
</plugin>
Run Code Online (Sandbox Code Playgroud)

哪个可以编译,但是没有达到预期的效果。

Mar*_*gon 1

看看https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#commandlineargfile

理想/官方的解决方案是使用 @args 文件,您可以将其与 jar 一起移植或分发。

使用您的参数 @argFile 调用创建一个文件

java/c @argFile something.jar
Run Code Online (Sandbox Code Playgroud)

文件 argFile (无扩展名)将包含任何内容,例如

--patch-module java.transaction=~/.m2\repository/javax/transaction/javax.transaction-api/1.3/javax.transaction-api-1.3.jar --patch-module java.xml.bind=~/.m2\repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar
Run Code Online (Sandbox Code Playgroud)

如果确实需要,您还可以创建一个类文件。IntelliJ 和 Netbeans 开箱即用地支持这一点。

尝试将所有导出/使用/提供/打开等放在 module-info.java 中,而不是使用命令行。

将命令行参数保留为只能在命令行上定义的内容。patch-module add-module 等,像“Add-Exports”之类的东西绝对应该在 module-info.java 类中定义。