[JAVA] Try using Maven

Overview

What is Maven? So I decided to investigate and use it. Personal notes.

What is Maven?

I read the following. http://d.hatena.ne.jp/keyword/Maven https://kengotoda.gitbooks.io/what-is-maven/content/preface.html

Build tool You can easily build and deploy projects, generate test reports, etc. without having to write build.xml by yourself like Apache Ant.

I've never used Java, so I decided to use Maven to run it as a study of Java.

Prerequisites

Installation procedure

$ wget http://ftp.jaist.ac.jp/pub/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

$ tar xvzf apache-maven-3.3.9-bin.tar.gz

$ mv apache-maven-3.3.9 /usr/local/

$ ln -s /usr/local/apache-maven-3.3.9/bin/mvn /usr/local/bin/mvn

$ mvn --version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/local/apache-maven-3.3.9
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.4.27-boot2docker", arch: "amd64", family: "unix"

Project creation

mvn archetype:generate -DinteractiveMode=false \
                         -DarchetypeGroupId=pl.org.miki \
                         -DarchetypeArtifactId=java8-quickstart-archetype \
                         -DgroupId=hello.world \
                         -DartifactId=helloworld \
                         -Dversion=1.0-SNAPSHOT \
                         -Dpackage=hello.world \
                         -Dpackaging=jar
$ tree helloworld/
helloworld/
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- hello
    |           `-- world
    `-- test
        `-- java
            `-- hello
                `-- world

9 directories, 1 file

Generated pom.xml

pom.xml is an important file with information about the project. For example, it contains information about building a project, information about dependent libraries, plugin settings, and so on.

reference http://www.techscore.com/tech/Java/ApacheJakarta/Maven/2-2/#maven-2-4

<?xml version="1.0" encoding="UTF-8"?>
<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>hello.world</groupId>
	<artifactId>helloworld</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

Place the source

$ pwd
/root/helloworld/src/main/java/hello/world

$ cat HelloWorld.java
package hello.world;

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello, world.");
    }
}

Source code build

Build creates target directory

$ cd helloworld
$ mvn compile
$ tree helloworld/
helloworld/
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |       `-- hello
|   |           `-- world
|   |               `-- HelloWorld.java
|   `-- test
|       `-- java
|           `-- hello
|               `-- world
`-- target
    |-- classes
    |   `-- hello
    |       `-- world
    |           `-- HelloWorld.class
    |-- generated-sources
    |   `-- annotations
    `-- maven-status
        `-- maven-compiler-plugin
            `-- compile
                `-- default-compile
                    |-- createdFiles.lst
                    `-- inputFiles.lst

19 directories, 5 files

Source execution

$ java hello.world.HelloWorld

Fitted 1

When writing package ~~~ in Java source, It had to be matched with the package specified at the time of execution when Maven project was generated.

Fitted 2

It didn't work when I ran the generated source. As a result, the java execution location was not good.

In this case, the package was hello.world. The place to execute was as follows.

$ pwd
/root/helloworld/target/classes

$ java hello.world.HelloWorld
Hello, world.

Fitted 3

Although it is not the source given in this example By using try-with-resources in the source to build, There was a problem that I couldn't build it well. try-with-resources was implemented from Java7, but now it's Java8 I was wondering why it didn't work. .. ..

So, as a solution, I had to explicitly tell you to build with Java 8 when creating the project.

I referred to the following. http://qiita.com/mkamotsu/items/98c6d721a87a74f2b36f http://d.hatena.ne.jp/Kazuhira/20151010/1444479555

Recommended Posts

Try using Maven
Try using libGDX
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
Try using Talend Part 2
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try using Spring JDBC
maven
Try using RocksDB in Java
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try scraping using java [Notes]
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
Try gRPC with Java, Maven
[Swift] Try using Collection View
Try using IntelliJ IDEA once
Try using Spring Boot Security
Try using gRPC in Ruby
[Rails] Try using Faraday middleware
[Processing] Try using GT Force.
[Programming Encyclopedia] ยง2 Try using Ruby
People using docker Try using docker-compose
Try using Redmine on Mac docker
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Check the operation using jetty with Maven.
Try HiveRunner
Try using the service on Android Oreo
Try using the Stream API in Java
Try using the Rails API (zip code)
Try Mockito
Study Java Try using Scanner or Map
Try Selenium
Try using JSON format API in Java
Try DbUnit
Try using Spring Boot with VS Code
Quick build maven project using maven docker container
Maven learning
Try using Reladomo's MT Loader (Multi-Threaded Matcher Loader)
try docker-compose
Try using JobScheduler's REST-API --Java RestClient implementation--
Try using Kong + Konga with Docker Compose.
Try using the Emotion API from Android
Try using the Wii remote with Java
Try Lombok
[Maven] About Maven