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 : Dependencies - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Adding dependencies

Continuing from Apache Maven 3 - Compile, build, and install a Maven project.

In this chapter, we'll add dependencies and let Maven know we have changed dependencies via pom.xml.

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

Since we've installed all the necessary components include jar files into local repository, we do not need target folder. So, we may want to delete it:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn clean
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTestApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenTestApp ---
[INFO] Deleting /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.739s
[INFO] Finished at: Sun Feb 01 10:34:10 PST 2015
[INFO] Final Memory: 6M/97M
[INFO] ------------------------------------------------------------------------

We can see the target folder has been deleted:

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

No_Target_folder.png




Adding dependency - source code change

We have our source code,
/home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp/src/main/java/com/k/bogotobogo/App.java
which looks like this:

package com.k.bogotobogo;

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

Now we want to print out the message to log instead of stdout, and the code modified like this:

package com.k.bogotobogo;

import org.slf4j.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        // System.out.println( "Hello World!" );
	Logger logger = LoggerFactory.getLogger(App.class);
	logger.info("Hello World!");
    }
}

Note that we commented out the 'System.out' and using Logger. Also, we're importing a new package called org.slf4j. Here slf4j stands for Simple Logging Facade for Java.





mvn compile - fail

Let's compile it:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn compile
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...
error: package org.slf4j does not exist
...

What's missing?
We need to tell Maven to pull up the slf4j from the central repository. To do that we need to modify our pom.xml file which is in base directory of our project.





pom.xml - dependencies

Here is our current pom.xml file:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.k.bogotobogo</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTestApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

We need to add the new dependency to the file.

Before we do that, we need information about the groupId, artifactId, and version for slf4j. We can find the info using a keyword like 'maven repository search', and search for 'slf4j':



slf4j_search.png

slfj4_versions_etc.png

SLF4J_PI_Module.png

We can see it even provide what we should put into our pom.xml to tell Maven the dependency.


Here is our new version of pom.xml after adding the new dependency:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.k.bogotobogo</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTestApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>




mvn compile - success

What Maven will do is to get download the slf4j jar before the compilation.

k@laptop:~/java/apache-maven-3.2.5/bin/myapp/MavenTestApp$ mvn compile
...
Downloading: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar
Downloaded: http://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar (29 KB at 44.3 KB/sec)
...
[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: 4.870s
[INFO] Finished at: Sun Feb 01 11:48:22 PST 2015
[INFO] Final Memory: 13M/97M
[INFO] ------------------------------------------------------------------------

This time the compilation is successful.

Note that we did not put scope in our new dependency. In fact, we're using compile scope which is the default because we haven't specified any scope:

<scope>compile</scope>







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