Maven - LearningRbcRegistry/Wiki GitHub Wiki
#Maven Antoine Bour / Oct 16*
#####a. Maven Lifecycle #####b. What is an Plugin #####b. What is an Artifact #####c. What is a Repository, How it works: #####e. Exemple: #####f. Write my own plugin:
- Standardisation tool
- Build tool
- Project management tool
- Reporting tool
- https://maven.apache.org/download.cgi
- Maven 3.3.* = JDK1.7 (set you JAVA_HOME, after maven install, set you path to mvn bin folder)
- How to setup? http://maven.apache.org/xsd/settings-1.0.0.xsd
### 3. First Project
As a java developper that do no knows maven, my first interest is to make a simple « hello world » application. I'm a junior, so I'm going to ask help to maven. Maven is going to generate this project for me thanks to the wished project template.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falseResult :
- a pom file has been created
- A project structure respecting the common standards is now available
- The developer can start to work.. The Project Object Model files contains all informations about my project:
- Now, I want more: What's the lifecycle?
- My root need is to compile and launch my project:
-
By the way I have to test it: Same command from “test” folder with Classpath update in order to include the missing library.
-
After that I need to make it independent.
-
At the end, I have to publish it.
Maven can help to do that very easily: The lifeCycle:
My first need is only to compile the code:
- What you used before: javac …... with a lot of setup to do it for respecting standards
- What you have to use now: . The result is the following:
Thas was my first step. In one cmd, maven facilitates the build. But this is only a small part of my need. In fact, In this example, we have called a plugin goal.
##### a. Maven Lifecycle
In Maven, there are 3 lifecycles:
The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation. We are going to focus on the default lifecycle. Once done, the other lifecycles are easy to understand:
The Default Lifecycle:
A lifecycle is made of Phases. During the compile phases I use the compiler Plugin to run the compile goal: mvn compiler:compile
| Phase | Description | More |
| validate | validate the project is correct and all necessary information is available | If a important tag is missing in the POM (like artifactId), this will fail :
|
| compile | compile the source code of the project |
What are the detailled lifecycle ? |
| test | test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed | |
| package | take the compiled code and package it in its distributable format, such as a JAR. | |
| install | install the package (jar, war...) artifact into the local repository, for use as a dependency in other projects locally | |
| deploy | done in the build environment, copies the final package to the remote repository for sharing with other developers and projects. |
- Executing a mvn phase (mvn install) will run all precedent phases:
```bash
mvn package = mvn validate + mvn compile + mvn test + mvn package
```
- Execution of a mvn goal will not execute the precend phases:
```bash
mvn compiler:testCompile
```
Will only execute it's own task: compile only the java ressources defined in test folder.
All goals are executed with the corresponding phase.
#####b. What is a Plugin:
- Plugins are artifacts that provide goals to Maven
- Each maven action is based on a plugin
- Here over, each phase is part of a plugin.
#####c. What is an Artifact It's a project (jar, binary..) uniquely identified by a group Id and an artifact ID unique within a group. As a developper, using maven to build your project, you can consider your project as an artifact.
#####d. What is a Repository, How it works
Your local repository is defined in settings.xml (config file from maven) (often C:\Users\userName.m2\repository) The remote enterprise Repository is https://rbcis-sf.fg.rbc.com/artifactory/ Central repositories are https://binary-repositories-comparison.github.io/
After mvn install on your project, you can see that, for the first time, maven is going to download from central repo to your local repo a lot of packages. In fact these are plugins needed to use maven and plugin defined in your dependencies. Once downloaded it will not download it again until a new revision of the concerned project. Once you want to share you progress in the project to other colleagues, then you should deploy it in the repository. In fact, in the Labs, developers are publishing in git. The purpose and advantages from a repository are explained here: https://maven.apache.org/repository-management.html
In the labs, the developers takes their owned code in git, dependancies are in the maven nexus repository. Jenkins is updating maven with the newer version.
When publishing the code (here, it's done by Jenkins), you have to update the version:
| Version | Description |
|---|---|
| [,3.8.1] | <3.8.1 |
| [3.8,4.0] | from 3.8 to 4.0 (excluded) |
| If the project is still in a development phasis, please add SNAPSHOT | A snapshot will contains the date and maven will automatically update the associated dependencies |
Ok, Now I want my little application to be portable. My application has to start in dev/test/prod environments with the right profile and without reinstalling maven.
You can use properties in you maven POM. You have to define them in the settings.xml, in the pom itself or in external files. The properties used can be taken from you JAVA environment...
Your properties have to be defined by profile and you have to appli the wished profile to the environment where your application is deployed.
Exemple: Setup the database connectivity, the jvm restriction, the log4j outputs...
Finally, you can ask maven to generate a jar which contains all needed dependencies.
| Kew word | My Definition | Official definition |
|---|---|---|
| Archetyp | Allow the creation of the wished project structure | Definition |
| POM Project Object Model | Declare and define my project structure | Definition |
| Dependency (tag in the pom) | Define and manage the list of tools/libraries needed in the project | Definition |
| Lifecycle | Process for building a project | Definition |
| Plugin | ||
| Repository |
So, in this first project we know a little bit more about the installation, POM,LifeCycle,plugin,artifact,archetyp,repository and maven properties maven properties.
- Configurate maven's settings:
The settings.xml file contains all informations that are required for the execution of maven:
- User informations
- proxies
- Repositories
- profiles
- ….
##### e. Exemple:
The whole project is available here: push it on git. </style>
| Projet name | Description | ||
| simple-podcast | Simple java app that retrieve the last podcast based on a RSS file (xml) | This project is independant, works by launching Main.java. We have defined an easy testing case and in order to display the results we have used Velocity. By the way I have used log4j to print the logs of the application. |
|
|
- Dependencies:
- log4j.jar for logs, - dom4j + jaxen,commons-io for manipulating the XML RSS - -velocity for the shell output template -junit in order to test (they can be skipped or configured as non-blocking tests) - To see the dependencies of a project : mvn dependency:tree |
|
||
Just for fun I want to compile my code in Java1.5:
|
|||
Start the program is possible independently:
|
|||
| Now we are going to create a web app in order to show the result with a browser : | |||
| SimpleWeb | Simple web app that use the rss retriever (to adapt with a Service Layer) | This project requires simple-podcast to run | |
| Finally we create a parent project names simple-parent that contains the 2 projects as « module » | |||
| Now, to create a web app showing me hello World. Same kind of project with a jetty server inside (SimpleWeb). For that, we define a server plugin in our pom : jetty. By the way ,as we user servlet, we have to include the associated plugin in our dependencies (javax.servlet.jsp) After mvn install, do a mvn jetty:run and open your browser http://localhost:8080/simpleWeb/ | |||
|
|||
It works, but how:
So, all my project definitions contains the test plugin as dependencies. Let's include it in the simple-parent pom.xml. The will all herit from the parent pom.xml Dependences from SimplePodcast that are not directly used in SimpleWeb are names Transitive dependencies. This jUnit dependence can be present in a tag. This tag manages the dependencies for all the project. In other term, it avoid the other project to manage and support the included plugin. This principle is called BOM, for Bill of Material. In maven, you can not have 3 poms for one same project. Maven is compilating the 3 poms inside a single one that is merged with the superPom which is handling maven dependencies used for the project. This is named the effective pom. This effective pom can be retrieved : mvn help:effective-pom Finally, depending on your needs, it possible to exclude dependencies. |
|||
What is a Maven wrapper: http://jakub.marchwicki.pl/posts/2015/06/04/maven-wrapper/
##### f. Write my own plugin:#####
- Git project available here.
- Usefull commands:
- First: Creation of the plugin project through an archetyp:
- Second: To check that the current artifact (in fact the plugin here) the plugin is in repo:
mvn archetype:create -DgroupId=checkArtifactPlugin -DartifactId= checkArtifactPlugin -DarchetypeGroupId=checkArtifactPlugin -DarchetypeArtifactId=maven-archetype-mojo
mvn mavenartifactplugin.mavenartifactcheck:mavenartifactcheck:0.0.1-SNAPSHOT:exists
to get the parameter through the console : mavenartifactplugin in settings.xml
- To check based on wished parameters
mvn mavenartifactplugin:mavenartifactcheck:0.0.1-SNAPSHOT:exists -Dexists.repository=http://repo1.maven.org/maven2 -Dexists.artifact=junilmkjljt-4.8.2.pom -Dexists.project=junit:junit:4.8.2 -X