maven learning

2009-08-28  来源:本站原创  分类:Java  人气:654 

1. The establishment of a project can use the following command:
mvn archetype: create-DgroupId = demo-DartifactId = simple-DpackageName = testpackage-Dversion = 1.0
groupId for the logo, as the only local namespace name;
artifactId name for the project, after implementation of the folder name for the simple
packageName package name for the implementation of the directory name after it is generated by C: \ maven \ simple1 \ src \ main \ java \ testpackage

version is the version number, default is 1.0-SNAPSHOT
packaging the type of packing, the default is jar, war as a web application

2. To create web project
mvn archetype: create-DgroupId = testweb-DartifactId = testweb-DpackageName = cn.canofy.com-DarchetypeArtifactId = maven-archetype-webapp
Create your web application project, run mvn archetype: create, together with the parameters artifactId and groupId. ArchetypeArtifactId designated for the maven-archetype-webapp.

3.mvn install
mvn clean install
This command is used to compile, package, such as the implementation of action, if successful in the C: \ maven \ simple \ target directory to generate simple-1.0-SNAPSHOT.jar file

<dependencies>
<dependency>
<groupId> junit </ groupId>
<artifactId> junit </ artifactId>
<version> 3.8.1 </ version>
<scope> test </ scope>
</ dependency>
<dependency>
<groupId> javax.servlet </ groupId>
<artifactId> jstl </ artifactId>
<version> 1.1.2 </ version>
</ dependency>
<dependency>
<groupId> org.sonatype.mavenbook </ groupId>
<artifactId> project-a </ artifactId>
<version> 1.0 </ version>
<exclusions>
<exclusion>
<groupId> org.sonatype.mavenbook </ groupId>
<artifactId> project-b </ artifactId>
</ exclusion>
</ exclusions>
</ dependency>
</ dependencies>

Package-dependent, groupId, artifactId, version, and means the same as above, where the packets need to rely on the definition have been basically good, and what packages need to be able to search online the things on the list ~ ~ ~
scope is dependent on the scope of
exclusions to exclude a transfer of dependence

compile (compile scope)

compile is the default scope; If you do not provide a scope, that is the scope of the reliance on the scope of the compiler. The scope of the compiler rely on all available in the classpath, they will be packaged at the same time.
provided (the scope has been provided)

provided to rely on only in the JDK or a container when provided before the use-dependent. For example, if you developed a web application, you may need to compile classpath of the Servlet API can be used to compile a servlet, but you will not want to pack a good WAR contains the Servlet API; the Servlet API JAR from your servlet container or application server to provide. Dependent on the scope has been provided at compile classpath (not the run-time) available. They are not transitive, and it will not be packaged.
runtime (run-time range)

runtime dependence on the system running and test when necessary, but not required at compile time. For example, you may only need time to compile the JDBC API JAR, and only when running JDBC driver needs to achieve.
test (test range)

test range dependence in the general run-time compiler and do not need, they will have only compiled and tested in the testing stage can be used to run. Dependent on the scope of the test before??? In introduced.
system (system-wide)

rely on the system and provided a similar range, but you must provide an explicit system for the local JAR file path. To do so is to allow the local object-based compiler, which is part of the system library. This component should be always available, Maven will not be in the warehouse to find it. If you rely on the scope of a system-wide set, you must provide a systemPath element at the same time. Attention to the area is not recommended (you should go as far as possible from the public or a customized quote to rely on Maven warehouse).

4.mvn test
The implementation of the project's unit testing

5. <project>
[...]
<build>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-surefire-plugin </ artifactId>
<configuration>
<testFailureIgnore> true </ testFailureIgnore>
</ configuration>
</ plugin>
</ plugins>
</ build>
[...]
</ project>
Ignore the test failure
Typically, you will develop a unit test with a lot of system failure. If you are the practice of Test Driven Development (TDD), you may use your test fails to measure how far away from the completion of the project. If you have a failure of unit testing, but you still want to have to build the output, you must tell Maven test failure ignored it. Maven encountered when a test fails, it is the default behavior is to stop the current build. If you want to continue to build the project, even if the Surefire plug the unit encountered a failed test, you will need to set up the Surefire configuration properties for testFailureIgnore this true.
You can also specify mven parameters: mvn test-Dmaven.test.failure.ignore = true

