[For super beginners] Maven super introduction

Maven super introduction

This article also serves as a reminder when building the Maven development environment, and is an article for the following people. ・ I've seen various Web sites, but I can't build a Maven development environment because something is missing. ・ Since the theory and mechanism are good, it is only necessary to build the Maven development environment for the time being. -I can't find the setting to build by myself, just the description of "dependency" in the library I want to use. ・ I understand Maven itself, but I can't link with Eclipse.

What is Maven

"Maven" is basically a tool for building Java programs. For those who have touched C language, I think it's easy to imagine something like make.

Similar tools include Ant and Gradle, but they are essentially the same, only in different ways. It is a tool that can automatically arrange the libraries that depend on before and after compilation, ** just write XML **, and do various things together with the build.

If you search for "What is Maven", you will find a lot of Web sites that have a lot of difficult things written on them, but I don't think you need to read them all at the super-introduction stage. Not limited to Maven, the author is prepared for anything like that (laugh) This article is aimed at people with a "temporary" constitution like the author.

If you say "No, I want to understand from corner to corner!", Please see the reference link at the end.

1. Install Maven

First, install Maven itself. From this point onward, the articles will be written assuming a development environment on Windows. If you are using Mac or Linux, please read it accordingly.

1.1. Download

Download Maven itself from here. Since it is Windows, let's download the one in zip format.

picture01.jpg

1.2. Installation

Unzip the downloaded zip to any folder. In my environment, I chose "C: \ maven". * The folder name has been changed.

After decompressing, the tree will look like the one below.

C:\maven
├─bin
├─boot
├─conf

│ └─ ・ ・ ・ └─lib ├─ ・ ・ ・ └─ ・ ・ ・

1.3. System path settings

Add "C: \ maven \ bin" to the beginning of the environment variable Path.

■ How to add environment variable Path (Windows 7) Right-click on the computer > Properties (R) > Advanced system settings > Environment variables Click in the order of. Add "C: \ maven \ bin" to the lower window, Path in the system environment variable (S).

1.4. Confirmation of installation

Start a command prompt and enter the command "mvn -version". Installation is complete if the following display is displayed.

■ Command display example Apache Maven 3.X.X ~ Omitted ~ Maven home: C:\maven Java version: 1.8.0_xxx, vendor: Oracle Corporation Java home: C:\Program Files\Java\jre1.8.0_xxx" ~ Omitted ~

2. Maven settings

By the way, the installation is completed. From here, configure the settings for the installed Maven. This setting may not be necessary for some people, but if you do not do it, you may be in trouble later.

I won't go into details, but I've skipped the settings here, which made me a lot of trouble later orz.

2.1. Local repository settings

First, create a repository locally to store the downloaded jars, such as the libraries that Maven depends on. If you don't do this, Maven's default repository will be created, but it's a good idea to do so for manageability.

The local repository is set by describing the repository path in "settings.xml" in conf in the installed Maven directory (hereinafter, MAVEN_HOME).

A lot of English is written in settings.xml from the beginning, but most of them are comments. There should be a \ tag in it, so describe the repository settings there.

You can uncomment and revive it, but this time I'll list it just below the comment.

settings.xml(Excerpt)


	<!-- localRepository
	| The path to the local repository maven will use to store artifacts.
	|
	| Default: ${user.home}/.m2/repository
	<localRepository>/path/to/local/repo</localRepository>
	-->
	<!--★ From here ★-->
	<localRepository>C:/maven/repository\</localRepository>
	<!--★ Up to here ★-->

I have set MAVEN_HOME to my local repository.

2.2. Proxy settings

When using Maven in a proxy environment, when downloading a library from a central repository etc., you cannot connect unless a proxy is set. This is also described in settings.xml.

settings.xml(Excerpt)


	<!-- proxies
	| This is a list of proxies which can be used on this machine to connect to the network.
	| Unless otherwise specified (by system property or command-line switch), the first proxy
	| specification in this list marked as active will be used.
	|-->
	<!--★ From here-->
	<proxies>
		<proxy>
			<id>proxy01</id>   ※settings.Anything is fine as long as it is unique in xml
			<active>true</active>
			<protocol>http</protocol>
			<username>proxyuser</username>* If a user is required for proxy connection, describe it.
			<password>proxypass</password>* If a password is required for proxy connection, enter it.
			<host>proxy.host.net</host>* Enter by host name or IP address
			<port>8080</port>
			<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
		</proxy>
	</proxies>
