APPENDIX B
Apache Maven Tutorial

When you are developing a Java software project, there are many tasks to take care of: downloading dependencies, putting JAR files on a classpath, compiling source code into binary bytecode, running tests, packaging bytecode into deployable JAR files, deploying JAR files to a remote repository server, and so on. Apache Maven, a software project management and comprehension tool that is based on the concept of a project object model (POM), can automate all of these tasks. Maven is intended primarily as a tool for managing Java-based projects, but it can also be used for projects using other programming languages such as C# and Ruby. As alternatives to Maven, Ant and Gradle are also popular software project management tools.

For more information about Ant, see http://ant.apache.org/.

For more information about Gradle, see https://gradle.org/guides/#getting-started.

For a comparison between Ant, Maven, and Gradle, see https://www.baeldung.com/ant-maven-gradle.

Downloading Maven

You can download Apache Maven as a zipped file, apache-maven-3.6.0-bin.zip, from the following address:

https://maven.apache.org/download.cgi

After downloading it, just unzip the file to a directory, for example, c:apache-maven-3.6.0. Once it is unzipped, you will find the Maven program, mvn.jar, in the bin subdirectory. To test your Maven, just run the following command:

mvn --version

But you need to add c:apache-maven-3.6.0in to your system PATH first, or simply run the Maven program with its full path. Either way, the result should show the version of Maven, the version of Java, and the information about your operating system, as shown in Figure B.1.

Screen capture depicting the version of Maven, the version of Java, and the information about your operating system in Command Prompt.

Figure B.1: Displaying the version of Apache Maven

Creating a Maven Project

The following command creates a Maven project, called my-app, with a group ID of com.mycompany.app, as shown in Figure B.2.

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Screen capture depicting the command to create a Maven project, messages to show the project is created successfully Command Prompt (top), and project directory and content in my-app folder (bottom).

Figure B.2: The command to create a Maven project, messages to show the project is created successfully (top), and project directory and content (bottom)

Once the project is successfully created, a project directory called my-app will be created, and Figure B.3 shows the structure of the project directory, which will basically include a pom.xml file, an App.java file, and an AppTest.java file.

Screen capture depicting the code of structure of a Maven project directory.

Figure B.3: The structure of a Maven project directory

The pom.xml file, as shown next, is the core of the Maven project, which describes the project details, manages dependencies, and configures plugins for building the software. Please make sure you insert the <Properties> section as shown in the listing.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
 <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

The App.java file, shown next, is the main Java program of the project, which specifies what your project will do. In this example, it just displays “Hello world!” on the screen. The AppTest.java file, also shown, is the app program that calls the App.java program to run.

This is the code for the App.java file:

package com.mycompany.app;
 
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

This is the code for the AppTest.java file:

package com.mycompany.app;
 
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
 
/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }
 
    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }
 
    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Compiling and Building the Maven Project

Type the following command to compile and build the project, as shown in Figure B.4. This will compile the project and package the binary bytecode into a deployable JAR file called my-app-1.0-SNAPSHOT.jar in the /my-app/target/ subdirectory.

mvn package
Screen capture depicting the Command Prompt window with command to compile and build the Maven project (top) and the messages to show the project has been successfully built (bottom).

Figure B.4: The command to compile and build the Maven project (top) and the messages to show the project has been successfully built (bottom)

Running the Maven Project

Type the following command to run the project, which should display “Hello world!” as shown in Figure B.5.

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Screen capture depicting the Command Prompt window with result of the command to run the Maven project.

Figure B.5: The result of the command to run the Maven project

For more information about Maven, see the following resources:

https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

https://www.javatpoint.com/maven-tutorial

http://tutorials.jenkov.com/maven/maven-tutorial.html

https://www.guru99.com/maven-tutorial.html

https://examples.javacodegeeks.com/enterprise-java/maven/create-java-project-with-maven-example/

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

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