BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Apache Maven 3 : Compile, Build, and install a Maven project - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




mvn compile

Continuing from Apache Maven 3 - Setting up and creating a project.


base_directory.png

To compile a Maven project, we need to go to the project base directory:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp$ cd MavenTestApp/
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ ls
pom.xml  src

Let's compile the project:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.3/maven-resources-plugin-2.3.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.3/maven-resources-plugin-2.3.pom (5 KB at 6.7 KB/sec)
...
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ MavenTestApp ---
...
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ MavenTestApp ---[INFO] Compiling 1 source file to /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.414s
[INFO] Finished at: Sun Feb 01 07:37:37 PST 2015
[INFO] Final Memory: 15M/101M
[INFO] ------------------------------------------------------------------------
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ 
...

When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, since we executed the compile phase, the phases that actually get executed are:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile





mvn package - Build the Project

The mvn package will produce jar file and may download some plugins during the process:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn package
...
[INFO] Building jar: /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/target/MavenTestApp-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.990s
[INFO] Finished at: Sun Feb 01 07:57:59 PST 2015
[INFO] Final Memory: 14M/161M
[INFO] ------------------------------------------------------------------------
k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$

As we can see from the output, it generated MavenTestApp-1.0-SNAPSHOT.jar files under the target folder.

Note that the mvn package also ran the test case:
/home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/src/test/java/com/k/bogotobogo/AppTest.java

package com.k.bogotobogo;

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 );
    }
}




running jar file

tree_jar.png

You may test the newly compiled and packaged jar with the following command:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ java -cp target/MavenTestApp-1.0-SNAPSHOT.jar com.k.bogotobogo.App
Hello World!




Maven Lifecycle

Maven defines 3 lifecycles in META-INF/plexus/components.xml:

  1. Default Lifecycle : defined without any associated plugin. Plugin bindings for this lifecycle are defined separately for every packaging:
  2. Clean Lifecycle : lifecycle is defined directly with its plugin bindings.
  3. Site Lifecycle : defined directly with its plugin bindings.

Maven_LifeCycle.png



maven install

When we use mvn install, Maven goes through all the necessary build phases of Maven again.

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn install
...
[INFO] --- maven-install-plugin:2.3:install (default-install) @ MavenTestApp ---
[INFO] Installing /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/target/MavenTestApp-1.0-SNAPSHOT.jar to /home/k/.m2/repository/com/k/bogotobogo/MavenTestApp/1.0-SNAPSHOT/MavenTestApp-1.0-SNAPSHOT.jar
[INFO] Installing /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/pom.xml to /home/k/.m2/repository/com/k/bogotobogo/MavenTestApp/1.0-SNAPSHOT/MavenTestApp-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.892s
[INFO] Finished at: Sun Feb 01 10:01:13 PST 2015
[INFO] Final Memory: 7M/99M
[INFO] ------------------------------------------------------------------------

We see the jar file, MavenTestApp-1.0-SNAPSHOT.jar and pom file, MavenTestApp-1.0-SNAPSHOT.pom have been installed into /home/k/.m2/repository/.

k@laptop:~/.m2/repository$ pwd
/home/k/.m2/repository

k@laptop:~/.m2/repository$ ls
antlr                     commons-codec          dom4j   plexus
aopalliance               commons-collections    javax   slide
asm                       commons-configuration  jaxen   sslext
avalon-framework          commons-digester       jdiff   tomcat
backport-util-concurrent  commons-el             jdom    wsdl4j
biz                       commons-httpclient     jline   xerces
bouncycastle              commons-io             jtidy   xml-apis
bsh                       commons-lang           junit   xmlenc
classworlds               commons-logging        log4j   xml-resolver
com                       commons-net            logkit  xom
commons-beanutils         commons-pool           net     xpp3
commons-chain             commons-validator      org
commons-cli               de                     oro


repository-bogo.png













Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master