2015年6月27日 星期六

Maven Note - Basic

I have study this project manage tool, Maven, for a while. But, I still feel confused for this tool. I used Ant for my previous job. This is an easy tool to manage project. But, it does not have the dependency management. So, we still need to learn it. Actually, if you are java coder, you should learn it for its basic mech anism. Other program languages also have their own project manage tool, but this is not our main goal. So, I make a list which your should learn.

Installation
The Concept of Maven
Create a project
Know about pom.xml



Installation


How to install maven, you can google. If you use mac, you can used Homebrew is a very great package manage tool.
brew install maven



The Concept of Maven


There are some good tutorials of Maven
Chinese-
http://www.codedata.com.tw/java/understanding-gradle-2-maven/
http://juvenshun.iteye.com/blog/213959
http://takeshi-experience.blogspot.tw/2011/12/java-mvc-sample-2-with-maven.html
English-
http://www.javacodegeeks.com/?s=maven

Using a simply way to explain, there are three life cycle clean, default, site.
clean life cycle - you should clean your previous building content, before you build new one.
build life cycle - build your project, and deploy it.
site life cycle - Analyze the project data.

There are many phases defined for each life cycle. You can read this
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference 

Each phase define its workflow and goal to execute.
goal is a maven plugin to execute the command, like compiler:compile.
compiler is the plugin name, and compile is the goal name.

You can also combine these three lifecycle, like mvn clean compile site. compile is a phase of default lifecycle.



Create a project


You can use command line to build a simple project, like

mvn archetype:generate -DgroupId=com.di.maven -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Actually, you can also use Eclipse( or other IDE which support Maven) to build Maven Project.
There pictures are how to create a Maven project in Eclipse.








Know about pom.xml


This is a simple pom.xml file
<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.di.maven</groupId>
  <artifactId>hello-world</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>hello-world</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Maven is a convention over configuration software design pattern.  This is why pom.xml file is so sample. We have a super pom.xml which config a lot of detail. So, we can start a sample project faster. But, it still confused for me. Actually, when you need to setup other configuration, you still need to figure out the correct way to config. This topic is still a problem for me. Anyway, let start it easier. In this pom.xml file, we only need to understand how to add the jar files which is needed for our project. You only add the dependency config to <dependencies>. It will auto download the jar files we need. This is also why we need to use Maven. Maven can solve the dependency hell in some situations. Otherwise, <scope> is mean the dependency will effect which s cenario will be used.

You also can search the dependency you need in
http://mvnrepository.com/

Further, there is an example which we had built before.
https://github.com/bpmabpma/JBossExmple/blob/master/springMvc/pom.xml

We can see a lot new Xml elements show up, such as <properties>, <dependencyManagement>,  <<build>, <plugin>, <profiles>. I think they are very important. But, in my present situation, I should build my project be my primary job. So, when I finish build the major part of coding. I should return Maven, and build the CI system based on Maven.

Moreover, if you really need to an expert of Maven. You should read this
http://maven.apache.org/guides/index.html




沒有留言:

張貼留言