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

Pom.xml uses the maven-dependency-plugin to load a specific external JAR file into a separate location (in / tmp / externalTestJars / testjar.jar).

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

I found this SO question by asking the same question, but the answer to this question did not help me.

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

 <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 SO question above, he has an answer, as shown 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, these arguments will be considered as passed for the main method in the power of Main.java, it expects two arguments. So this approach did not work for me.

Is it possible to do this with an exec maven plugin or is there any other way to do the same.

0
source share
1 answer

If you want to run a class similar to java -cp /tmp/externalTestJars/testjar.jar org.example.Main , the plugin must be configured as shown 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 a class similar to java -jar /tmp/externalTestJars/testjar.jar (assuming org.example.Main is defined as Main-Class in MANIFEST.MF ), the plugin must be configured as shown below.

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

In both cases, run it using mvn exec:exec

edit: An example of 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 contain the class org.example.Main , then the class from the project will be launched. Because the classpath elements defined by additionalClasspathElement will be added to the project class path.

+1
source

Source: https://habr.com/ru/post/911936/


All Articles