After posting a
topic about Maven in the comments, questions arose about how to start working with it, where to start, how to compile pom.xml files, where to get plug-ins, etc. This topic will be some kind of getting started or faq
Terminology
As in any system, in Maven, there is a set of terms and concepts.
The entire project structure is described in the pom.xml file (POM - Project Object Model), which should be located in the root folder of the project. The key concept of Maven is an
artifact - in fact, any library stored in a repository. This may be some kind of dependency or plugin.
')
Dependencies are those libraries that are directly used in your project to compile code or test it.
Plug-ins are used by Maven himself when building a project or for some other purposes (deployment, creating project files for Eclipse, etc.).
At the very beginning of work with Maven, the user will certainly encounter such a concept as an archetype.
An archetype is a kind of standard layout of files and directories in projects of various kinds (web, swing-projects and others). In other words, Maven knows how projects are usually built and, in accordance with the archetype, creates a directory structure.
As a rule, the name of the artifact consists of the name of the group, its own name and version. For example, Spring will have this name in the Maven environment: org.springframework.spring: 2.5.5. The last domain always means artifactId, all that is in front of it - groupId - remember this well!
I will not dwell on the life cycle, as it is well described in the above-mentioned article. And now for the practice.
Install Maven
The latest version can always be downloaded from
the download page on the official website. Just unpack the archive in any directory. Next, you need to create a variable in the Path, in which you must specify the path to Maven. Go to Win + Pause - Advanced - Environment Variables - in the top window, click Create, enter the name M2_HOME and the value is “C: \ apache-maven-2.2.1”. Next, in the same place, we create another M2 variable with the value% M2_HOME% \ bin. Also make sure that there is a JAVA_HOME variable with the path to the JDK. Its value should be something like “c: \ Program Files \ Java \ jdk1.6.0_10 \”. Finally, in the same window, create / modify the Path variable; you just need to write% M2% to it, so that our daddy with the Maven executable can be seen from the command line. Now you need to check the performance of our installation. To do this, go to the command line and enter the command
mvn –version
Information about versions of Maven, jre and the operating system should appear, something like:
Maven version: 2.2.1
Java version: 1.6.0_10
OS name: "windows 2003" version: "5.2" arch: "x86" Family: "windows"
Maven will create you a local repository in your personal folder, for example in the directory C: \ Documents and Settings \ username \ .m2 \ repository
Everything, Maven is ready to work, you can start creating an application.
Creating an application from the archetype
The Maven website
lists the most popular archetypes for applications, but you can easily create your own or find more specific ones, for example
here .
So, let's say we are interested in a web application - we find a suitable archetype, it is called maven-archetype-webapp. In the command line, in the required directory, execute the Maven command:
mvn archetype: create -DgroupId = com.mycompany.app -DartifactId = my-webapp
-DarchetypeArtifactId = maven-archetype-webapp
Now we can see a rather visual directory structure with java speaking names - here will be your classes, webapp - here are the web application pages, resources - various kinds of resources in the classpath (configuration files, for example), test - unit tests, respectively, and t .P.
Build project
Everything is simple here - we execute the command
mvn package
or
mvn install
in the root directory of the application, where the pom.xml file is located. The first command will compile your project and put it in the target folder, and the second will also put it in your local repository.
There is a useful function like a conveyor, so you can write
mvn clean install
and Maven will first clear the project's target folder, then collect it and put it into the repository.
We have studied the minimum set of actions for Maven, now we are going to customize and add project dependencies.
Dependencies and Repositories
As a rule, most of the popular libraries are located in the
central repository , so you can write them directly to the dependencies section of your pom-file. For example, to connect the Spring Framework, you need to define the following dependency:
<dependencies>
...
<dependency>
<groupId> org.springframework </ groupId>
<artifactId> spring </ artifactId>
<version> 2.5.5 </ version>
</ dependency>
</ dependencies>
Although it is possible not to specify the version and then Maven will take the last option, but I personally advise you to do it, because we have repeatedly had cases that the project just stopped gathering at one moment or began to fall in completely unexpected and well-tested places, although no one would change anything.
Specific items are usually not in the central repository and then you will have to specify the manufacturer’s repository manually. For example, add the JSF framework dependency of JBoss RichFaces ajax components.
Dependencies are simple:
<dependencies>
<dependency>
<groupId> org.richfaces.ui </ groupId>
<artifactId> richfaces-ui </ artifactId>
<version> 3.3.1.GA </ version>
</ dependency>
</ dependencies>
But the JBoss repository now needs to be registered with pens either in the settings.xml file, which lies in the root of your local repository, or in the pom.xml file itself, depending on whether this repository is needed in all projects, or in any one specific, respectively:
<! - JBoss RichFaces Repository ->
<repositories>
<repository>
<releases>
<enabled> true </ enabled>
</ releases>
<snapshots>
<enabled> false </ enabled>
<updatePolicy> never </ updatePolicy>
</ snapshots>
<id> repository.jboss.com </ id>
<name> Jboss Repository for Maven </ name>
<url>
http://repository.jboss.com/maven2/
</ url>
<layout> default </ layout>
</ repository>
</ repositories>
As a rule, on sites of large projects they write all the information necessary for embedding their library in a project based on Maven, but there are cases when an artifact has to be searched for for a very, very long time. Here,
MVNrepository.com can help
us a lot - it will always tell you where the required library can be located. But if even there was not found, then from my own experience I can advise google "
<library name> pom.xml". It happens that the projects have long been closed and are not supposed to be in the repository because the developers no longer care about their distribution. Then there is only one way - to add the file to the repository manually with the command:
mvn install: install-file
-Dfile = <path-to-file>
-DgroupId = <group-id>
-DartifactId = <artifact-id>
-Dversion = <version>
-Dpackaging = <packaging>
The last parameter most often matters jar.
Plugins
Since plugins are the same artifacts as dependencies, they are described almost the same. Instead of the dependencies section - plugins, dependency - plugin, repositories - pluginRepositories, repository - pluginRepository.
With Maven plug-ins, everything does, even directly, what it was for - the project build, only this plug-in is not necessary to be specified in the project properties, unless you want to add some features.
Let's see how to set up a plugin to create a project for Eclipse using WTP ver. 2.0. In the plugins section of our pom.xml, we write the following plugin:
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-eclipse-plugin </ artifactId>
<configuration>
<wtpversion> 2.0 </ wtpversion>
</ configuration>
</ plugin>
Now we go again to the command line and execute the command
mvn eclipse: eclipse
We are waiting for Maven to find all the libraries in the repository or to download them and voila - now our Maven project can be opened as an eclipse project. At the same time, libraries are not copied anywhere as with the classical approach, but remain in the repository and Eclipse makes reference to them through its variables.
Naturally, there is no single list of all plugins, the
official site only has plugins supported directly by Maven developers. However, I would like to note that the names of the plug-ins are quite straightforward, and by doing a search for the keywords “maven tomcat plugin” you will most likely find the plugin for the deployment of the project in Tomcat as the first link.
Own repository
Unfortunately, I myself do not have much experience configuring the repository, but I can advise as the most simple and common Nexus. For more information please refer to the
site of this project .
However, it is impossible to ignore the worthy competitors in the face of
Artifactory and
Archiva .
Conclusion
I really hope that the goal of this article has been achieved - the minimum knowledge of Maven should be enough to work with not very large projects. For more serious purposes, I strongly advise you to examine in detail the maven-compiler-plugin and maven-resource-plugin - they are directly responsible for the final layout of the project. I believe that the most important point in learning Maven is an understanding of ideology - Maven describes the final structure of the project, and not the way to achieve it, unlike Ant.
useful links
Official Maven Page
Documentation
Central repository
IBiblio repository
Search artifacts by name
Nice Maven Forum
Maven: The Definitive Guide - A Good Book by Maven