[2] using maven and use maven pom file

First, the use maven:
First of all you need to know is that there is a word called in our development staff: convention over configuration .
For example, if we write the code has several options:
hard-coded: obj.setPath ( "d: / xxxx "), arranged in the form: conf.xml -> <path> d : / xxx </ path>, conventions form: use the default d: / xxx
maven use is the agreement form , it agreed to main directory to write the implementation of the project, test directory to write the test project, the maven will find in main when execution order was given, the test order was given in test in looking for.


agreed maven directory structure:
project
  --src
    --main # program code
      --java #java Code
      --resource # source code, configuration Diamante
    --test # test code
      --java
      --resource
  --pom.xml # project Object model, the document Object model dom remember it? dom is the html element as a node / object, and pom project is seen as a node / object.

 

mvn command needs to use the network.
Compile command:
mvn the compile # compile only main files in the directory
to perform test command:
mvn the Test If failures are the test fails, error is the code error
package command:
mvn Package Penalty for # only test package by order, packaged and it will actually automatic test again.
Installation command:
mvn install # will develop modules (labeled package), into a local warehouse for other modules (junior partner) using
Clear compilation:
mvn Clean # delete target directory, delete the directory compiled files.

 

These problems exist sequential instructions, such as if you perform test test command, you certainly need to be built, so the compile command will execute. Instruction sequence substantially as follows:

compile - > test -> package -> install

That is, if the install command, the preceding instruction will be executed.


These require some of the basic environment maven jar package, it will enter the first jar package basis whether there is a local library mvn seen, if not it will automatically go to the central warehouse download, so the first performance will be a little slower.
Note that, clean deleted is not a local warehouse jar package, but instead of clearing the install command into the local warehouse pack, but cleared compile bytecode files compiled from the command.
If you want to delete the item jar local warehouse, you will have to delete manually enter the local repository mvn by gav the project.

 

Two, pom file:

Use maven pom.xml file that describes the project model. html tags in the dom tree will be seen as a node, and pom tree sucked project seen as a node.

Common elements of the file is as follows:


① <properties> element :

<properties> element is used to define global attributes (by way of key-value pairs), defined role and acts as a variable, the specified value and which can be custom labels, and then applied through the EL expression syntax classes. By using this property can do it easy to modify and maintain the code, and improved readability. For example, I defined by the version number of the key property of this module to:
<Properties>
  <key1> VALUE1 </ key1> 
</ Properties>
<Version> VALUE1 $ {} </ Version>

 

② <dependencies> element:

<dependencies> element defines dependencies , it may comprise a plurality of dependency sub-elements, each dependency defines a dependency. Dependencies by gav, i.e. to locate the coordinates of which depend on the module.
<dependency>
  <the groupId> pers.tz.maven </ the groupId> # big project name
  <artifactId> HolleWorld </ artifactId> # submodule name
  <version> $ {value1} < / version> # version
  <scope> test < / scope> # depend on the scope and effectiveness, the default is the compile
</ dependency>
we already know, maven a key role in a project will be split into several modules, so large were responsible for locating the project in the local mvn library, and He was responsible for locating sub-module responsible for the total project, the version number is positioned in many versions iteration.

 

③ <dependencyManagement> element:

Dependency defines the parent project , its child elements are the elements <dependencies>.

 

④ <parent> element:

The project which is defined inherited from the project , the relative path to the parent pom project file to locate the project through its parent pom file gav and coordinate the project. Example:
<parent>
  <the groupId> pers.tz.maven </ the groupId>
  <the artifactId> hollemaven </ the artifactId>
  <Version> the SNAPSHOT-0.0.1 </ Version>
  <relativePath The> ../ hollemaven / the pom.xml </ relativePath>
</ parent>
If a parent works gav coordinates and coordinate the project gav same section, you can delete that part, which is the maven conventions.

 

⑤ <packaging> element:

Project objectives specified packaging type , java project labeled jar package, web engineering package labeled as war, the parent project labeled pom package.
<packaging> jar </ packaging>

 

⑥ <profiles> element:

We previously used eclipsebuild path jdk modified version, or right project, property, project factors modify jdk version.
Now we can use profiles elements pom file to modify the environment. Code is as follows:
<Profiles>
  <Profile>
    <- name easily play -!>
    <The above mentioned id> jdk17 </ the above mentioned id>
    <Activation>
      <- - activate!>
      <ActiveByDefault> to true </ activeByDefault>
      <-! version number ->
      <the JDK> 1.7 </ the JDK>
    </ Activation>
    <the 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>
  <

The more elements of the role I do not know -.- I do use it to modify the jdk environment

Guess you like

Origin www.cnblogs.com/twz123/p/11517869.html