Introduction to Maven – Part 3

Login | Register

Introduction to Maven – Part 3

Maven

In the previous article about Maven, I discussed how to use Maven to create a new project and then how to execute the project via the command line. In this post I’ll show how we can use Maven to execute the application we have build and how to add extra dependencies onto the project.

Running a Maven application

Its always possible to run a Java application using the java command, but when we have many different jar file dependencies on an application it can become difficult and tedious to write out the entire classpath. Fortunately Maven allows us to execute a project compiled with Maven using the mvn command. To enable us to run applications this way, we must make a change to the pom.xml file to define the exec-maven-plugin plugin. This is the plugin that allows Java classes to be executed.

The following code needs to be entered into the pom.xml file. Note that the Java class to be executed is specified within the mainclass element.

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>com.acme.mavenapp.App</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

Now that we have defined the exec plugin, we can run the application from the Maven command line by executing the mvn exec:java command. As previously, if this is the first time we run a maven goal, the relevant plugins are downloaded from the Maven repository to allow the command to complete.

me@opensolaris:~/develop/MavenApp$ mvn exec:java 
[INFO] Scanning for projects... 
[INFO] Searching repository for plugin with prefix: 'exec'. 
[INFO] artifact org.codehaus.mojo:exec-maven-plugin: checking for updates from central 
... Lots of output ... 
[INFO] [exec:java] 
Hello World! 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 7 seconds 
[INFO] Finished at: Wed Dec 10 20:12:09 GMT 2008 
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

Adding an external dependency

Now that we can execute Maven applications from the command line, lets take a look at adding an external jar as a dependency of an application. This is where being able to run the application using Maven becomes useful because we don’t need to worry about the classpath.

For this simple application, we are going to add a dependency on Apache Commons Net. One of the benefits of Maven is that we don’t need to download additional jar dependencies for our applications – Maven does this for us. All we need to do is add a dependency within the pom.xml file specifying the dependency and its version. To add commons net, add the following dependency into the pom.xml file.

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId> 
    <version>1.4.1</version>
    <scope>compile</scope>
</dependency>

Now when we compile the project for the first time, Maven will automatically download the required jars and store them locally so that they can be reused in the future without the need to download them again.

me@opensolaris:~/develop/MavenApp$ mvn compile 
[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building MavenApp 
[INFO]    task-segment: [compile] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [resources:resources] 
[INFO] Using default encoding to copy filtered resources. 
Downloading: http://repo1.maven.org/maven2/commons-net/commons-net/1.4.1/ 
commons-net-1.4.1.pom 
4K downloaded 
Downloading: http://repo1.maven.org/maven2/commons-net/commons-net/1.4.1/ 
commons-net-1.4.1.jar 
176K downloaded 
[INFO] [compiler:compile] 
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 7 seconds 
[INFO] Finished at: Wed Dec 10 20:22:11 GMT 2008 
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------

We can now modify the src/main/java/com/acme/mavenapp/App.java file to make use of the new dependency.

package com.acme.mavenapp; 
   
import org.apache.commons.net.WhoisClient; 
   
public class App 
{ 
    public static void main( String[] args ) throws Exception 
    { 
      WhoisClient whois = new WhoisClient(); 
      whois.connect(WhoisClient.DEFAULT_HOST); 
      System.out.println(whois.query("sun.com")); 
      whois.disconnect(); 
      return; 
    } 
}

Finally, we can compile and execute the app using the mvn compile and mvn exec:java commands.

me@opensolaris:~/develop/MavenApp$ mvn exec:java 
[INFO] Scanning for projects... 
[INFO] Searching repository for plugin with prefix: 'exec'. 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building MavenApp 
[INFO]    task-segment: [exec:java] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Preparing exec:java 
[INFO] No goals needed for project - skipping 
[INFO] [exec:java] 
   
Whois Server Version 2.0 
   
Domain names in the .com and .net domains can now be registered 
with many different competing registrars. Go to http://www.internic.net 
for detailed information. 
   
Domain Name: SUN.COM 
Registrar: NETWORK SOLUTIONS, LLC. 
... Lots of WhoIs output ... 
   
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESSFUL 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1 second 
[INFO] Finished at: Wed Dec 10 20:34:07 GMT 2008 
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------ 
me@opensolaris:~/develop/MavenApp$

About

At Develop In Java, we've been a part of the Java community for over 10 years. If there's anything you'd like us to cover, just ask !

Current versions

  • Java - 1.8
  • Java EE - 7
  • Spring - 4.x