Exec Maven Plugin Running Java Programs With FreeBSD

When creating a project with Maven, we often need to run a Java file with the command line and pass arguments. However, this may be a little tricky because Maven uses a special directory structure and build process. In this article, we will run a Java file with the Maven exec plugin.

Exec Maven plugin is one of the plugins available in Maven, this plugin was developed by the MojoHaus project. Exec Maven Plugin helps us run Java and other programs using Maven.

The Maven exec plugin allows us to run Java systems and programs with maven commands. The maven exec plugin has three main functions:
  1. exec:exec, can be used to run any program in a separate process.
  2. exec:java, can be used to run Java programs on the same VM.
  3. exec:help, It displays information about this plugin.

Create a sample project

We will demonstrate the use of the Maven exec plugin using a sample project. We have created a simple Maven project for this job.

Run the mvn archetype:generate command to start working on the sample project with the maven exec plugin.
root@ns7:/var # mvn archetype:generate -DgroupId=com.unixwinbsd -DartifactId=maven-exec-args -DinteractiveMode=false
The project contains a single package, "App.java", which has a single class called App. The App class has a main method like this.

package com.unixwinbsd;

public class App 
    public static void main( String[] args ) {
        System.out.print(" args:");
        for (String s: args) {
            System.out.printf(" <%s>", s);

We will learn how to execute this code using Maven.

Create pom.xml

The first thing we need to do is add the maven exec plugin to the pom.xml file. Add the following code to the pom.xml file.


This code snippet should be included under the build -> plugins tag. The entire pom.xml script will look like this.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<!-- FIXME change it to the project's website -->


  <exec.args>exec.args property</exec.args>
  <!-- If both are set, exec.args takes priority -->


  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
      <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
      <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <commandlineArgs>"arg 1" 'arg 2' "O'Reilly"</commandlineArgs>
          <argument>argument 1</argument>
          <argument>argument 2</argument>

Compile the project

Compile the code before running, because exec:java will not compile the code automatically, you need to run the mvn compile manually to complete the code we have written.
root@ns7:/var/maven-exec-args # mvn compile && mvn package && mvn install
You can also compile using exec:java. To do this, we can use exec-maven-plugin. To be more specific, the exec:java target of this plugin executes the Java classes provided with the project dependencies attached as classpath. To execute the main class methodExec, we have to pass the full class name to the maven exec plugin.
root@ns7:/var/maven-exec-args # mvn compile exec:java -Dexec.mainClass="com.unixwinbsd.App.Exec"
root@ns7:/var/maven-exec-args # mvn package exec:java -Dexec.mainClass="com.unixwinbsd.App.Exec"
root@ns7:/var/maven-exec-args # mvn install exec:java -Dexec.mainClass="com.unixwinbsd.App.Exec"
After the compilation is complete, now, let's try to run the plugin goal. Type.
root@ns7:/var/maven-exec-args # mvn exec:java -Dinput.name=mvnn
Try -Dexec.arguments
root@ns7:/var/maven-exec-args # mvn exec:java -Dexec.mainClass="com.unixwinbsd.App" -Dexec.arguments='O'\''Reilly,The answer is "yes","red, white, and blue"'

5 args: <O'Reilly> <The answer is "yes"> <"red> < white> < and blue">

Try -Dexec.args
root@ns7:/var/maven-exec-args # mvn exec:java -Dexec.mainClass="com.unixwinbsd.App" -Dexec.args='"O'\''Reilly" '\''The answer is "yes"'\'' "red, white, and blue"'

3 args: <O'Reilly> <The answer is "yes"> <red, white, and blue>

Maven exec plugin Very useful and important to execute Java and system code from maven commands. There are two purposes used, namely exec: exec and exec: java. To use it in a project we need to configure it in the pom.xml file.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post