6. Maven study notes

  1. About Maven
    Maven is an open source project under the Apache, project management tools, management Java project.
    1.1. Project Object Model (Project Object Model)
    POM object model, each project has a maven pom.xml file, define the project depends jar package, coordinates this project, packaging operation mode.
    1.2. Dependency management system (core basis)
    maven by the coordinates of the project depends jar package standardized management
    1.3. Lifecycle maven define a project
    to clean up, initialization, compile, test, report, package, deploy, Site Builder
    1.4 . maven project life cycle management process is based on plug-in completed
    . 1.5 a set of standards
    emphasize: maven project has its own standard working directory structure, the definition of standard coordinates
  2. Maven repository
    2.1. Central warehouse
    containing daily development of the common jar package, a professional team (maven team) to maintain unity.
    Central warehouse address: HTTP: //repo1.maven.org/maven2/
    . 2.2 local repository
    serves as a cache, the project will be the first time to download jar package from a remote repository (the Internet), there will be a local warehouse jar package, the second No, start with a local warehouse to find a remote repository to download from, if not find the will to find a remote repository.
    2.3. PW
  3. Maven environment to build
    3.1 Maven download.
    Http://maven.apache.org/download.cgi
    3.2 Configuring local repository.
    Open maven installation directory of the conf / setting.xml files, configuration D: \ apache-maven-3.5.4 \ local_repository
  4. eclipse (IntelliJ IDEA) to configure maven
    4.1 configuration installation directory.
    Window -> Preferences -> Maven -> Installations -> add the Add maven installation directory location
    . 4.2 Configuration File
    Window -> Preferences -> Maven - -> User Settings -> Browse to add profile path
    4.3 shows the Maven Repositories.
    Window -> show View -> Other -> Maven -> Maven Repositories
    . 4.4 index rebuild Maven Repositories
    select Local Repository right -> rebuild Index
  5. Creating a Maven project
    . 5.1 Creating a Maven Project -> Check Create a simple project (skip archetype selection ) -> Fill Project coordinates -> Finish
    . 5.2 includes engineering coordinates
    Group Id: Name of the project, the name of the project to reverse domain name such as: cn.xiaofan.mavendemo
    Arficatc Id: module name (subproject name)
    version: version of the module, snapshot (snapshots, no official release) release (the official release)
  6. Set the compiler version
    . 6.1 Maven project default is JDK1.5, you need to modify the compiled version
    6.2 configuration information:.



    Org.apache.maven.plugins
    Maven-plugin-Compiler
    3.1

    1.8
    1.8




    . 6.3 of the project Alt + F5 to refresh
  7. Add dependence
    7.1 Wizard mode (local warehouse there is a corresponding Jar package).
    Right-engineering selected Maven -> Add Dependency -> Search for a dependent (provided that the local warehouse rebuild the index, otherwise it can not automatically query)
    7.2 by Nexus. Repository Manager in the search, and then add to pom.xml file
    link: https: //repository.sonatype.org
  8. Project Life Cycle (understand)
    Maven has three sets of independent life cycle, namely: clean, default, site. Each activity corresponds with the life cycle of a maven command.
    8.1 clean cycle.
    Pre-clean: ready to clean
    clean: a true clean-up
    post-clean: do some follow-up work to clean up after
    8.2 default cycle.
    ...
    the compile: compile the source code
    ...
    the Test: unit tests run the test code
    package: a good compiler the code is packaged into jar or war or ear etc
    ...
    install: installed to the local maven repository
    ...
    8.3 site cycle.
    pre-site: ready to generate site
    site: build a site and document
    post-site: site closeout
    site-deploy: generated site is published to the server
  9. eclipse in command
    compile: Right-click the project -> run as -> Maven build ... -> Goals Enter the compile -> RUN
    Clean: Right-click the project -> RUN AS -> Maven Clean
    the Test: execution unit test class execution classes under src / the test / the Java
    Package Penalty for: right-click the project -> run as -> Maven build ... -> enter the package Goals in -> RUN
    install: the project to publish a local warehouse jar click project right -> run as -> Maven install
  10. maven native command
    mvn command name not run debugging
    mvnDebug command names commissioning
    premise: maven need to configure the environment variables
    MAVEN_HOME D: \ the Apache-maven-3.5.4
    the PATH% MAVEN_HOME% \ bin
  11. About No compiler is provided when Maven project build in this environment processing
    link: https: //blog.csdn.net/lslk9898/article/details/73836745

12 Creating SSH2 engineering (*****)
12.1. Creating war project
12.1.1. On the basis of creating a normal maven project on the packaging type of election war, and web.xml need to manually add (create WEB-INF, in which the following Creating the web.xml)
12.1.2. adding compiled plug-in, set the compiler version, refer to 6
12.1.3. adding tomcat plug-ins, and configure the port path


