What is Maven? So I decided to investigate and use it. Personal notes.
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.
$ 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"
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
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>
$ 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.");
}
}
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
$ java hello.world.HelloWorld
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.
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.
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