Slide 1

Slide 1 text

Quick Start to Maven

Slide 2

Slide 2 text

Build Tools • Ant (2000) • One of the first "modern" build tools • Very popular among Java • Easy, uses procedural programming idea • Uses XML which is not very good on the concept of procedural programming • XML can began to be very big • Maven (2004) • Uses also XML, but structure is different • Relies conventions and provides available targets (goals) • Has repository support (dependencies) • Gradle (2007) • Uses DSL instead of XML

Slide 3

Slide 3 text

Convention over Configuration • Developers are not required to create build process • Maven offers sensible default behavior for projects • Maven creates a default project structure • Developer adds files to directories and writes configuration to pom.xml

Slide 4

Slide 4 text

Default Project Structure Item Default source code src/main/java resources src/main/resources tests src/test jar target/ byte code target/classes

Slide 5

Slide 5 text

pom.xml • POM, Project Object Model, is an xml file • Base directory of your project • Information about the project and compiling process • Defines dependencies • The pom.xml can inherit a super pom, which provides basic configuration

Slide 6

Slide 6 text

pom.xml 4.0.0 com.company myapp jar 1.0-BETA 11 11 UTF-8 artifact is a file and each artifact has a group id, usually like package in Java. Also version is defined. GroupId + artifactId + version creates uniquely identified artifact. Project's dependencies are specified as artifacts

Slide 7

Slide 7 text

Project Structure . ├── pom.xml └── src └── main └── java └── com └── company └── Main.java

Slide 8

Slide 8 text

Main.java package com.company; public class Main { public static void main(String [] args) { System.out.println("Hello World"); } }

Slide 9

Slide 9 text

Commands • To compile • mvn compile • To test (using JUnit) • mvn test • Create jar file • mvn package • To remove target • mvn clean • To run • mvn exec:java -Dexec.mainClass="com.company.Main" • To create Eclipse project • mvn eclipse:eclipse

Slide 10

Slide 10 text

Easier Running with plug-in: mvn exec:java org.codehaus.mojo exec-maven-plugin 1.5.0 com.company.Main false

Slide 11

Slide 11 text

Main – class for jar org.apache.maven.plugins maven-jar-plugin 3.0.2 com.company.Main

Slide 12

Slide 12 text

public class Main { public static void main(String [] args) { try { String data = makeHttpRequest("https://jsonplaceholder.typicode.com/todos/"); System.out.println(data); } catch(URISyntaxException | IOException | InterruptedException e) { e.printStackTrace(); } } public static String makeHttpRequest(String url) throws URISyntaxException, IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(new URI(url)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); return response.body(); } } Uses HttpClient to make http request

Slide 13

Slide 13 text

JSON Parsing ... import org.json.*; public class Main { public static void main(String [] args) { try { String data = makeHttpRequest("https://jsonplaceholder.typicode.com/todos/"); // Output the first title JSONArray allTodos = new JSONArray(data); JSONObject todo = allTodos.getJSONObject(0); System.out.println(todo.getString("title")); } catch(URISyntaxException | IOException | InterruptedException e) { e.printStackTrace(); } } Will Fail! This is not part of java se!

Slide 14

Slide 14 text

Dependency to pom.xml org.json json 20190722

Slide 15

Slide 15 text

Location of downloaded jars • In Unix/Mac • ~/.m2 • In Windows • C:\Documents and Settings\{user-name}\.m2