Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Quick Intro to Maven

Quick Intro to Maven

Jussi Pohjolainen

April 14, 2020
Tweet

More Decks by Jussi Pohjolainen

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. pom.xml <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.company</groupId> <artifactId>myapp</artifactId> <packaging>jar</packaging>

    <version>1.0-BETA</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project> 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
  5. Project Structure . ├── pom.xml └── src └── main └──

    java └── com └── company └── Main.java
  6. Main.java package com.company; public class Main { public static void

    main(String [] args) { System.out.println("Hello World"); } }
  7. 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
  8. Easier Running with plug-in: mvn exec:java <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId>

    <artifactId>exec-maven-plugin</artifactId> <version>1.5.0</version> <configuration> <mainClass>com.company.Main</mainClass> <cleanupDaemonThreads>false</cleanupDaemonThreads> </configuration> </plugin> </plugins> </build>
  9. Main – class for jar <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration>

    <archive> <manifest> <mainClass>com.company.Main</mainClass> </manifest> </archive> </configuration> </plugin>
  10. 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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); return response.body(); } } Uses HttpClient to make http request
  11. 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!
  12. Location of downloaded jars • In Unix/Mac • ~/.m2 •

    In Windows • C:\Documents and Settings\{user-name}\.m2