Introduction Introduce Maven
Maven Resources
The difference between Maven and Ant
The basic function of Maven
Maven use
Maven installation and configuration
Maven's basic use of Java project implementation to create Maven project run the Maven project implementation to create Web project Maven project run the Maven project
POM basic configuration file
POM Introduction What is POM
Express views the relationship between the basic set up in collaboration
The relationship between POM-dependent relationship between the paradigmatic relations of succession to configure other settings other offline install a third-party packages to deploy to tomcat
Not mentioned the contents of
Maven concise curriculum
Main Objective: To facilitate the development of staff and running faster
1. Maven Introduction
1.1. Brief introduction
java prepared for automated tools to build systems.
The current version is 2.0.9, pay attention to maven2 and maven1 have very different reading of third parties when necessary to distinguish between versions of documents.
1.2. Maven resources
See the official website;
The 5 minute test, the official entry summary document;
Getting Started Tutorial, official entry documents;
Build Cookbook, the official cookbook;
POM Reference, POM file settings reference
Settings Reference, settings file settings reference
Better Builds with Maven, free e-books, download Registration required.
1.3. Maven and Ant distinction
Maven is gradually replaced by Ant, a lot of java open source software (Spring, Struts2 ... ...) are already using maven.
Do not need to write complex scripts deal with;
Declarative management of library-dependent.
1.4. Maven to build the basic functions: to generate such as class, jar, war or ear files
Generated files: such as to generate javadoc, web documents
Generate reports: such as JUnit test report
Generation-dependent library: generate documentation on many other software projects rely on
On the SCM: SCM (Software Configuration Management), software configuration management, such as version control, such as bug management and so on
Published: Published for the distribution of generated packets, such as to generate Struts2 distribution package, for submission to the users
Deployment: For instance, web applications, automatically deployed to the specified server on
Writing my goods through the management of small examples, combined with maven and svn presentation function.
2. Maven use
2.1. Maven installation and configuration
From the official website download the latest Maven distribution package http://maven.apache.org/download.html, currently 2.0.9;
Extracted to the local;
Configuration maven, the maven / bin directory to the windows environment variable settings in the Path
Maven check whether the installation is successful, at the command line to perform
mvn-version
2.2. Maven's basic use of
Introduce the basic use of Maven through the preparation of a simple command line java and web projects.
2.2.1. Implementation Java project
Through the maven at the command line to create an ordinary java project, which is the main method of implementing the project or class library jar files.
2.2.1.1. Create Maven project
Executive:
mvn archetype: generate
At the interactive interface:
Choose a number: Enter you, that is, select 15
Define value for groupId: Enter the organization id, such as easymorse.com
Define value for artifactId: Enter the project name, such as helloworld
Define value for version: Enter the version number, you can directly enter, the default is 1.0-SNAPSHOT
Define value for package: java package name, such as com.easymorse
Then enter that confirm the input.
Observation helloworld directory (Define value for artifactId enter the name of the project) under the generated files and directories:
Construction document project: pom.xml
Code framework: src \ main \ java \ com \ easymorse \ App.java
Test code: src \ test \ java \ com \ easymorse \ AppTest.java
2.2.1.2. Run Maven project
Command line to enter the helloworld directory Define value for artifactId enter the name of the project).
Project package
mvn package
Checking command to generate what?
target directory
Compiling the code
Compile the test code
The use of JUnit testing and generate report
Jar file to generate code
Running packaged jar file:
java-cp target \ helloworld-1.0-SNAPSHOT.jar com.easymorse.App
Compiler source code
mvn compile
Compile and test
mvn test
Generated empty file
mvn clean
To maven project into eclipse project
Running command line:
mvn eclipse: eclipse
Open the eclipse, select the menu: file> import> general> existing projects into workspace, select the directory in the dialog box, you can import.
If you want to clear the configuration on the eclipse project information:
mvn-Dwtpversion = 1.0 eclipse: clean
The use of United
mvn eclipse: clean clean
2.2.2. Web project implementation
Through the maven at the command line to create java web project.
2.2.2.1. Create Maven project
At the command line input, this step and create a java project like this:
mvn archetype: generate
Interactive steps Description:
Choose a number: Enter you, that is, select 18, here and java not the same as ordinary items
Define value for groupId: Enter the organization id, such as easymorse.com
Define value for artifactId: Enter the project name, such as helloworld
Define value for version: Enter the version number, you can directly enter, the default is 1.0-SNAPSHOT
Define value for package: java package name, such as com.easymorse
Then enter that confirm the input.
Required an increase in the pom.xml file servlet container plug-ins:
<build>
<plugins>
<plugin>
<groupId> org.codehaus.mojo </ groupId>
<artifactId> tomcat-maven-plugin </ artifactId>
</ plugin>
<plugin>
<groupId> org.mortbay.jetty </ groupId>
<artifactId> maven-jetty-plugin </ artifactId>
<version> 6.1.6 </ version>
</ plugin>
<plugin>
<artifactId> maven-compiler-plugin </ artifactId>
<configuration>
<source> 1.6 </ source>
<target> 1.6 </ target>
<encoding> UTF-8 </ encoding>
</ configuration>
</ plugin>
</ plugins>
</ build>
tomcat plugin
jetty plugin
Compiler plug-in configuration
The role of repository directory
repository location, in the user directory. m2 directory.
The role of repository directory, which rely heavily on library cache.
2.2.2.2. Run Maven project
Project package
mvn package
Start tomcat
mvn tomcat: run
Start jetty
mvn jetty: run
Into eclipse project
mvn-Dwtpversion = 1.5 eclipse: eclipse
This plugin generates WTP web project.
Open the eclipse, select the menu: file> import> general> existing projects into workspace, select the directory in the dialog box, you can import.
In addition, at eclipse necessary to create a classpath variable, name: M2_REPO, value for users of the system under .m2/repository directory.
3. POM basic configuration file
3.1. POM Introduction
3.1.1. What is a POM
Project Object Model, the project object model.
Xml format through the pom.xml file.
Role similar to ant's build.xml file and more powerful.
The document used to manage: the source code, configuration files, the developer's information and the role of problem tracking system, organizational information, project authorization, project url, project dependencies and so on.
3.1.2. Quick View
A complete pom.xml file, placed in the project's root directory.
<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>
<! - The Basics ->
<groupId> ...</ groupId>
<artifactId> ...</ artifactId>
<version> ...</ version>
<packaging> ...</ packaging>
<dependencies> ...</ dependencies>
<parent> ...</ parent>
<dependencyManagement> ...</ dependencyManagement>
<modules> ...</ modules>
<properties> ...</ properties>
<! - Build Settings ->
<build> ...</ build>
<reporting> ...</ reporting>
<! - More Project Information ->
<name> ...</ name>
<description> ...</ description>
<url> ...</ url>
<inceptionYear> ...</ inceptionYear>
<licenses> ...</ licenses>
<organization> ...</ organization>
<developers> ...</ developers>
<contributors> ...</ contributors>
<! - Environment Settings ->
<issueManagement> ...</ issueManagement>
<ciManagement> ...</ ciManagement>
<mailingLists> ...</ mailingLists>
<scm> ...</ scm>
<prerequisites> ...</ prerequisites>
<repositories> ...</ repositories>
<pluginRepositories> ...</ pluginRepositories>
<distributionManagement> ...</ distributionManagement>
<profiles> ...</ profiles>
</ project>
3.2. Basic Settings
3.2.1. Collaborative relationships
<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> org.codehaus.mojo </ groupId>
<artifactId> my-project </ artifactId>
<version> 1.0 </ version>
<packaging> war </ packaging>
</ project>
groupId: Organize logo, such as: org.codehaus.mojo, at M2_REPO directory will be: org / codehaus / mojo directory.
artifactId: project name, for instance: my-project, at M2_REPO directory will be: org / codehaus / mojo / my-project directory.
version: version number, for example: 1.0, at M2_REPO directory will be: org/codehaus/mojo/my-project/1.0 directory.
packaging: packaging format for: pom, jar, maven-plugin, ejb, war, ear, rar, par
3.2.2. POM relationship between
3.2.2.1. Dependence
A list of dependencies (dependency list) are an important part of POM.
<dependencies>
<dependency>
<groupId> junit </ groupId>
<artifactId> junit </ artifactId>
<version> 4.0 </ version>
<scope> test </ scope>
</ dependency>
...
</ dependencies>
groupId, artifactId, version:
scope: compile (default), provided, runtime, test, system
exclusions
How to find dependent libraries?
Generally through this website: http://www.mvnrepository.com
Inquiries such as hibernate, the results can be found in the list of library entries hibernate.
Click: http://www.mvnrepository.com/artifact/org.hibernate/hibernate,
Choose version such as 3.2.6ga, namely: http://www.mvnrepository.com/art ... / hibernate/3.2.6.ga
Copy of the article:
<dependency>
<groupId> org.hibernate </ groupId>
<artifactId> hibernate </ artifactId>
<version> 3.2.6.ga </ version>
</ dependency>
To your pom.xml file.
Whether it is still necessary to find hibernate dependency pom?
Do not need, hibernate will have pom, maven will automatically pom through its dependence on the library to find it.
3.2.2.2. Inheritance
Pom.xml configuration inheritance of other content.
maven provides a similar top java.lang.Object parent pom.xml file.
Through the following command to view the current pom.xml by the impact of ultra-pom.xml file:
mvn help: effective-pom
Various projects to create a reusable pom.xml file: http://easymorse.googlecode.com/svn/trunk/pom/pom.xml
Deployment of reusable want pom.xml file:
mvn install
In their own pom file to inherit these pom:
<parent>
<groupId> com.easymorse </ groupId>
<artifactId> pom </ artifactId>
<version> 0.1 </ version>
</ parent>
3.2.2.3. Paradigmatic relations
Used to aggregate multiple maven projects for a large project.
Such as directory structure is as follows:
.
| - Pom.xml
| - Module-a
`- Pom.xml
| - Module-b
`- Pom.xml
| - Module-c
`- Pom.xml
| - Foo-all
`- Pom.xml
So overall pom.xml file like this:
...
<modules>
<module> module-a </ module>
<module> module-b </ module>
<module> module-c </ module>
<module> foo-all </ module>
</ modules>
</ project>
Reference documents: http://maven.apache.org/plugins/maven-eclipse-plugin/reactor.html
There is misuse of the original sample, see: modules.rar
3.2.3. Other configuration
maven's property, are placeholder values, similar to EL, similar to ant's property, such as $ (X), can be used for any assignment pom file location.
Has the following categories:
env.X: operating system environment variables, such as $ (env.PATH)
project.x: pom files property, such as: <project> <version> 1.0 </ version> </ project>, quoted manner: $ (project.version)
settings.x: settings.xml file attributes, such as: <settings> <offline> false </ offline> </ settings>, quoted manner: $ (settings.offline)
Java System Properties: java.lang.System.getProperties () in the property, such as java.home, cited the way: $ (java.home)
Custom: pom file can be in: <properties> <installDir> c: / apps / cargo-installs </ installDir> </ properties>, quoted manner: $ (installDir)
3.3. Other
3.3.1. Settings offline
At. M2 directory created settings.xml file (if not)
<settings 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/xsd/settings-1.0.0.xsd ">
<offline> true </ offline>
</ settings>
3.3.2. Install a third-party packages
Often there is a third-party package, because some of the reasons, there is no on-line repository, do-it-yourself installation required.
Sun such as some versions of jar files, such as oracle driver.
Has oracle driver for example, drivers such as the path to c: / driver/ojdbc14.jar, are versions 10.2.0.3.0
The site can be found at: http://www.mvnrepository.com/artifact/com.oracle/ojdbc14 artifactId and groupId.
mvn install: install-file-DgroupId = com.oracle-DartifactId = ojdbc14-Dversion = 10.2.0.3.0-Dpackaging = jar-Dfile = c: / driver/ojdbc14.jar
This will depend on the pom quoted:
<dependency>
<groupId> com.oracle </ groupId>
<artifactId> ojdbc14 </ artifactId>
<version> 10.2.0.3.0 </ version>
</ dependency>
3.3.3. Deployed to tomcat
tomcat configuration users have administrative privileges: conf \ tomcat-users.xml
<? xml version ='1 .0 'encoding =' utf-8 '?>
<tomcat-users>
<role rolename="manager"/>
<user username="marshal" password="password" roles="manager"/>
</ tomcat-users>
At the tomcat plugin pom file, add the following:
<plugin>
<groupId> org.codehaus.mojo </ groupId>
<artifactId> tomcat-maven-plugin </ artifactId>
<configuration>
<url> http://localhost:8080/manager </ url>
<server> myserver </ server>
<path> / mycontext </ path>
</ configuration>
</ plugin>
Increase at .m2/settings.xml document:
<settings 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/xsd/settings-1.0.0.xsd ">
<servers>
<server>
<id> myserver </ id>
<username> marshal </ username>
<password> password </ password>
</ server>
</ servers>
</ settings>
Run the deployment package in maven project directory:
mvn tomcat: deploy
Then visit: http://localhost:8080/mycontext/ can.
Revocation of the deployment:
mvn tomcat: undeploy
Start web application:
mvn tomcat: start
Stop the web application:
mvn tomcat: stop
Re-deployment:
mvn tomcat: redeploy
Expand the deployment of war file:
mvn war: exploded tomcat: exploded
3.3.4. Not about the content of
settings.xml configuration
pom.xml detailed configuration
Ways custom plug-ins: http://maven.apache.org/plugins/maven-archetype-plugin/examples/archetype.html
Goal of the implementation of custom: <preGoal> <postGoal>
Inquiries and the use of plug-ins
Structures mirror repository
At maven call ant
work / training / java / maven / get start (2008-05-28 21:07:26 by marshal Edit)







