Maven 2.0: Compile. Test. Deployment. Run


<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>

Created the project, we can add code to the project and use the Maven of all new skills. Note the following command must be in the directory where the pom.xml file to run.
- mvn test: run the application unit testing
- mvn package: the basis of the project to generate jar file
- mvn install: will the project jar file added to the library,  for this project rely on the use of
- mvn site: generate project-related information site
- mvn clean: to remove the target directory of the generated results
- mvn eclipse: eclipse: generate Eclipse project files

Next, we look at the place a little complicated point, we know that manually start a Java web project than to manually start a simple Java projects more time-consuming, however, the use of Maven can of can not make easy. The following example (and his party are in fact command) shows the structure of the construction project.
mvn archetype: create-DgroupId = com.oreilly
-DartifactId = Oreilly
-DarchetypeArtifactId = maven-archetype-webapp

The results generated by the structure as follows:
Oreilly
---- src
---- main
---- resources
---- webapp
---- WEB-INF

This time, because of U.S. support for the project will be included in the war file in web resources and set up different. pom.xml file will be included in his party to show that the project should be packaged into a war file: <packaging> war </ packaging>. Now you can use mvn package command to generate the war file. Not have to worry about how to get from the WEB-INF/lib directory to rely on items, in the reliance on property value is set to compile the case, Maven will automatically include dependencies. The following code can also be added to the pom.xml file to change the war file name:

<build>
<finalName> PromoteC </ finalName>
</ build>

Reliance on management

Create a good project structure, you are done adding some code, test and compile a good application, the next can take a look at how to deal with Maven are dependent on the relationship. To the project in order to add a dependent, the need to rely on items added to the pom.xml file. When the next time you run Maven, it will be ibiblio repository of this dependence and this dependence on items added to the project build path.

Dependence have questions about a few important things worth noting. At the time of writing, Maven in the biggest trouble is that it is not from the Maven repository to obtain Sun's jar file. Sun attributed the problem in its code set licensing restrictions. Solutions to this problem there are two types of download the code to install them in your local repository, and the other is to do an external statement, the statement will point to the file system dependent on the location of items. Sun hopes to be able to as soon as possible to create your own repository, in spite of this, Maven will also be upgraded to make it be able to download these resources, only in the download before it will prompt the user to accept the license agreement.

Another trouble is sometimes used in place up-to-date database file may be stored in the remote database does not exist. Another possibility is that because of unable to access Internet, all of the necessary dependence of all the local access. These problems the best solution is to install the jar file to the local storage repository. To the local repository on a web server is also to facilitate the move, so that the entire development team can benefit from this, everyone there is no need to go to manage their own repository of. Changes in the Maven repository path simply edit the installation directory under the conf folder to the following settings.xml file.

In reliance on the use of Maven are simple. Let us look at the pom.xml file to the above add a dependent of the situation. Although we have been using JUnit, but let us be powerful Quartz library to add to the project. Quartz is a pure Java using prepared about the timing of the open source project, it is your timing needs a very good selection.

<dependency>
<groupId> quartz </ groupId>
<artifactId> quartz </ artifactId>
<version> 1.5.1 </ version>
<scope> compile </ scope>
</ dependency>

Software companies are usually a kind of approach is to build multiple projects to the primary products. Maintenance of chain and a one-time dependencies to build the entire product can be a challenge, but if you are using Maven, then things will become easy. If you create a point to the other sub-module pom.xml parent, Maven will you handle the entire building process. It will analyze each sub-module pom.xml file, and in accordance with these sub-modules of the interdependence of the order to build the project. If each item clearly dependent on their items, then the sub-modules in the parent document to place the order are not to cause any effect at all. However, taking into account other developers, the best guarantee that the father of sub-modules in the pom.xml file to place the order and you expect the order of sub-project was to build the same. Now we watch a sample.
the main pom.xml file as follows:

<project>
<modelVersion> 4.0.0 </ modelVersion>
<groupId> com.oreilly </ groupId>
<version> 1.0-SNAPSHOT </ version>
<artifactId> my-app </ artifactId>
<packaging> pom </ packaging>
<modules>
<module> Common </ module>
<module> Utilities </ module>
<module> Application </ module>
<module> WebApplication </ module>
</ modules>
</ project>

We need to ensure that WebApplication module contains all the three jar package, so need to rely on the jar of package declaration. In this example, Utilities project depends on the Common project, so the project required Utilities to add a Common project dependency. Application module is also the same token, because it relies on Common and Utilities Project, Utilities also depends on the Common. If this example has 60 sub-modules, and they are interdependent, which makes it difficult for newly developed items calculated dependent on other projects, so this is precisely the requirement to ensure that the parent pom.xml file in the project to place the order to remove the causes.

