Intro To Maven

Intro To Maven

Apache Maven forms the backbone of build tools like Gradle, continuous integration tools like Jenkins and even development tools like Eclipse. But because these aforementioned products often abstract Maven away, many developers have never had any reason to properly learn Maven fundamentals. 

A Maven install is one of the easier software installations you can do. At a high level, the steps to install Maven are:

  1. Download Maven binaries from the Apache website.
  2. Unzip the Apache Maven download, and move the folder to a place on your computer where you keep all of your other DevOps tools.
  3. Add system variables named MAVEN_HOME and M2_HOME, and point both of these variables to the root Maven directory. The root Maven directory will contain a folder named \bin, which contains the pivotally important mvn command-line utility.
  4. The root Maven directory contains a subfolder named \bin. Add this \bin directory to the OS' path. This makes the mvn command-line tool universally available to the OS.

If you want to learn Maven, you need to make sure your Maven install was successful, and you can't be completely sure of that until you have successfully invoked the mvn command-line tool. When you install Maven on Windows, check the mvn by entering the following command on the DOS prompt:

C:\>mvn --version

If you look at the console output generated from the mvn command-line tool, you will notice both the Java version and Java home directories are listed. This is a Maven prerequisite, so before using Maven, first install a Java Development Kit (JDK) and configure the JAVA_HOME system variable. When Maven compiles and runs Java code, it uses the preinstalled JDK tools to do so. Maven doesn't have any facilities to compile Java code itself.

Maven has stringent expectations on how to structure a software project, so it's best to ask the mvn command-line utility to create a Java project, rather than to create one by hand. To do so, just create a folder in which you want your Maven project to reside, and then, issue the following command:

mvn -B archetype:generate
   -DarchetypeGroupId=org.apache.maven.archetypes
     -DgroupId=com.mcnz.maven
       -DartifactId=install-maven-tutorial

Note how the above instruction is broken over four lines due to formatting constraints, but when executed in a scripting environment, it should all be contained on a single line, lest you will receive a Failed to execute goal error.

This command instructs the tool to create a Maven project named install-maven-tutorials (the artifactId). This command also indicates that all Java code will be placed under a set of subfolders that map to the package name com.mcnz.maven (the groupId). The command itself might look a little bit intimidating, but quite frankly, this command is probably the most difficult task for people who want to learn how to install and use Maven. Furthermore, when you look at the structure of the Maven project this command creates, the switches and flags used in the mvncommand will make much more sense.

Maven can create a variety of different projects, from a microservice to a Spring Boot application, depending upon what type of Maven archetype you specify. The command used in this Maven tutorial simply uses a default Maven archetype, which maps to a basic Java project. The command created a new Maven project folder named install-maven-tutorial, based on the -DartifactId switch. Maven also created an impressive branch of folders under the project for separating test code from application source code. The default Maven directory structure looks as follows:

install-maven-tutorial
|--pom.xml
~--src
----main
~------java
~--------com
----------mcnz
~------------mvn
|--------------App.java

~----test
~------java
~--------com
~----------mcnz
~------------mvn
|--------------AppTest.java

Along with the impressive directory structure, the Maven command also created three additional files:

  1. pom.xml
  2. App.java
  3. AppTest.java

A key objective when you learn Maven is to understand the role of the pom.xml file. The POM (Project Object Model) file describes how to build a given project, how the code should be packaged when it is deployed and any dependencies upon which the project relies.

When you look at the pom.xml file that was generated when the Maven project was created, you will notice it contains the artifactId and groupId properties that were specified when you ran the mvn command-line utility. In addition, Maven has indicated through the version property that this is a snapshot version. You can use the packaging property to package and deploy the project as a JAR file. You can also see from the dependency management section that you will use the JUnit during the test lifecycle phase.

Here's the Maven POM example file generated when you execute the task to create a new Maven project.

<project>
  <!--Install and learn Maven tutorial-->
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mcnz.maven</groupId>
  <artifactId>install-maven-tutorial</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>install-maven-tutorial</name>

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

</project>

Along with the pom.xml file, Maven provides two Java classes named App.java and AppTest.javato help get your project started. The App.java file simply contains a basic Hello World application.

package com.mcnz.maven;

/* App.java from the install Maven tutorial.*/

public class App {
    public static void main( String[] args ){
        System.out.println( "Hello World!" );
    }
}

The App.Test.java file simply contains a simple JUnit test that will always pass.

package com.mcnz.maven;
import junit.framework.*;

/* AppTest.java from the install Maven tutorial.*/

public class AppTest extends TestCase{

    public void testApp(){
        assertTrue( true );
    }

}

These two Java files aren't particularly interesting, but they do allow you to test some key Maven build facilities, such as compiling code, running tests and packaging an application. Run the following command from the root folder of the project. It will compile the code in the App.java file, run the JUnit test in the AppTest.java file and zip and package the compiled code in a JAR file.

C:\maven projects\install-maven-tutorial>mvn compile test package

As the mvn command-line utility runs, Maven will display the results of compilation, communicate the results of any failed JUnit test and specify the location of the packaged application. In this instance, the mvn command has created a new folder in the project named target and added a file to that folder with the following name: install-maven-tutorial-1.0-SNAPSHOT.jar.

And that's all there is to it if you need to know how to install Maven and are interested in performing a simple build using the mvn command-line utility. It's really not that hard to learn Maven. Of course, there wasn't a great deal of Java development in this exercise, but with the Maven directory structure created, the pom.xml file added to the root of the project and sample source code provided, that can be addressed when the developer begins to write code. The process will be much easier to do with Maven taking care of the testing, compiling and packaging.


要查看或添加评论,请登录

Serge Burlyga的更多文章

  • Time When Giants Are Failing

    Time When Giants Are Failing

    For Oracle, despite more than $35bn of acquisitions over seven years and shiny new cloud products, its revenues have…

  • 10 Jenkins Interview Questions

    10 Jenkins Interview Questions

    1. What are the software prerequisites that must be met before Jenkins is installed? Since version 2.

  • Apple App Store Review Process

    Apple App Store Review Process

    If you’ve ever submitted an iOS app and been rejected, it’s not as rare as you may think. According to an Apple lawyer,…

  • Who Will Drive Your Car?

    Who Will Drive Your Car?

    Cars have been pretty stupid in the past, even with all of the computerization and automation that has come in recent…

  • Back To School

    Back To School

    Summer vacation is over and it’s time for your kids to go back to school. The technology that awaits them when the bell…

  • Intel's Paranoia

    Intel's Paranoia

    For all its power, success, profits and influence, life at Intel is a permanent struggle. Chide if you will Andy…

  • Carbon Capture By Norway

    Carbon Capture By Norway

    The Norwegian Ministry of Petroleum and Energy has announced that it will be moving forward with an undersea project…

  • I can’t quit you, baby

    I can’t quit you, baby

    If any company had a reason to dump Oracle, it’s Amazon. And yet, 14 years after Amazon lamented its “straining…

  • Sharks

    Sharks

    When scientists want to study birds, they have an enormous crowdsourced data set that they can use. When they want to…

  • IoT Risks

    IoT Risks

    The U.S.

社区洞察

其他会员也浏览了