6. <project>
[...]
<build>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </ groupId>
<artifactId> maven-surefire-plugin </ artifactId>
<configuration>
<skip> true </ skip>
</ configuration>
</ plugin>
</ plugins>
</ build>
[...]
</ project>
You may want to configure Maven to skip unit testing entirely. May you have a great system, unit testing a lot of minutes it takes to complete, and you do not want to generate the final output before the completion of unit testing and so on. You may be the work of the above in a legacy system, the system has a series of failed unit tests, you may only want to generate a JAR and not to the repairs of all the unit testing. Maven provides the ability to skip unit testing, only need to use the skip parameter Surefire plugin. In the command line, simply add to any target attribute maven.test.skip will be able to skip the test: mvn install-Dmaven.test.skip = true

7.
<project>
...
<properties>
<hibernate.annotations.version> 3.3.0.ga </ hibernate.annotations.version>
</ properties>

<dependencyManagement>
...
<dependency>
<groupId> org.hibernate </ groupId>
<artifactId> hibernate-annotations </ artifactId>
<version> $ (hibernate.annotations.version) </ version>
</ dependency>
<dependency>
<groupId> org.hibernate </ groupId>
<artifactId> hibernate-commons-annotations </ artifactId>
<version> $ (hibernate.annotations.version) </ version>
</ dependency>
...
</ dependencyManagement>
...
</ project>
In a multi-module development, reliance on management for the most important measure is to reduce our reliance on repeat configuration, which is dependent on the same layer mentioned parent, and can repeat version of the definition of property to deal with

8. <project>
[...]
<build>
<finalName> simple-webapp </ finalName>
<plugins>
<plugin>
<groupId> org.mortbay.jetty </ groupId>
<artifactId> maven-jetty-plugin </ artifactId>
</ plugin>
</ plugins>
</ build>
[...]
</ project>
Jetty plugin configuration.
You have to compile, test and package your web application, you will want to deploy it to a servlet container, and then test the plug-ins created by the Maven Archetype of index.jsp. Under normal circumstances, you need to download the Jetty or Apache Tomcat, extract the distribution package, copy your application WAR file to webapps / directory, and then restart your container. Now, to achieve the same purpose, you no longer need to do these things. Instead, you can use the Maven Jetty plugin in Maven run your web applications. To do this, you need the pom.xml in the project to configure Maven Jetty plug-ins.
Finally, run mvn jetty: run command to start Jetty

9. Add dependent J2EE
<project>
[...]
<dependencies>
[...]
<dependency>
<groupId> org.apache.geronimo.specs </ groupId>
<artifactId> geronimo-servlet_2.4_spec </ artifactId>
<version> 1.1.1 </ version>
<scope> provided </ scope>
</ dependency>
</ dependencies>
[...]
</ project>
Servlet 2.4 specification Add as dependent

<project>
[...]
<dependencies>
[...]
<dependency>
<groupId> org.apache.geronimo.specs </ groupId>
<artifactId> geronimo-jsp_2.0_spec </ artifactId>
<version> 1.1 </ version>
<scope> provided </ scope>
</ dependency>
</ dependencies>
[...]
</ project>
Add a JSP 2.0 specification as a dependent

src / main / java / org / sonatype / mavenbook / web directory in the following categories selvlet write

10. Multi-module Enterprise Project
Sub-module
<modules>
<module> simple-command </ module>
<module> simple-model </ module>
<module> simple-weather </ module>
<module> simple-persist </ module>
<module> simple-webapp </ module>
</ modules>
It cited the five sub-modules: simple-command, simple-model, simple-weather, simple-persist, and simple-webapp.
<parent>
<groupId> org.sonatype.mavenbook.ch07 </ groupId>
<artifactId> simple-parent </ artifactId>
<version> 1.0 </ version>
</ parent>
The definition of sub-module
Most complex multi-module Maven project POM preferred top-level defined in all versions. The impact of top-level POM became the focus of the project changes.
<project>
...
<properties>
<hibernate.annotations.version> 3.3.0.ga </ hibernate.annotations.version>
<hsqldb.version> 1.8.0.7 </ hsqldb.version>
</ properties>
...
</ project>

Hibernate3 plug-ins need to use the database structure.
mvn hibernate3: hbm2ddl

mvn dependency: analyze
Target analysis of the project to see if there are directly dependent on, or some quoted statements, but not directly dependent on.
mvn dependency: tree
dependency: tree goal, the goal of the project will be listed in all of the direct and transitive dependence.