Utility modules are the following dependencies:

<dependencies>
<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Common </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>
</ dependencies>

The following is a statement of how the dependence of Application modules:

<dependencies>
<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Common </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>
<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Utilities </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>
</ dependencies>

Finally WebApplication modules are dependent on item:

<dependencies>
<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Common </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>
<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Utilities </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>

<dependency>
<groupId> com.oreilly </ groupId>
<artifactId> Application </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ dependency>

</ dependencies>

Now, we need to do is for each sub-module pom.xml files to add the elements to show that they are part of a logical construct:

<parent>
<groupId> com.oreilly </ groupId>
<artifactId> my-app </ artifactId>
<version> 1.0-SNAPSHOT </ version>
</ parent>

Parent pom.xml file is located in the same directory, has the project directory existence: Common, Utilities, Application, and WebApplication. When we run in the directory mvn package command, these projects will be in accordance with the dependence on the order to build.

Plug-ins and statements

Maven2.0 have a large number of plug-ins can be used. Unfortunately, because of Maven rewriting, Maven1.0 plug-ins can not be used in 2.0. Nevertheless, there are some that can be used Maven2.0 plug-ins. The following pom.xml configuration file examples are plug-ins directly from the website Maven2.0 come. The plug-ins are used to configure the compiler options.

<plugins>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-compiler-plugin </ artifactId>
<configuration>
<source> 1.5 </ source>
<target> 1.5 </ target>
</ configuration>
</ plugin>
</ plugins>

Statements Maven plug-ins can be used to generate different statements, these statements are in when you use the mvn site command to generate the project site generated. The following example demonstrates how to use <reporting> element to configure such a plug-in.

<reporting>
<artifactId> NRC </ artifactId> <! - the item id, and GROUPID component of this project with a unique id, to use in other projects maven2 ->
<packaging> jar </ packaging> <! - Finally this project packaging types, here are labeled jar package ->
<version> 1.0 </ version> <! - version number ->
<name> kenan nrc batch </ name> <! - project name, different from the artifactId ->
<description> insert a nrc in kenan db </ description> <! - the project description, will appear in the generated project site ->
<build> <! - projects to build information ->
<sourceDirectory> src </ sourceDirectory>
<testSourceDirectory> test </ testSourceDirectory>
<outputDirectory> target \ classes </ outputDirectory>
<testOutputDirectory> target \ test-classes </ testOutputDirectory>
<directory> target </ directory> <! - build generated files (jar, site, etc.) location ->
</ build>
<dependencies> <! - the project to rely on external packages, maven one of the advantages ->
<dependency>
<groupId> junit </ groupId>
<artifactId> junit </ artifactId>
<version> 3.8.1 </ version>
<scope> test </ scope>
</ dependency>
</ dependencies>
<reporting> <! - project reports, has a lot of plug-ins available for selection ->
<outputDirectory> target / site </ outputDirectory>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-javadoc-plugin </ artifactId>
</ plugin>
</ plugins>
</ reporting>
</ project>

You configure your pom file, you can use the following command to run maven2

mvn compiler: compile compile your original document
mvn jar: jar packaging
mvn site: site to generate the project site

Followed by introduction of various elements of pom.xml

developor, organization and mail lit
build
dependencies
reporting

1.developor, organization and mail list

Medium pom can add a list of developers, project organization information, mailing list, which will appear in the information generated by the project site

<organization>
<name> CTZJ BSS </ name>
</ organization>
<developers>
<developer>
<name> Chen Lin </ name>
<organization> ZJHCsoft </ organization>
<email> chenlin@zjhcsoft.com </ email>
</ developer>
</ developers>
<mailingLists>
<mailingList>
<name> $ (pom.name) Dev List </ name>
</ mailingList>
<mailingList>
<name> $ (pom.name) User List </ name>
</ mailingList>
</ mailingLists>

2. Build

Configuration information to build the project, there are specified directory, including source code, testing, source code, the output directory, resource directory and so on. If there are no special needs of the pom has been more than enough

3. Dependencies

Configuration items used to package the jar. Since maven think of the jar package management is one of its essential characteristics. Here is a jar package configurations:

<dependency>
<groupId> concurrent </ groupId>
<artifactId> concurrent </ artifactId>
<version> 1.3.4 </ version>
<scope> compile </ scope>
</ dependency>

groupId: there is no specific meaning, the general organization of the jar from the same package have the same value GROUPID
artifactId: the jar of the id, typically the name of the jar package
version: jar package version number, under normal circumstances, artifactId-version.jar is the jar package file name
scope: this jar will affect the scope, compile saying that it will be used when compiling the source code. There are test, provide, runtime equivalent