<!--★ Up to here-->

This completes the settings for Maven itself.

3. Eclipse settings

It's almost time to get tired, but it's still a long way before Maven is available. From here, set Eclipse. In my environment, I use Eclipse 4.4 (Luna).

3.1. Installing the Maven plugin

First, install the plugin.

On Eclipse Help > Eclipse Marketplace Click and enter "m2e" in the window that appears to search. From the search results, "Install Maven integration for Eclipse (Luna and newer) 1.5.

picture2.jpg

After installation, restart Eclipse and you're done.

3.2. Maven plugin settings

Set the installed plugin. Some people may be able to get this setting without doing it, but in my environment, if I don't do this, I can't even build because of an error such as "The building plan couldn't be calculated". became.

Window> Settings Click to display the settings window.

Maven > User settings In the "User Settings" text box, enter the settings.xml you set earlier. After inputting, click the OK button to complete the Maven plug-in settings.

picture3.jpg

4. Hello World with Maven!

By the way, it's been a long time underlaying up to this point. At this point, you're all set to use Maven. Of course, it is necessary to adjust the settings according to the project, but this will not be inconvenient for creating a working environment for the time being.

From here, we will actually create a project using Maven and create a program that outputs the familiar Hello World.

4.1. Creating a Maven project

First, create an Eclipse project. There is also a way to create a project with Maven commands, but this time I will create a project from Eclipse.

File > New > Other > Maven Project Select to display the project creation screen.

picture4.jpg

Click Next to proceed to the next screen. Check "Create a simple project" and proceed to the next.

picture5.jpg

Enter any ID for Group ID and Artifact ID and click Finish.

picture6.jpg

After a short wait, a project with an image-like tree will be created. However, for some reason there was a yellow surprise mark from the beginning ... What is this ...? When I look at the problem view, it seems that the build path is saying ... I think that some people may not get it depending on the environment, but I have a strong pull like this, so even if there is an error that does not make people, I will definitely pull it.

picture7.jpg

Although a warning has been issued, the process of creating a project has been completed. If you don't mind the warning, you can do Maven development as it is. If you think "Is it a warning after all?", Please continue to develop it as it is.

However, if you think, "What? What is this warning? It doesn't make sense and I want to erase it?", Don't worry. If you read more, I'll show you how to get rid of this warning.

5. Edit pom.xml

Now that the project is complete, it's time to get into the practical content. Maven is well worth the tedious preparatory work. I'm sure it won't hurt. Because, if you write ** XML, ** Maven will build it according to the rules. If you take it seriously, it will do the deployment without permission after building.

However, as I mentioned at the beginning, it is necessary to write ** XML **. Yes, you can enjoy the taste of ** Maven by writing ** XML.

That XML is the problem. It's annoying. The XML configuration file I've written so far. Nesting incomprehensible tags in layers ... forgetting to close the tags and getting an error ... So much for black history, let's introduce how to write XML.

5.1. What is pom.xml?

I don't know the origin of the name because I haven't investigated it in particular, but it is a Maven configuration file that manages the project settings. Large for components ・ Dependency ・ Plugin It is a configuration file that describes what kind of library is essential for developing a program and what kind of plug-in is used to add useful functions to Maven.

To be honest, it's more of a hassle than going around the world of the web, downloading the latest jars, creating a library management folder, putting it in it, setting the build path, and so on. It's a little better.

In addition, once this pom.xml is created, when developing in large numbers, if you pass only this file, each member will not have to build a development environment with a large number of steps. Let's do our best to edit pom.xml to enjoy the taste of Maven development.

5.2. compiler plugin

Maven also needs a plugin to compile java. Don't think, "What? What? Use it!" You can do pretty much anything as long as you put in a plugin. Among them, the first thing that must be installed is the compiler plugin.

When I created the Eclipse project, a warning should have occurred, but that warning can be resolved by installing the compiler plugin and setting it properly. To install the plugin, write the plugin settings in pom.xml.

I think there is a file called pom.xml at the root of the hello-world project. Edit this as follows.

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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>maven.hello</groupId>
	<artifactId>hello-world</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!--★ From here-->
	<!--Character code and Java version settings-->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!--In my environment I set the version to 8-->
		<java.version>1.8</java.version>
	</properties>

	<!--Plugin settings-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<!--Set Java version here-->
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!--★ Up to here-->
	</project>

