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

1

5

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.

Enthusiast answered 20/2, 2017 at 6:30 Comment(1)
This might also help if the .jar download location doesn't matter for the plugin. So that we can add that as a plugin dependency. mojohaus.org/exec-maven-plugin/examples/…Enthusiast
O
5

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.

Orientalism answered 20/2, 2017 at 9:33 Comment(5)
Great (Y) This seems like the solution for the problem. Thank you.Enthusiast
Btw can we achieve this for mvn exec:java , so that we can set the Main class in the pom.Enthusiast
@Enthusiast Have a look at the example for exec:java.Orientalism
great. About your note regarding having the same class in project and .jar I did found this though. mojohaus.org/exec-maven-plugin/examples/…Enthusiast
@Enthusiast It depends what you want to achieve. In your question you told about an external Jar file which is in no kind part of the project. The link in your comment speaks about a Maven managed dependency.Orientalism

© 2022 - 2024 — McMap. All rights reserved.