If you do not know jar package GROUPID and artifactId, can go to http://www.mavenregistry.com/ Query

Once the designation has been made, when you compile, it will be to your local resource library

Also, if you introduce a local package, you need to first install it to a local database (repository), use the following command

mvn install: install-file-DgroupId =% groupId%-DartifactId =% artifactId%-DgeneratePom = true-Dfile =% file%-Dpackaging = jar-Dversion =% version%

4. Reporting

Through the use of the corresponding report plug-in can generate a wide range of report. Such as javadoc, junit test report, code inspection

Commonly used have

<plugin>
<groupId> org.apache.maven.plugins </ groupId> <! - java doc ->
<artifactId> maven-javadoc-plugin </ artifactId>
</ plugin>
<plugin>
<groupId> org.apache.maven.plugins </ groupId> <! - code check ->
<artifactId> maven-checkstyle-plugin </ artifactId>
<configuration>
<configLocation> config / sun_checks.xml </ configLocation>
</ configuration>
</ plugin>
<plugin>
<groupId> org.codehaus.mojo </ groupId> <! - test results statements ->
<artifactId> surefire-report-maven-plugin </ artifactId>
</ plugin>
<plugin>
<groupId> org.codehaus.mojo </ groupId> <! - the source code of the HTML ->
<artifactId> jxr-maven-plugin </ artifactId>
</ plugin>

In the maven the official website can be found have more report plugin

Finally a relatively complete pom.xml looks like this:

<? xml version = "1.0" encoding = "UTF-8"?>
<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>
<name> CutoverModule </ name>
<groupId> ctzj_batch </ groupId>
<artifactId> eai_cutover </ artifactId>
<url> http://134.98.83.137:8081/scarab/issues </ url>
<inceptionYear> 2006 </ inceptionYear>
<organization>
<name> CTZJ BSS </ name>
<url> http://www.zjhcsoft.com </ url>
</ organization>
<version> 1.0 </ version>
<description> 97 cutover batch module: invoke eai-adapter to update the phone number or value of
c4/c5 </ description>
<developers>
<developer>
<name> Chen Lin </ name>
<organization> ZJHCSoft </ organization>
<email> chenlin@zjhcsoft.com </ email>
</ developer>
</ developers>
<mailingLists>
<mailingList>
<name> $ (pom.name) Dev List </ name>
</ mailingList>
</ mailingLists>
<build>
<sourceDirectory> src </ sourceDirectory>
<testSourceDirectory> test </ testSourceDirectory>
<outputDirectory> target \ classes </ outputDirectory>
<testOutputDirectory> target \ test-classes </ testOutputDirectory>
<directory> target </ directory>
<plugins>
<plugin>
<artifactId> maven-clean-plugin </ artifactId>
<version> 2.1 </ version>
</ plugin>
<plugin>
<artifactId> maven-resources-plugin </ artifactId>
<version> 2.1 </ version>
</ plugin>
<plugin>
<artifactId> maven-compiler-plugin </ artifactId>
<version> 2.0 </ version>
</ plugin>
<plugin>
<artifactId> maven-surefire-plugin </ artifactId>
<version> 2.1.2 </ version>
</ plugin>
<plugin>
<artifactId> maven-jar-plugin </ artifactId>
<version> 2.0 </ version>
</ plugin>
<plugin>
<artifactId> maven-install-plugin </ artifactId>
<version> 2.1 </ version>
</ plugin>
</ plugins>
</ build>
<ciManagement>
<system> continuum </ system>
<url> http://localhost:8080/continuum/servlet/continuum </ url>
</ ciManagement>
<scm>
<connection> scm: local | E: / eclipseworkshop | EAIBatch </ connection>
</ scm>
<dependencies>
<dependency>
<groupId> ctzj_batch </ groupId>
<artifactId> Eaiadapter </ artifactId>
<version> 1.0 </ version>
<scope> compile </ scope>
</ dependency>
<dependency>
<groupId> ctzj_batch </ groupId>
<artifactId> ta </ artifactId>
<version> 1.0 </ version>
<scope> compile </ scope>
</ dependency>
<dependency>
<groupId> ctzj_batch </ groupId>
<artifactId> grnds-common </ artifactId>
<version> 4.1 </ version>
<scope> compile </ scope>
</ dependency>
<dependency>
<groupId> junit </ groupId>
<artifactId> junit </ artifactId>
<version> 3.8.1 </ version>
<scope> test </ scope>
</ dependency>
<dependency>
<groupId> concurrent </ groupId>
<artifactId> concurrent </ artifactId>
<version> 1.3.4 </ version>
<scope> compile </ scope>
</ dependency>
</ dependencies>
<reporting>
<outputDirectory> target / site </ outputDirectory>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-javadoc-plugin </ artifactId>
</ plugin>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-checkstyle-plugin </ artifactId>
<configuration>
<configLocation> config / sun_checks.xml </ configLocation>
</ configuration>
</ plugin>
<plugin>
<groupId> org.codehaus.mojo </ groupId>
<artifactId> surefire-report-maven-plugin </ artifactId>
</ plugin>
<plugin>
<groupId> org.codehaus.mojo </ groupId>
<artifactId> jxr-maven-plugin </ artifactId>
</ plugin>
</ plugins>
</ reporting>
</ project>



