Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn exec:java to run a java file in an external JAR file

In the pom.xml there is a usage of maven-dependency-plugin to download a specific external JAR file to a separate location (in /tmp/externalTestJars/testjar.jar).

And I want to use exec-maven-plugin to run a java class in the testjar.jar file (Main.java).

I found this SO question asking kinda same question but the answer for that question did not help me.

If I directly run the Main.java file (in the original project where the .jar got created, using mvn exec:java) I can use the below pom configuration.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
    <mainClass>org.example.Main</mainClass>
    <!-- need to pass two arguments to the Main.java file main method -->
    <arguments>
        <argument>arg one</argument>
        <argument>arg two</argument>
    </arguments>
</configuration>
</plugin>

In the above SO question it has an answer like below to run a java file inside a .jar file.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
    <mainClass>org.example.Main</mainClass>
    <arguments>
        <argument>-jar</argument>
        <argument>/tmp/externalTestJars/testjar.jar</argument>
    </arguments>
</configuration>
</plugin>

But in my case those arguments will be considered as the one to pass for the main method in Main.java sine it is expecting two arguments. So that approach didn't work for me.

Can this be done using exec maven plugin or is there any other method to do the same.

like image 608
prime Avatar asked Feb 20 '17 06:02

prime


1 Answers

If you want to run the class similar to java -cp /tmp/externalTestJars/testjar.jar org.example.Main the plugin should be configured as below.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-cp</argument>
            <argument>/tmp/externalTestJars/testjar.jar</argument>
            <argument>org.example.Main</argument>
        </arguments>
    </configuration>
</plugin>

If you want to run the class similar to java -jar /tmp/externalTestJars/testjar.jar (assuming org.example.Main is defined as Main-Class in the MANIFEST.MF) the plugin should be configured as below.

<configuration>
    <executable>java</executable>
    <arguments>
        <argument>-jar</argument>
        <argument>/tmp/externalTestJars/testjar.jar</argument>
    </arguments>
</configuration>

In both cases run it with mvn exec:exec

edit: An example for using mvn exec:java.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>org.example.Main</mainClass>
        <additionalClasspathElements>
            <additionalClasspathElement>
                /tmp/externalTestJars/testjar.jar
            </additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>

note: If the project and the jar file testjar.jar both contain the class org.example.Main then the class from the project will be executed. As the classpath elements defined by additionalClasspathElement will be appended to the projects classpath.

like image 115
SubOptimal Avatar answered Sep 22 '22 22:09

SubOptimal