11. In the use of multi-module, the father of the type of module must be packaged pom
Pom sub modules do not need to set the groupId and version, it has a direct utility of the parent module groupId and version
When the Maven sub-modules with the implementation of a project when, Maven first loading the parent POM, and then positioning all of the sub-module POM. Maven and then all of these projects to the POM Add a group called the Maven reactor (Reactor) in the East and West, which is responsible for analyzing the dependencies between modules. Reactor components to deal with this sort to ensure that the module can be independent of each other in an appropriate order to be compiled and installed.

12.POM best practices (particularly important function, just a statement in reliance on a group of other works, is only to call this can be a pom to reuse the function)
If you have a set of logically grouped together dependence. You can create a package for the pom to be dependent on the project are grouped together.
For example, let us assume that your application uses Hibernate, a popular object-relational mapping framework. All projects use Hibernate may also depend on the Spring Framework and the MySQL JDBC driver. You can create a special POM, in addition to its reliance on a statement other than a group of common and do nothing. So you do not need to use in each Hibernate, Spring, and MySQL projects to include all of these rely on. You can create a project called persistence-deps (referred to as persistent dependence), and then let each of the projects need sustained depends on the project to facilitate this.
In a separate project to consolidate POM-dependent
<project>
<groupId> org.sonatype.mavenbook </ groupId>
<artifactId> persistence-deps </ artifactId>
<version> 1.0 </ version>
<packaging> pom </ packaging>
<dependencies>
<dependency>
<groupId> org.hibernate </ groupId>
<artifactId> hibernate </ artifactId>
<version> $ (hibernateVersion) </ version>
</ dependency>
<dependency>
<groupId> org.hibernate </ groupId>
<artifactId> hibernate-annotations </ artifactId>
<version> $ (hibernateAnnotationsVersion) </ version>
</ dependency>
<dependency>
<groupId> org.springframework </ groupId>
<artifactId> spring-hibernate3 </ artifactId>
<version> $ (springVersion) </ version>
</ dependency>
<dependency>
<groupId> mysql </ groupId>
<artifactId> mysql-connector-java </ artifactId>
<version> $ (mysqlVersion) </ version>
</ dependency>
</ dependencies>
<properties>
<mysqlVersion> (5.1,) </ mysqlVersion>
<springVersion> (2.0.6,) </ springVersion>
<hibernateVersion> 3.2.5.ga </ hibernateVersion>
<hibernateAnnotationsVersion> 3.3.0.ga </ hibernateAnnotationsVersion>
</ properties>
</ project>

Statement of a dependence on the POM
<project>
<description> This is a project requiring JDBC </ description>
...
<dependencies>
...
<dependency>
<groupId> org.sonatype.mavenbook </ groupId>
<artifactId> persistence-deps </ artifactId>
<version> 1.0 </ version>
<type> pom </ type>
</ dependency>
</ dependencies>
</ project>

13. M2eclipse plug-ins installed
a. install Subclipse Subclipse 1.2: http://subclipse.tigris.org/update_1.2.x
b. the installation of Mylyn Mylyn (Eclipse 3.3): http://download.eclipse.org/tools/mylyn/update/e3.3
Mylyn (Eclipse 3.4): http://download.eclipse.org/tools/mylyn/update/e3.4
c installed AspectJ Tools Platform (AJDT) AJDT (Eclipse 3.3): http://download.eclipse.org/tools/ajdt/33/update
AJDT (Eclipse 3.4): http://download.eclipse.org/tools/ajdt/34/dev/update
d install the Web Tools Platform (WTP) WTP: http://download.eclipse.org/webtools/updates/

