Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting, and documentation from a central piece of information.
Primarily used for Java-based projects but that can also be used to merge projects in other programming languages like C# and Ruby. Many integrated development environments provide plugins for Maven. Typical tasks if a build tool is a compilation of source code, running test and packaging results into jar files. Maven can also perform related activities, for example, creating websites, uploads build results or generate reports.
Automating process of creating the initial project structure for Java application is very important, especially when doing it manually may take much more time and also there is a possibility for making typos during using some commands in the console, the last one is very frequent.
A little bit of history:
Originally Maven was designed to simplify building process in Jakarta Turbine project.
Maven can be used for:
handle dependencies and intermediate dependencies
easy selection of tasks from the command line
versioning and tagging code
a large number of plugins to simple and complex tasks
Integration with all IDE such as Eclipse, NetBeans or IntelliJ
ability to manage a number of the modules of the project at a time
mailing list management
Maven is working according to the Convention Over Configuration pattern, which means that the configuration requires only those components that are nonstandard or user will want to adopt them for their needs, and the same configuration change can also be found under only one file – pom.xml.
Developers can build Maven project without the need to understand how the individual plugin works.
I use several systems start from Linux, Windows and Mac OS X machines. For this post, I will use Mac OSX.
Open terminal and type: brew install maven
If we have already maven installed, in a terminal should appear:
After installing Maven should check if everything is working properly, this purpose should be in the command line to run Maven and check the versions of what was installed:
As you can see installation is not difficult in inches. Some different looks to install Maven on Linux and Windows.
Before we turn to the creation of the first project we need to discuss a matter of a few, namely what they are:
The artifact is a name that identifies the project in the group. The group allows you to organize the namespace in which it is the artifact.
Both the group and artifact and version uniquely identifies the library of which we want to use. There is also something like what is called a plugin and archetype. The plugin is a special Java language, class in which performs the appropriate actions on the basis of asking configuration, for example, to pack compiled class to a jar file, generate documentation. An artifact is a specific plugin using which created them st project. It contains in its structure mapping of tree directories a new project, or file pom.xml contains basic project data.
To generate maven project we have two options. First from a console and second from IDE.
Creating maven project looks as follows:
With this command Maven generates a Java project:
This last step can take some time depends on how much RAM memory do you have (These days it’s 16 GB).
Structure of the generated project looks like follows:
We have generated whole Maven project structure Java source code. Maven created App.java class which is just simple “Hello World” program. We have also can see AppTest.java that is the simple test class. In root catalog, we can find our pom.xml file:
To compile Java source code we need to trigger: mvn compile in root project directory. This command runs through all life cycle phase, which is needed to compile a project.
To run test phases instead to run a full build we need just to trigger: mvn test
To clean project and remove all generated files from ./target directory we can use: mvn clean