Write the above contents to pom.xml and save it. Then, the Eclipse build will start, and the warning that has been issued since the project was created will disappear.

If the warning does not disappear after modifying the contents of pom.xml Right-click on the project > Maven > Update project (U) Click.

picture8.jpg

5.3. Hello World! At this point, all the project settings are complete. The rest is completed by incorporating the process of actually outputting Hello World.

Let's add a class to "src / main / java" displayed in the package explorer like a normal Java project.

HelloWorld.java


	package hello.main;

	/**
	 *Hello World on Maven
	 */
	public class HelloWorld {
		/**
		 *main processing
		 *Display Hello World on standard output.
		 * @param args command arguments
		 */
		public static void main(String[] args) {
			System.out.println("Hello Maven World!");
		}
	}

Add this class and you're done implementing Hello World. If you run it as a Java application as it is, you can usually run Hello World. But this is a Maven project! Use the one built with Maven.

5.4. Build with Maven

Finally, I'll show you how to build a Java project using Maven. This time, we will generate everything from compilation to build artifacts at once, so specify "install" for goal. goal is a command argument specified when Maven is executed. Strictly speaking, it has a decent meaning, but I won't explain it here, so please look at a decent reference book to understand the "phase" and "goal".

To do the act of install in Maven Right click on the project > Run > install Select.

picture9.jpg

Then, the rattling process will proceed on the console. The first build takes quite some time as it downloads a lot of things, such as the Compiler plugin, from a place called the Maven Central Repository.

Finally, [INFO] BUILD SUCCESS Is output, the build is completed successfully.

Then a folder called "target" will appear in the project, and there should be a built jar file there. If it does not appear, refresh the project. If you followed the steps so far, you should have a jar file called "hello-world-0.0.1-SNAPSHOT.jar" built.

You can run Hello World by running this at the command prompt. This time, it is not built as an executable jar file with the entry point specified, so execute it with the following command.

Command: java -classpath hello-world-0.0.1-SNAPSHOT.jar hello.main.HelloWorld

Then, the character string "Hello Maven World!" Is on the command line!

Congratulations! And it was cheers for good work! You have successfully created a Java program using Maven!

6. Finally

It's been a long time, but thank you for staying with us so far. What did you think? Could you have a little experience with Maven? I hope it helps those who have been stumbling on Maven's development environment construction.

Also, since I learned about Maven on this occasion, I wouldn't be so happy if anyone would like to try it.

EX. Reference

・ TECH SCORE http://www.techscore.com/tech/Java/ApacheJakarta/Maven/2/

・ Basic usage of Maven 3 https://www.qoosky.io/techs/07d7bf8708

・ How to use Maven http://qiita.com/soushiy/items/6095401054959ce5d1a5

Recommended Posts

[For super beginners] Maven super introduction
[For super beginners] DBUnit super introduction
[For super beginners] Mirage SQL super introduction
[For super beginners] Struts2 Super Primer --2018 Edition
[For super super beginners] What is object orientation?
Ractor super introduction
Book introduction: Spring Boot Recommended reference book for beginners!
[For super beginners] How to use autofocus: true
[Introduction to Java] Basics of java arithmetic (for beginners)
Groovy super easy introduction
Scraping for beginners (Ruby)
Introduction to Java for beginners Basic knowledge of Java language ①
How to use GitHub for super beginners (team development)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
More usable Enumerable for beginners
Java for beginners, data hiding
Easy library introduction with Maven!
Java application for beginners: stream
Binary Tree Memorandum for Beginners (Part 1)
CI for Maven project on AppVeyor
CI for Maven project at CircleCI
[For beginners] Summary of java constructor
[Rails] Introduction of Rubocop by beginners
Rock-paper-scissors game for beginners in Java
Environment construction with Docker for beginners
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Links for creating Android apps (for beginners)
Java for beginners, expressions and operators 2
[Folio LSP] Roughly Docker (for beginners)
Notes for Android application development beginners
[Super Introduction] About Symbols in Ruby
Object-oriented course for beginners sent by beginners
Starting heroku through heroku CLI (for beginners)
Introduction to Programming for College Students: Introduction
Recommended learning method for programming beginners
[For Java beginners] About exception handling
Classes and instances Java for beginners
(For super beginners) Getter / setter and property to think in D language