Automated build tools Maven

@

Brief introduction

Now we build a project need to use many third-party libraries, a number of projects often jar package as much as we can not imagine, and the complex relationship between a jar, a jar package will often reference other jar package, the lack of any project will result in a jar package fails to compile.
When development projects in the past, people often need to spend more energy on references Jar package to build the project environment, and this work is particularly difficult, because the relationship between the jar package is very complex, when certain jar package useless, and very from which it is difficult to remove.
The Maven is a build tool to help drive our projects, we just need to tell Maven what needs to jar package, it will help us to download all of the jar, greatly enhance the development efficiency.

Maven's core concepts

Well, since you see this article, I am going to think you have some knowledge of Maven and Maven have completed the configuration of download and installation environment variables. About Maven installation and configuration There are many online tutorials, I do not repeat things, we go directly to the topic. Take a look at the core concepts of Maven:

  • Conventions directory structure
  • POM
  • coordinate
  • rely
  • warehouse
  • The life cycle
  • inherit
  • polymerization

Well, what I would learn in depth from eight aspects to look at Maven.

1. agreed directory structure

For directory structures, Maven has its own rules, so we only need to know the Maven build directory structure and automatically generated effect.

2.POM

POM (Project Object Model), the project object model.
In the project's Maven build, pom.xml is the core configuration file, all settings associated with the build process are configured in this file. Important program equivalent to web.xml for Web projects.

3. coordinate

We coordinate analogy mathematics:
on a plane, using the X, Y two vectors can be any point on a plane uniquely positioned;
in space, using the X, Y, the Z can be any three vectors uniquely positioned in space one point.
In Maven then, using the following three vectors of any point of a repository can be uniquely positioned.

  • groupId: reverse domain name company or organization name + project
  • artifactId: project name
  • version: The version number

This is a narrowing range of positioning, in real life, you want to mail a courier, you have to write the provinces, and then write the city, and then write the county, the last of which is a specific building or a location. We look at an example:
Here Insert Picture Description
there is such a jar package in the warehouse, how do we locate it? We see a pom file in the folder, open look :( we intercepted a part)
Here Insert Picture Description
we look at key parts, then in Maven coordinates and paths warehouse project is actually the same, so we can spell a path through these three vectors:

commons-logging/commons-logging-api/1.1/commons-logging-api-1.1.jar

Jar file naming convention is atrifactId - version number. And we look in the repository directory, found the jar file in which the file path is true, this is a unified way to manage Maven jar package.

4. dependent

When Maven resolves dependencies information, it will be to find a local warehouse is dependent jar package.
Maven project for development of our own, using the install command to install after you can enter the warehouse.
Rely more important concept is dependent on a range, the range can be divided into three categories dependent on:

  • compile
  • test
  • provided

In the main program is unable to see the test program, but in a test program to see the main program, so the range is valid for the main program compile and test procedures, and participate in packaging; and test the range of the main program is invalid, the test program effective, is not involved in packaging; provided the scope is valid for the main program and testing procedures, but does not participate pack.

Here a brief mention of delivery dependent on: a dependency may be transmitted by other projects in the project, do not have to be transferred depends redeclaration project in each module, once at the bottom of the dependent project.
But the transitive dependencies are not absolute, such as non compile dependencies can not be delivered.
Conversely, dependent on having exclusive. When you do not want some of the jar package, you need to set the exclusion dependent manner:

<exclusions>
    <exclusion>
        <groupId></groupId>
        <artifactId></artifactId>
    </exclusion>
</exclusions>

This allows you to exclude jar package, but it can only take effect in the current project.
Dependencies Principle:

  • Verify the shortest path priority
  • Verify that the path is the same first declaration of priority

5. Warehouse

In Maven, the warehouse can be divided into two categories:

  • Local Warehouse: warehouse currently deployed on a computer directory, for all Maven projects on the current computer services
  • Remote repository (remote repository can also be divided into three categories):
      - PW: set up under the current LAN environment, for all Maven engineering services within the scope of the current local area network
      - central warehouse: erected on the Internet, for all the world Maven Engineering Services
      - central warehouse image: erected in every continent, flow balancing central repository, a central warehouse to reduce the pressure, while faster response to user requests

The content is stored in the repository Maven project can be divided into three categories:

  • Maven itself require plug-ins
  • Third-party frameworks or tools of the jar package
  • Develop their own Maven project

6. Life Cycle

Maven in the life cycle, in fact, the various aspects of the build order.
Build links:

  • [1] cleaning: will remove the old class bytecode files compiled previously obtained, compiled in preparation for the next time
  • [2] Compile: The source code is compiled into Java byte code class files
  • [3] test: automatic test, automatic call JUnit program
  • [4] Report: results of the test program execution
  • [5] packaging: Dynamic Web project to fight the war package, Java jar packaging engineering
  • [6] Installation: Maven in particular concept - packaging the obtained copy file to the specified location in the warehouse
  • [7] deployment: Copy dynamic Web project generated war package to a specified directory Servlet container to enable it to function properly

The build order can not be upset, it must execute the order.
Maven's core program defines the abstract statement cycle, the life cycle of specific tasks for each phase is to be completed by the plug.
Maven has three sets of independent life cycle, namely:

  1. Clean Lifecycle: some cleanup work before the actual building during
  2. Default Lifecycle: build the core part, compile, test, package, install, deploy, etc.
  3. Site Lifecycle: generate project reports, sites, publish site

