Exec Maven Plugin Comprehensive parsing and usage examples

Source: Internet
Author: User

1. Why use exec?


Today's projects often rely on a large number of jar packages, unlike the war package project, for those packaged in the form of a jar package of native Java applications, the Java command to start will be a very cumbersome thing, the reason is very simple, too much reliance on the parameter-classpath to become abnormal terror. To do this, when you publish an app, you typically use two methods to start the application: one is to package the project and all of its dependent jar packages into a separate jar package (two plugins in Maven assemly and shade are used to do this); Another method is to write a Run.bat file that contains a Java command to launch the application, and it is clear that the classpath of the command must contain all dependent jar packages. But for applications that are still in the development phase, the first approach involves extracting and repackaging all the jar packages, which can be time-consuming, especially when the project is very large. The problem with the second approach is that it is often necessary to introduce or upgrade jar packages for projects in the development phase, which requires frequent modifications to the Run.bat file. In fact, for projects that use MAVEN management, it is entirely possible to get the project's classpath through MAVEN, simplifying the application's startup commands, which is the main motivation that MAVEN plug-in exec has designed. The biggest advantage of using exec to start an application with Java commands is that you don't have to worry about-classpath.

2. The difference between exec:exec and Exec:java

exec consists mainly of two goal: Exec:exec and Exec:java. How should you choose? First of all, you need to remember that exec:exec is always stronger and more flexible than Exec:java, which is reflected in the following example, except that the main difference between the two is thread management: Exec:exec always starts a new one, and only the Guardian thread is left. Time to exit from the VM (close the application). For Exec:java, when all non-daemon threads end, the daemon thread is joine or interrupt, and the program should not be closed. But for average users, this difference is not important. For the choice of both, in general, if your project start is very simple, do not need to set the JVM parameters, System Properties, command line parameters, then use Exec:java, you only need to specify the MainClass, everything is OK. For example This section of the 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 starts very complex, you need to set JVM parameters, System Properties, command-line arguments, and so on, then you need to use exec:exec, let's look at a exec:exec "good" "Big" "full" example.

3. An example of a "good" "Big" "full"

Let's say that our application is started by this Java command:


Java-dsystemproperty1=value1-dsystemproperty2=value2-xx:maxpermsize=256m-classpath .... com.yourcompany.app.Main Arg1 arg2


This start command has set the necessary system properties for the application systemProperty1 and SystemProperty2, then set a JVM parameter, followed by the program's Classpath, .... The omitted part is I do not say you can also think of how long the classpath, and then the program entrance-the class name of the main class, Arg1 Arg2 is passed to the application command line arguments.


3.1. Configure in XML:

First, let's take a look at how to implement this startup command in the POM by 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. < 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 attribute parameter -

  9. < argument > -dsystemproperty2=value2</argument> <!-- This is a system attribute parameter --

  10. < argument > -xx:maxpermsize=256m</argument> <!--This is a JVM parameter--

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

  12. Also adding the project build directory->

  13. < argument >-classpath</argument> <!--This is the Classpath property, and its value is the following < Classpath/> --

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

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

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

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

  18. </ arguments >

  19. </ Configuration >

  20. </ plugin >


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


MVN exec:exec


You can start the application.


3.2. Configure on the command line:

In addition to writing in the Pom configuration file, exec also supports a more flexible command line to start, you can only declare in your POM to introduce the exec plug-in, do not provide any configuration content, and then set the relevant parameters on the command line, as an example of the above command, if using the command line to configure, Then this 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 "


What, does it look more concise?


Note: Exec.args refers to the exec:exec CommandLineArgs parameter, and our XML configuration above the parameters are arguments, the two are not the same, this practice is better, Because EXEC rules: if there is a CommandLineArgs, will take precedence over the use of CommandLineArgs, if not to find whether to configure the argument, so give us the command line to execute different parameters of the opportunity. The following is a description of this from the EXEC website:

1.If CommandLineArgs is specified, it'll be used as are, except for replacing%classpath with proper classpath using Depe Ndencies
2.Otherwise If the property Exec.args is specified, it'll be used
3.Otherwise the list of argument and classpath would be parsed and used


Exec Maven Plugin Comprehensive parsing and usage examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.