[Transfer] Exec Maven Plugin comprehensive analysis and usage examples

Exec Maven Plugin comprehensive analysis and usage examples

1. Why use exec?


Today's projects often rely on many jar packages, unlike war package projects, for those local java applications packaged in the form of jar packages, it will be extremely cumbersome to start through the java command, the reason is very simple, too many Dependency makes the parameter -classpath terrifying. For this reason, when publishing an application, two methods are generally used to start the application: one is to package the project and all its dependent jar packages into an independent jar package through tools (there are two plug-ins assemly and shade in maven is used to complete this kind of work); another method is to write a run.bat file, the file contains a java command to start the application, obviously, the classpath of this command must contain all dependent jar packages. But for applications that are still in the development stage, the first method requires decompressing and repacking all jar packages, so it is very time-consuming, especially when the project is very large. The problem with the second method is that, for projects in the development stage, it is often necessary to introduce or upgrade jar packages, which requires frequent modification of the run.bat file. In fact, for projects managed by maven, the classpath of the project can be obtained through maven, which simplifies the application startup command. This is the main motivation for the maven plugin exec to be designed. The biggest advantage of using exec over using the java command to start the application is that you don't need to worry about -classpath anymore.

2.  Difference between exec:exec and exec:java

exec is mainly composed of two goals: exec:exec and exec:java. How should you choose? First of all, you need to remember that exec:exec is always more powerful and flexible than exec:java, which will be shown in the following examples, other than that, the main difference between the two is in thread management: exec:exec Always start a new thread and exit from the VM (close the application) when only the daemon thread is left. For exec:java, when all non-daemon threads end, the daemon thread will be joined or interrupted, and the program should not be closed. But for the average user, this difference is not important. For the choice between the two, in general, if your project is very simple to start, and you don't need to set jvm parameters, system properties, and command line parameters, then use exec:java, you only need to specify mainClass, and everything will be OK. For example this configuration:

[html] view plaincopy

  1. <plugin>  

  2.     <groupId>org.codehaus.mojo</groupId>  

  3.     <artifactId>exec-maven-plugin</artifactId>  

  4.     <version>1.2.1</version>  

  5.     <executions>  

  6.         <execution>  

  7.             <goals>  

  8.                 <goal>java</goal>  

  9.             </goals>  

  10.         </execution>  

  11.     </executions>  

  12.     <configuration>  

  13.         <mainClass>com.yourcompany.app.Main</mainClass>  

  14.     </configuration>  

  15. </plugin>  


If on the contrary, your application startup is very complicated, you need to set jvm parameters, system properties, command line parameters, etc., then you need to use exec:exec, let's look at an exec:exec "good" "big"" full" example.

3. An example of "good", "big" and "full"

Suppose our application is started with a java command like this:

 

java -DsystemProperty1=value1 -DsystemProperty2=value2 -XX:MaxPermSize=256m -classpath .... com.yourcompany.app.Main arg1 arg2

 

This startup command successively sets the necessary system properties systemProperty1 and systemProperty2 for the application, and then sets a jvm parameter, followed by the classpath of the program, .... The omitted part is that you can imagine how verbose it will be without saying it. The class path is followed by the program entry - the class name of the main class, arg1 and arg2 are the command line parameters passed to the application.


3.1. Configuration in xml:

First, let's take a look at how to implement this startup command through configuration in pom:

[html] view plaincopy

  1. <plugin>  

  2.     <groupId>org.codehaus.mojo</groupId>  

  3.     <artifactId>exec-maven-plugin</artifactId>  

  4.     <version>1.2.1</version>  

  5.     <configuration>  

  6.         < executable > java </ executable >  <!-- executable refers to what kind of command to execute -->  

  7.         <arguments>  

  8.             < argument > -DsystemProperty1 = value1 </ argument >  <!-- This is a system property argument -->  

  9.             < argument > -DsystemProperty2 = value2 </ argument >  <!-- This is a system property argument -->  

  10.             < argument > -XX:MaxPermSize = 256m </ argument >  <!-- This is a JVM argument -->  

  11.             <!--automatically creates the classpath using all project dependencies,   

  12.                 also adding the project build directory -->  

  13.             < argument > -classpath </ argument >  <!-- This is the classpath attribute whose value is the following <classpath/> -->  

  14.             < classpath />  <!-- This is the most valuable part of the exec plugin. The classpath of the project does not need to be specified manually, it will be automatically calculated by exec -->  

  15.             < argument > com.yourcompany.app.Main </ argument >  <!-- program entry, main class name -->  

  16.             < argument > arg1 </ argument >  <!-- the first command line argument to the program -->  

  17.             < argument > arg2 </ argument >  <!-- the second command line argument to the program -->  

  18.         </arguments>  

  19.     </configuration>  

  20. </plugin>  

 

Add the above configuration to the pom and save it, then execute:

 

mvn exec:exec

 

The application is ready to be launched.


3.2. Configure on the command line:

In addition to writing in the pom configuration file, exec also supports a more flexible command line method to start. You can only declare the introduction of the exec plugin in your pom without providing any configuration content, and then set the relevant parameters on the command line, the same Taking the above command as an example, if you use the command line to configure, then the command will be:

 

mvn exec:exec -Dexec.executable="java" -Dexec.args="-DsystemProperty1=value1 -DsystemProperty2=value2 -XX:MaxPermSize=256m -classpath %classpath com.yourcompany.app.Main arg1 arg2"

 

How, doesn't it look more concise?


Note: exec.args refers to the commandlineArgs parameter of exec:exec, and the parameter in our xml configuration above is arguments, the two are different, this approach is better, because exec stipulates: if there is commandlineArgs, it will be Use commandlineArgs first. If you don't find out whether the argument is configured, this gives us the opportunity to execute different set parameters on the command line. The following are the relevant instructions on the exec official website:

1.If commandLineArgs is specified, it will be used as is, except for replacing %classpath with proper classpath using dependencies
2.Otherwise if the property exec.args is specified, it will be used
3.Otherwise the list of argument and classpath will be parsed and used

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326124475&siteId=291194637