They are independent of each other, you can call only clean to clean up the working directory, just call the site to build a site, of course, you can also run directly mvn clean install site to run all three sets of the life cycle. Do not do analysis on the specific content of each life cycle.
The Maven core program in order to better automate the build, execute all stages of the life cycle in accordance with this feature: no matter what stage of life now execute cycle, execution starts from the initial location of this life cycle.

7. Inheritance

Assume a scenario:
JUnit version Hello project dependencies: 4.0
Hello2 project dependencies JUnit Version: 4.0
Hello3 project dependencies JUnit Version: 4.9
since the test-dependent range is not transmitted, it is inevitably dispersed in each module, it is easy to produce inconsistent versions The problem.
Then we can be extracted into a unified version of JUnit dependent parent project, you can not specify versions when declaring rely on sub-project, the sub-projects will be the father works in a unified set of version will prevail, but also easy to modify. Note that if a project works as a parent, a packaging shall pom. Use parent label in the sub-project to declare the reference to the parent class.

8. polymerization

Action: each module a key installation works
configuration: configure each module participating in the polymerization in a total polymerization project.
Configured by the tag models. Then simply run the install command on the polymerization to a key installation engineering works of all modules.

Maven plug-in settings

Having said that earlier theories about Maven, not just their own right thirsty to see people certainly have confusing. But no way, we have to master the basics before you can even easier in practical use. Then the next we look at the actual operation Maven project, to eclipse here as an example. The first is to set Maven plugin: as long as not particularly old version of eclipse, which have been built Maven plugin, we only need to set it to.
Click Windows, and then select Preferences to open the Preferences window.
Here Insert Picture Description
Click Installations we set about Maven's path, eclipse will be built a Maven, but generally considered a built-in Maven is not good enough, not stable enough, so we click Add, and then find our own download Maven, can be added to it. Then User Setting Maven repository is used to specify the location, eclipse default will find yourself.
Here Insert Picture Description
Once set up, we create a version of Maven Java project in eclipse:
Right -> New-> Other-> find Maven Project-> Next
Here Insert Picture Description
the Java project there will be a default JDK version, if you want to directly modify the default JDK version, you can came Maven installation directory under the conf directory, open the settings.xml file, find the profiles tab, and then add on the inside:

<profile>
        <id>jdk-1.7</id>
        <activation>
            <activeByDefalut>true</activeByDefalut>
            <jdk>1.7</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
        </properties>
    <profile>

Then the next show you how to create a Web project:
Here Insert Picture Description
specific engineering to create what, in fact, is determined by the Packaging, if the election is the jar, then create a Java project; if the election is war, then create a Web project.
Here Insert Picture Description
So you can see that, Maven build a Web project did not web.xml file, an error led to the project, under the webapp nor anything, obviously this Web project is not complete, we have to solve this problem.
Right-click the project, and select Properties, open the configuration window:
Here Insert Picture Description
Click Projecy Facets, will be issued a new Dynamic Web Module is selected by default, we remove the hook, and then click Apply, and then again hook marked, then you will find more of the following an option, we click to open the window:
Here Insert Picture Description
in accordance with the above modifications, then click OK. Now that the project is not being given.
Here Insert Picture Description
However, when you create a new jsp file in webapp directory, the project has the error.
Here Insert Picture Description
This is because the project is missing from the Apache Tomcat runtime environment, how to solve it?
Open the pom.xml, we know, HttpServlet in the servlet-api jar package, then we can import the package by pom jar file, add the following dependency in pom.xml file:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Click Save Project not being given immediately. Then we run the program, the program runs correctly, and then came under the deployment directory, find lib directory is empty.
Here Insert Picture Description
This is because the range is dependent provided, if we rely on the range is set to compile, and then run to see results.
Here Insert Picture Description
This time jar package can be downloaded to the lib directory. This also explains the above said range-dependent relationship.
Now we come to the index.jsp file a written EL expression:
Here Insert Picture Description
'll find no prompt pageContext back there and request, because the request is serlvet-api package, and this jar package we have just introduced, so we only need to import the jar package to pageContext located, add a dependent under the pom file:

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1.3-b06</version>
    <scope>provided</scope>
</dependency>

Now prompts pageContext came out.
Here Insert Picture Description
Regarding the scope of dependency particular attention to, if you will range jsp-api is set to compile, run the project will produce a null pointer exception, the exception is clearly not our code generation, because we never even write any code. If the range is set to rely compile, we know that the jar package will be downloaded to the lib directory, and the need to rely on two jsp-api jar package, so it downloaded a total of three jar packages, and these packages also jar in Tomcat presence. At this time, these jar package will produce conflict, causing some errors can not understand. Therefore, we must rely on strict range.

Unified management relies version

If we are doing a project, start using version spring4.0, but later we need to jar package upgrade, version 5.0 will be upgraded, how to upgrade it? We know, spring framework dependent jar package is very large, one by one, to modify clearly unreasonable in the pom file. Then in the pom, we have a good configuration:
[1] using the internal properties label use custom labels unified version number is
[2] in position requires a unified version, using $ {name} references a custom label declaration version number

Common Maven command

Note: Execute the command associated with the Maven build process, you must enter the directory pom.xml file.
Commonly used commands:

  • [1] mvn clean: clean up
  • [2] mvn compile: Compile the main program
  • [3] mvn test-compile: test program compiler
  • [4] mvn test: a test is performed
  • [5] mvn package: Package
  • [6] mvn install: install
  • [7] mvn site: Site generated

However, these jar package dependency information about us may not be able to memorize, and in fact we do not need to remember, but when you need to search the Internet. Paste a query-dependent information website: https: What information dependent jar package //mvnrepository.com/ need, simply enter in the search box and search.
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/blizzawang/p/11411634.html