Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add command line arguments when building jar with Maven

Tags:

java

maven

I can add a command line argument when running a .jar from the command line by running:

java --my-command-line-argument argumentValue -jar myJarFile.jar

I am using mvn clean package to build this .jar in the first place, is there a configuration value I can change in the pom.xml so that this argument will be added when I just run java -jar myJarFile.jar? I tried adding a property in the pom.xml with:

<properties>
  <my-command-line-argument>argumentValue</my-command-line-argument>
<properties>

Additionally, this argument is only required when using Java 9 (It's called Add-Exports). And I also tried adding:

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

Which compiles, but doesn't have the desired effect.

like image 363
ECH Avatar asked Nov 08 '22 12:11

ECH


1 Answers

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

The ideal/official solution is to use an @args file that you can port or distribute with your jar.

Create a file with your arguments @argFile call

java/c @argFile something.jar

the file argFile (no extension) would contain whatever, such as

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

you can also create a classes file if you really need to. IntelliJ & Netbeans supports this out the box.

Try to place all of your exports/uses/provides/opens etc in your module-info.java instead of using command lines.

Keep the command line arguments purely to what can only be defined on the command line. patch-module add-module etc, something like "Add-Exports" should most definitely be defined in your module-info.java class.

like image 51
Marc Magon Avatar answered Nov 14 '22 22:11

Marc Magon