org.apache.tomcat.maven
tomcat7-maven-plugin
2.2

8080
/

. 12.1.4 add a jsp files, configure the web.xml
12.1.5 running a web server and client access.
Click Project Right -> run as -> Maven build ... -> Goals enter tomcat7: run - -> RUN
12.1.6 configuration servlet-api in pom.xml.
because we are creating a war project, which requires response and request and other objects, and these objects are in the servlet-api in, so only add servlet-api, to function properly compiled by

the javax.servlet
servlet-api
2.5
provided

Note: because tomcat plugin itself will provide servlet-api plug-ins, so you want to add this provided depend on the scope, packaged to prevent conflicts
12.1.7 fight the war package (see 9).
12.2 Add dependence. definition of variables, to extract the appropriate version


4.0.0

cn.xiaofan.mavendemo
parent
0.0.1-SNAPSHOT

b

c in pom.xml


4.0.0
cn.xiaofan.mavendemo
c
0.0.1-SNAPSHOT

  1. Construction of multi-module Maven project (*****)
    Maven multi-module project, using some relatively large projects, through reasonable split module, to achieve code reuse, ease of maintenance and management. In particular, some open source framework, but also by way of a multi-module, there is provided an integrated plug-in, the user can configure the module top as needed.
    14.1 Split rule
    14.1.1 splits on the service module (ie, enterprise resource planning ERP (Enterprise Resource Planning)).
    Parent Project -> General configuration
    basic information module
    Invoicing module
    Personnel Management Module
    Financial Management Module
    CRM Module
    . 14.1.2 layer was divided by
    the parent project -> common configuration
    web tier
    business logic
    data access layer
    entity class
    Note: 1. for comparison large items can be considered both a collection of
    2. for the parent module, general packaging types selected pom, general packaging type selection sub-module JAR, WAR
    14.2. dependencies between modules transitive
    14.3. profile split, only needs to be configured in web.xml applicationContext * can be found!
    . 14.4 tomcat7: run run
    14.5 pack package (running, package are selected parent project).
  2. Dependency management
    15.1 Depending range
    dependent relationship range is used to control the three kinds of dependence and CLASSPATH (compiler classpath, test classpath, run CLASSPATH) of

Dependent range for testing the compiler classpath classpath effective effective effective runtime classpath example
the compile-YYY Spring Core
Test - the Y - JUnit
Provided YY - the servlet API-
Runtime - YY the JDBC Driver
Note:

  1. compile compile, test, run, A-dependent B at compile time, and is also dependent on the test and runtime. strus-core, spring-beans hit war package bag or jar

  2. test is valid only in the testing phase, it does not hit or war package inside the jar package junit

  3. provided compile, and test effective, A when compiling and testing needs B. For example: servlet-api is useful to compile and test without (tomcat container has been provided) at runtime will not hit the war package

  4. Test run is valid. For example jdbc driver package
    15.2 transferred dependent rules
    directly dependent on (A dependent B) \ transfer dependent (B dependent C) the compile Provided Runtime Test
    the compile the compile - Runtime -
    Provided Provided Provided Provided -
    Runtime Runtime - Runtime -
    Test Test - Test -
    NOTE: a dependent ordinate represents B, B depends abscissa represents C, a and C on behalf of other dependencies, - indicates no dependencies.
    15.3 Mediation principle relies
    A A -.> B -> C (V1)
    B A -.> D -> E -> C (V2)
    C A -.> D -> C (V3 )
    the first principle: path first, short path will be dependent on (a, b, two pathways, a will ultimately depend C (v1) version)
    the second principle: declaration of priority, under the premise that the depth of the same path, the front declaration will be referenced C (v1)

  5. Locking version
    dependencyManagement role in Maven is the equivalent one pair jar package depends manage version control
    pom.xml file, jar version of the judgment of two ways

  6. If the dependencies in the dependency he did not declare version elements, then maven you will not have to go inside the dependencyManagement artifactId and groupId been declared, and if so, to set it, if not on the error, you must tell one of dependency declaration version

  7. If the dependencies of dependency declared version, regardless of the version is incorrect denpendencyManagement declared the jar, have to denpendency in the subject.

  8. Dependent negative (version is high low)

    org.apache.struts
    Struts2-Core
    $ {} struts.version


    Javassist
    Javassist


  9. Test unit
    18.1 Depending JUnit

    JUnit
    JUnit
    4.11
    Test

    18.2. Write test classes
    public class TestErpDao {

    @Test
    public void testErpDao() {
    System.out.println(“测试Junit”);
    }
    }

Published 85 original articles · won praise 12 · views 3733

Guess you like

Origin blog.csdn.net/fanjianhai/article/details/103800226