相关文章
  • maven learning 2009-08-28

    1. The establishment of a project can use the following command: mvn archetype: create-DgroupId = demo-DartifactId = simple-DpackageName = testpackage-Dversion = 1.0 groupId for the logo, as the only local namespace name; artifactId name for the proj

  • Maven learning and problem solving 2010-06-20

    Eclispe install Maven plug-in, build all project issues can not be recompiled solution: the command line mode mvn eclipse: eclipse. Re-generate project files.

  • maven Learning 9 - in Eclipse 3.2 using Maven 2 Plug-in 2010-12-22

    In Eclipse 3.2 using Maven 2 Plug-in This section covers the Eclipse, Maven 2.x Plug-in for some commonly used functions. Can use in your Maven 2 plug-in project functionality, you need to enable in an Eclipse Maven project properties. Right-click th

  • Maven Learning Summary (a) - Maven Getting Started 2010-12-27

    1. First, a brief description of some of the information about Maven: Maven is a project management tool. Maven Java-based platform for major service project to build, dependency management, and project information management. Maven can help automate

  • Maven Learning Summary (a) 2011-04-22

    mvn help: describe-Dplugin = help to get help plug-in, the output coordinates of the Maven plugin mvn help: describe-Dplugin = help-Dfull for a complete list with the target parameters mvn help: describe-Dplugin = compiler-Dgoal = compile information

  • maven learning Archive 2010-03-08

    To be a record, this is a good book

  • Maven5 minutes learning tutorial 2010-04-19

    maven learning a little trouble feeling harder than the ant number, in order to facilitate them to learn, hereby translation maven2 5 Minutes tutorial to others.

  • Mina framework for study notes (f) 2010-10-16

    Described in the previous section the basic concepts and Apache MINA specific usage, some high-level topics are discussed below. Apache MINA Many of operations are asynchronous, such as connection establishment, connection closed, there are data tran

  • Learning to use maven to see a very practical Tutorial 2010-07-17

    I am learning Maven, the perceived usefulness of the article, citing what. Update Maven Installation http://hi.baidu.com/% EE% D1% D7% D300544/blog/item/316669a2063504a1caefd0fb.html Maven2 Introduction http://hi.baidu.com/% EE% D1% D7% D300544/blog/

  • Learning to use maven to see a very practical introduction to tutorial (rookie level entry) 2010-08-20

    I. Introduction knew maven java project in the management of reputation and so he thought of learning to master, so a large amount of documentation. Discovery of these documents are the biggest names in java, most are standing mastered maven introduc

  • Practical learning maven Tutorial 2011-03-03

    Through the following three-step learning, the basic maven have a comprehensive understanding of: 1, to learn a good entry-maven tutorial collection! http://www.360doc.com/content/10/0303/22/284485_17481406.shtml 2, maven's eclipse plugin: m2eclipse

  • Learning finishing Maven 2010-11-09

    09/15 Objectives: maven function, write a simple script to run the project construction; Data 1: http://www.sonatype.com/books/maven-book/reference_zh/public-book.html Maven features: 1, a project object model (Project Object Model); 2, a set of stan

  • maven introduction of learning 2011-07-04

    http://www.cnblogs.com/hanwesley/articles/1948230.html Maven brief Maven is a project-based object model (Project Object Model), can be managed by a short description of the project's build, reporting and documentation of software project management

  • Spring Annotations Learning Letters (6) test 2010-02-13

    Since the system is based on notes self-contained, then the test based on whether it can rely on Spring annotation easy to complete? Resolutely No problem! Spring-Test duty-bound to complete this task! And, through the Spring-Test of the transaction

  • eclipse configure maven and create a project 2010-03-29

    1. Download the file to the D disk, ① jdk-1.6.0 URL: http://java.sun.com/javase/downloads/index.jsp # docs ② apache-ant-1.7.0 URL: http://archive.apache.org/dist/ant/binaries/ ③ apache-maven-2.2.1 URL: http://maven.apache.org/download.html ④ apache-t

  • To learn the use of maven to see a very practical introductory tutorial (rookie level entry) 2010-03-29

    I. Preface knew maven project management in java distinguished reputation, so the thought of learning to master, so access to a large number of documents. The authors found that these documents are the biggest names in java, most of them are all on t

  • [Change] Maven 2.0: Compile. Test. Deploy. Run 2010-03-30

    Abstract: Maven1.0 has experienced a few years, and as a substitute for Ant has been widely received by developers, but it did not very much to the developer's build.xml files from Ant freed. Maven1.0 slow and clumsy, and difficult to use up as much

  • Four-step work for you up to maven 2010-04-10

    maven build is not your ordinary Shuang, the following will see how to let maven to work. The first step, make preparations before installing maven, first install JDK5 or jdk6. This step is relatively simple, learning java students should be able, no

  • Java expert need to pay attention to the 25 learning objectives 2010-05-26

    Java expert need to pay attention to the 25 learning goals 1. You need to master the object-oriented analysis and design (OOA / OOD), involving patterns (GOF, J2EEDP) and the integrated model. You should be aware of UML, particularly class, object, i

  • cruisecontrol integration with maven 2 SVN 2010-07-05

    Recent project to be a scenic area, the project manager required to manage the project with cruisecontrol, a long learning process has begun. . . Prior to the cruisecontrol is blank. . First, install cruisecontrol: 1 to http://cruisecontrol.sourcefor