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.length);
        System.out.print(" args:");
        for (String s: args) {
            System.out.printf(" <%s>", s);
        }
        System.out.println();
    }
}

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.

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
      </plugin>

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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.unixwinbsd</groupId>
<artifactId>maven-exec-args</artifactId>
<version>1.0-SNAPSHOT</version>

<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>

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

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    <plugins>
      <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
      <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.7.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
      </plugin>
    </plugins>
  </pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>e1</id>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>com.unixwinbsd.App</mainClass>
        <!--
        <commandlineArgs>"arg 1" 'arg 2' "O'Reilly"</commandlineArgs>
        <arguments>
          <argument>argument 1</argument>
          <argument>argument 2</argument>
        </arguments>
        -->
      </configuration>
    </plugin>
  </plugins>
</build>
</project>


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"'
Result

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"'
Result.

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