maven structure of the POM



[Author: | Source: | Hits: <script src="maven The structure of the POM - Tutorial - Source code - Download .files/Click.htm" type="text/javascript"> </ script> 65]

<! - Take part in the development of staff ->
<developers>
<developer>
<name> Dave Blackett (Jetspeed logos) </ name>
<id/>
<email> blackett@black.com </ email>
<organization/>
</ developer>
......
</ developers>

****** ****** Part of project-related and a central resource library components used in conjunction will eliminate a few common questions to build

<!------------------------------------------------ ---- ->
<! - Project-related part ->
<!------------------------------------------------ ---- ->

<! - build process set up maven based on the classpath, and maven to remote repository will automatically download these dependencies, such as type are jar, download the file is <artifactId> - <version>. jar ->
<dependencies>

<! - Project dependent JAR files "activation-1.0.1.jar", documents in Maven repository
The activation / jars subdirectory, and if resources can not be found, maven will automatically download maven.repo.remote from ( "maven install use" mentioned in the amendment, it is because the default setting maven remote resource base can not connect I) ->
<dependency>
<id> activation </ id>
<version> 1.0.1 </ version>
<properties>
<war.bundle.jar> true </ war.bundle.jar>
</ properties>
</ dependency>
</ dependencies>

Maven told GROUPID resources subdirectory hangar which contains the relevant documents.
artifactId tell Maven that uniquely identifies the component. If you are jar files, can be understood as to remove the suffix and version number of the file name.
express the relevant version of the version number.
jar (optional) express the relevance of the JAR file. In most cases, from the relevance of the <artifactId> and <version> structure JAR file name.
type (optional) the type of correlation; such as version, such as jar and distribution. Default values are jar.
url optional) associated projects URL, in the correlation are found on the Internet very useful third-party libraries.
properties dependency also can set properties, the specific reference to their quoted plugin

Construction of part of the project ****** ******
<!------------------------------------------------ ---- ->
<! - Project build and reports part ->
<!------------------------------------------------ ---- ->

<build>
<! - (OPTIONAL) the definition of the source file location. ->
<sourceDirectory> src / java </ sourceDirectory>

<! - (OPTIONAL) the definition of unit test source file location. ->
<unitTestSourceDirectory> test / java </ unitTestSourceDirectory>

<! - (OPTIONAL) unit test cases document model. ->
<unitTest>
<! - Exclude testing ->
<excludes>
<exclude> org / apache / jetspeed / services / registry / TestRegistryCategories.java </ exclude>
......
</ excludes>
<! - Contains test ->
<includes>
<include> ** / Test *. java </ include>
</ includes>
<! - Unit testing the use of resources ->
<resources>
<resource>
<! - Resource location, project.xml relative path ->
<directory> $ (basedir) / src / java </ directory>
<! - Exclude files ->
<excludes>
<exclude> **/*. java </ exclude>
</ excludes>
<! - Include files ->
<includes>
<include> org / apache / jetspeed / modules / localization / JetspeedLocalization_ *. properties </ include>
</ includes>
</ resource>
.......
</ resources>
</ unitTest>

<! - Project to use the resources, structure and unit testing the same structure as the use of resources ->
<resources>
<resource>
......
</ resource>
.......
</ resources>

<! - (OPTIONAL) Use maven site generated when the site was quoted, and to specify the order in the navigation section, report to generate the report of the designated plugin ->
<reports>
<report>
<report> maven-tasklist-plugin </ report>
<report> maven-changelog-plugin </ report>
<report> maven-javadoc-plugin </ report>
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Maven 2.0: Compile. Test. Deployment. Run

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

  • spring struts2.0 hibernate bug killer 1

    exception There is no Action mapped for namespace / and action name checkLogin. - [Unknown location] com.opensymphony.xwork2.DefaultActionProxy.prepare (DefaultActionProxy.java: 186) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries