📜 ⬆️ ⬇️

How to build a simple Java program using Maven

The article is written for those who can write the simplest programs in java, but cannot build them. These people already know what classes are, what packages are, and why public static main (String [] argv) is needed, but they did not run the code without the development environment, and they don’t understand who and why they might need it.


I’ll say right away that a Java programmer who cannot build his program from the console will not be hired, and this is generally more than enough reason to learn the art of handling assembly systems. The rest of the details, which is devoted to the article.


I basically will not discuss in the article nothing but the assembly of the minimum HelloWorld. Also, I will try to omit all the technical details that can be omitted and disclose everything in detail, without understanding what is impossible to do.


In order to use the information from the article you need to know what xml is, environment variables, why the PATH environment variable is needed and how to use the console.


Why collect code without an IDE


The first question that arises from a novice developer is how in real life the ability to assemble code without a development environment can be useful. After all, it is installed and configured by everyone - this is the first thing a programmer does when he comes to a project.


The answer is simple - in order to properly organize the workflow - the code should be regularly collected, checked that it compiles at all, and then, in order to make sure that the code works, run the tests.


You can, of course, select a special person who will run the IDE once every 15 minutes and carry out the procedures described above, but this is crazy, such things should be done automatically.


Being able to build code without a development environment is a dire need. So severe that to solve this problem there is a special class of software called build systems.


What is a build system?


An assembly system is a program that assembles other programs. At the input, the build system receives the source code, and the output produces a program that can already be run.


How does it differ from the compiler? In short, the build system calls the compiler in its work, and the compiler does not even suspect the existence of the build system.


If it is longer, then the assembly, in addition to compilation, also includes a whole range of tasks for which the compiler is not suitable for the word at all.


For example, if the program needs some pictures to work, then putting them in the program directory is the task of the build system. If the program needs third-party libraries, then putting them into the program directory is the task of the build system. Well, and so on.


And yes, if the automatic build of the project is configured and working, then there is no need to manually configure the IDE - a modern development environment will generate the project yourself, just show it where the configuration for the build system is located.


Build systems for java by and large 3 - ant, maven and gradle. ant is getting rid of its age, now it is used by either retrogrades, or really cool dudes, like Anton Keks, while the gradle is for hipsters, but maven is the industry standard. To know how to use it is necessary.


How to install maven


Maven is installed simply by copying to the correct directory - there is no installer. As is the case with most console utilities, to use it is enough to add the maven / bin directory to the PATH environment variable.


That is, if maven is in d: / soft / maven, then in PATH you need to add d: / soft / maven / bin


Also, maven will require the JAVA_HOME variable, which points to the JDK. If the JDK is in C: / Program Files / Java / jdk1.8.0_05, then that is the value to be placed in JAVA_HOME. There is no need to add bin to the end.


After that you can try to write in the console


mvn --version 

If it works, then maven is installed.


How to structure a project for maven


In the terminology of maven, a collection of files with the source code of a program, settings files, and all that is called a project. The directory in which these files are located is called the project root directory. In the following, I will denote this directory as <project>


As is known, the java programming language imposes on the programmer a directory structure that dictates the location of files with classes. Napimer class with the full name com.app.HelloWorld should be in the file com / app / HelloWorld.java and nothing else.


Maven adds one more thing to this restriction - the source code must be located in the <project> / src / main / java directory. That is, the class com.app.HelloWorld maven will search in <project> /src/main/java/com/app/HelloWorld.java


This is what this HelloWorld will look like.


 package com.app; public class HelloWorld { public static void main(String[] argv) { System.out.println("Hello world"); } } 

How to make a project description


But simply putting the files into the expected maven directory structure is not enough. To build a project, you need its description as an xml document. Maven will search for a description in the project root directory in the pom.xml file.


The contents of the minimal pom.xml file will be approximately as follows. To go through the tutorial, you can leave it like this and, if nothing is changed, the code will gather normally.


 <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</groupId> <artifactId>hello-world</artifactId> <version>1.0-SNAPSHOT</version> </project> 

What was important and what should I remember? Remember these three lines here.


 <groupId>com</groupId> <artifactId>hello-world</artifactId> <version>1.0-SNAPSHOT</version> 

These three lines are the program identifier for the outside world. The program, which is also the result of the work on assembling a project, is called an artifact in maven terminology.


The incomprehensible word artifact is used here instead of the understandable word program because the result of the work of the build system can be not only the program itself, but also a library or something else. The combination of the groupId, artifactId and version parameters is unique for each artifact. The programmer should take care of the uniqueness of this combination.


Technically, the values ​​of these parameters can be any, but when choosing them it is strongly recommended to follow the rules of good tone.


groupId


The groupId usually contains the name of a package. Let me remind you that the name of the package according to the same rules of good tone should be the name of the web domain, which is owned by the author of the package, which allows the very least to ensure the uniqueness of the name.


artifactId


In artifactId, a string with the name of the artifact that is created by its creator. As a rule, this is some word, sometimes with dashes. For example hibernate-annotation-wat. artifactId must be unique within groupId.


version


Finally, the version is the version of the artifact that needs to be increased with each more or less significant change. The version usually includes numbers and letters. Type 1.0-SNAPSHOT


How to build a project


Now that the structure of the project files is as expected and its description is in the pom.xml file, in order to build the project, all that remains is to open the console, change the current directory to the project directory and write to the console:


 mvn compile 

After maven finishes its work, the target directory will appear in the project directory, which will contain the compiled code.


The compiled java code looks the same as the source code, only instead of files with java extension in directories corresponding to the package names, there are files with the class extension, in which there will be not the source code, but the code intended for interpretation by the java-machine.


That is, now this code can be run.


How to run a project


To run the compiled code, you need to type in the console from the same directory


 mvn exec:java -Dexec.mainClass="com.app.HelloWorld" 

After the maven stops downloading any rubbish from the Internet, somewhere in front of a healthy sign with the inscription BUILD SUCCESS, the line Hello World will appear.


The code worked, everything went well.


This is how java programs are built using the maven build system.


Total


  1. maven is looking for code to build in the <project> src / main / java directory.


  2. Maven will search for instructions in <project> /pom.xml


  3. The result of the assembly system is called an artifact.


  4. The programmer is required to set the groupId , artifactId and version


  5. Building is done with the mvn compile command.


  6. The compiled java code looks the same as the source code, but instead of files with a java extension, there will be files with a class extension.


  7. You can start the compiled program with the command
    mvn exec: java -Dexec.mainClass = "com.app.HelloWorld"

')

Source: https://habr.com/ru/post/309222/


All Articles