Running a custom executable

There are many situations when you want Maven to run a specific executable on your computer. A simple use case would be to run the JAR that you created. Another case would be to have Maven run commands that are not provided as plugins (for instance, create a native Windows installer).

Maven provides support to run any executable system in a separate process along with Java programs in the same virtual machine on which Maven runs. The Maven Exec plugin provides this support using the exec goal (to run in a separate process) and the java goal (to run Java programs in the same process).

How to do it...

  1. Open a simple Maven project (simple-project).
  2. Run the command:
    mvn clean package exec:java –Dexec.mainClass="com.packt.cookbook.App"
    
  3. Observe the results:
    How to do it...

How it works...

We wanted to run the JAR file that we had created in the project. To do this, we called the java goal of the Maven Exec plugin. We provided the plugin with the required parameter (mainClass) so that it knew which main class needed to be run.

There's more...

You could integrate the running of the executable as part of the project lifecycle. Let us do this for our example:

  1. Open the project (let's call it project-with-exec).
  2. Add the following code to the pom file:
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>1.3.2</version>
              <executions>
                <execution>
                  <id>hello-world</id>
                  <phase>package</phase>
                  <goals>
                    <goal>java</goal>
                  </goals>
                </execution>
              </executions>
              <configuration>
                <mainClass>com.packt.cookbook.App</mainClass>
              </configuration>
            </plugin>
  3. Run the following command:
    mvn clean package
    
  4. Observe the result:
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project-with-exec ---
    [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-exec	arget
    project-with-exec-1.0-SNAPSHOT.jar
    [INFO]
    [INFO] --- exec-maven-plugin:1.3.2:java (hello-world) @ project-with-exec ---
    [WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment
    on MEXEC-6.
    06:25:26.005 [com.packt.cookbook.App.main()] INFO  com.packt.cookbook.App - Hell
    o World
    [INFO] ------------------------------------------------------------------------
    

The project is run during the package phase based on the configuration that we specified in the plugin.

The same can be done for non-Java executables; we need to invoke the exec goal instead of the java goal.

Tip

Running system executables makes the build nonportable, so use it with care.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.189.67