Elements modules, parent, properties, and import in Maven Pom.xml

Source: Internet
Author: User

Objective

MAVEN is used in the project, and the content used is not as simple as using maven/eclipse to build SSM (spring+spring mvc+mybatis) The core of Maven is pom.xml, so I'll talk about the different places.

What impresses me most is the following four elements: modules, parent, properties, import.

Modules

In the literal sense, module is And Pom.xml in the modules is precisely this meaning, used to manage the various modules in the same project; If MAVEN is simpler to use, or if the project's modules are not partitioned in pom.xml, then this element is not available, but generally larger items are used.

  1. Demand Scenarios

If our project is divided into several modules, how many modules do we need to build several times (to execute the MVN command in the directory of each module)? Of course, you build one-by-one, but you have to build it in such a troublesome way, so the simple thing is to use aggregations to build all the modules at once.

  2. Concrete implementation

A. Now that you are using aggregations, you need an aggregation vector to create a common MAVEN project Account-aggregator, such as:

  

Because it is a polymer, only responsible for aggregating other modules, then only need the above directory, the deletion is deleted; Note that the Pom file is written (in red):

 <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/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId> Com.youzhibing.account</groupid> <artifactId>account-aggregator</artifactId> <version> 1.0.0-snapshot</version> <packaging>pom  </packaging> < Name>account aggrregator</name> <url>http:// maven.apache.org</url>  <MODULES> 
<!--modules are written here---& Lt;module>account-register</module> <module>account-persist</module> </modules> </project>

B. Create a submodule account-register, account-persist: Right-click on Account-aggregator,new-----and MAVEN, select Maven module and create the Moven module.

C. After the creation is complete, the project structure is as follows, then the account-aggregator can shrink up at this time, we operate the concrete sub-module just fine.

D. Note that when we open the Pom file of the submodule of the package structure, we find that there is more content than expected, so we can sit down and deal with it.

E. After the coding is over, we just need to build account-aggregator, all of the submodule will be built.

Parent

inheritance, equivalent to inheritance in Java, is the function of reuse

  1. Demand Scenarios

If each submodule is using spring, is it necessary for each submodule to have a separate spring dependency? It is possible to do so, but we have better practices, that is, inheritance, implemented with parent.

  2. Concrete implementation

A. Configuring the parent Pom.xml

I use the aggregate pom as the parent pom to configure the public dependency of the submodule.

Parent (Account-aggregator) Pom.xml:

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.youzhibing.account</groupId> <artifactid>account-aggregator </artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name >account aggrregator</name> <url>http://maven.apache.org</url>    <modules> <!--modules are written here--<module>account-register</module> <module>acco      Unt-persist</module> </modules> <dependencies> <!--configuration Common dependency-<!--spring-dependent <dependency> <groupId>org.springframework</groupId> <artifactid>spring-core</ artifactid> <version>4.0.2.RELEASE</version> </dependency> <dependency> & Lt;groupid>org.springframework</groupid> <artifactId>spring-beans</artifactId> <vers ion>4.0.2.release</version> </dependency> <dependency> <groupid>org.springframewor K</groupid> <artifactId>spring-context</artifactId> <version>4.0.2.release</versi on> </dependency> <dependency> <groupId>org.springframework</groupId> <a Rtifactid>spring-contExt-support</artifactid> <version>4.0.2.RELEASE</version> </dependency> <!-- JUnit Dependent-<dependency> <groupId>junit</groupId> <artifactid>junit</artifa ctid> <version>4.7</version> <scope>test</scope> </dependency> </depende Ncies></project>
View Code

B.account-register's Pom.xml:

<?xml version= "1.0"? ><project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" > <modelVersion>4.0.0</modelVersion> <parent>    <groupId>com.youzhibing.account</groupId> <artifactId>account-aggregator</artifactId> <version>1.0.0-SNAPSHOT</version> <relativepath&gt ... /pom.xml</relativepath> <!--with no configuration, the default is to find the parent directory Pom.xml--</parent> <artifactId> Account-register</artifactid> <name>account-register</name> <url>http://maven.apache.org</url><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </ Properties> <dependencies> <!--Configure your own unique dependencies--<dependency> <groupid>javax.mail </groupId> <artifactId>mail</artifactId> <version>1.4.3</version> &lt ;/dependency> <dependency> <groupId>com.icegreen</groupId> &LT;ARTIFACTID&GT;GREENMAIL&L t;/artifactid> <version>1.4.1</version> <scope>test</scope> </dependency> & Lt;/dependencies></project>
View Code

C.account-persist's Pom.xml:

<?xml version= "1.0"? ><project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" > <modelVersion>4.0.0</modelVersion> <parent>    <groupId>com.youzhibing.account</groupId> <artifactId>account-aggregator</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactid>account-persist</artifactid > <name>account-persist</name> <url>http://maven.apache.org</url><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </ Properties> <dependencies> <!--Configure your own unique dependencies--<dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.0.2.release</vers ion> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifact id>druid</artifactid> <version>1.0.16</version> </dependency> </dependencies>& Lt;/project>
View Code

D. The dependent jar package all OK, need to do is in each module code development!

  3. Dependency Management

Inheritance can eliminate repetition, so is there no problem? The answer is that there is a problem, assuming that in the future need to add a new Submodule Account-util, the module just provides some simple help tools, do not need to rely on spring, JUnit, then inherit the dependency on, there is no way? Yes, Maven has already thought for us, that is the dependencymanagement element, not only can the child module inherit to the parent module's dependency configuration, but also can guarantee the child module to rely on the flexibility of use. A dependency declaration under the Dependencymanagement element does not introduce a real dependency, but it can constrain dependency usage under dependencies.

Configuring the Dependencymanagement element in the parent Pom.xml

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.youzhibing.account</groupId> <artifactid>account-aggregator </artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name >account aggrregator</name> <url>http://MAVEN.APACHE.ORG</URL>      <modules> <!--modules are written here--<module>account-register</module> <module>acco unt-persist</module> </modules> <dependencyManagement> <dependencies> <!--configuration shared dependency--&G      T <!--spring Dependent-<dependency> <groupId>org.springframework</groupId> <arti Factid>spring-core</artifactid> <version>4.0.2.RELEASE</version> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <artifactid>spring-beans</arti factid> <version>4.0.2.RELEASE</version> </dependency> <dependency> <gro Upid>org.springframework</groupid> <artifactId>spring-context</artifactId> <version& gt;4.0.2.release</version> </dependency> <dependency> <groupid>org.springframework&lt   ;/groupid>     <artifactId>spring-context-support</artifactId> <version>4.0.2.RELEASE</version> &L      T;/dependency> <!--junit Dependency--<dependency> <groupId>junit</groupId>    <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement></project>
View Code

Account-persist's Pom.xml (Account-register also):

<?xml version= "1.0"? ><project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" > <modelVersion>4.0.0</modelVersion> <parent>    <groupId>com.youzhibing.account</groupId> <artifactId>account-aggregator</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactid>account-persist</artifactid > <name>account-persist</name> <url>http://maven.apache.org</url>  <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </ Properties> <dependencies> <!--spring Dependency--<dependency> <groupid>org.sprin gframework</groupid> <artifactId>spring-core</artifactId> </dependency> <depende     Ncy> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> &LT;ARTIFAC tid>spring-context</artifactid> </dependency> <dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-context-support</artifactId> </dependency> & lt;! --JUnit Dependent-<dependency> <groupId>junit</groupId> <artifactid>junit</arti Factid> </dependency>    <dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc</ artifactid> <version>4.0.2.RELEASE</version> </dependency> <dependency> &lt ;groupid>com.alibaba</groupid> <artifactId>druid</artifactId> <version>1.0.16< /version> </dependency> </dependencies></project>
View Code

Using this dependency management mechanism does not seem to reduce too much of the POM configuration, there is less version (JUnit has less scope), it does not feel useful, in fact, the role is very large, the parent pom use dependencymanagement can unify the project scope of the dependent version , when a dependent version is declared in the parent Pom, the submodule does not have to declare the version when it uses dependencies, and it does not occur when multiple sub-modules use version inconsistencies, helping to reduce the chance of dependency collisions. If a submodule does not declare a dependent use, it does not produce any effect even if the dependency is declared in the dependencymanagement in the parent POM.

Import

Import is only effective under the dependencymanagement element, which is to import and merge the Dependencymanagement configuration in the target pom into the dependencymanagement element of the current POM. The following is an introduction to the Dependencymanagement configuration in Account-aggregator and merging it into the current POM.

<dependencyManagement>      <dependencies>        <dependency>            <groupId> com.youzhibing.account</groupid>              <artifactId>account-aggregator</artifactId>              < version>1.0.0-snapshot</version>            <type>pom</type>              <scope>Import </scope>        </dependency>      </dependencies>  </dependencyManagement>

Properties (Maven properties)

With the <properties> element the user can customize one or more Maven attributes, and then refer to the property in the same way that the ${property name} is used elsewhere in the POM, the most important thing to do is to eliminate duplication and unify management.

There are 6 types of properties in Maven, built-in properties, POM attributes, custom attributes, settings properties, Java System Properties, and environment variable properties;

  1. Built-in properties

Two common built-in properties ${basedir} indicates the project and directory, which is the directory containing the Pom.xml file; ${version} indicates the project version

  2.POM Properties

You can use this class property to reference the value of the corresponding element in the Pom file. As ${project.artifactid} corresponds to the value of the <project> <artifactId> element, the commonly used POM properties are:

${project.build.sourcedirectory}: Project's main source directory, default to src/main/java/

${project.build.testsourcedirectory}: Project's test source directory, default to src/test/java/

${project.build.directory}: Project build output directory, default to target/

${project.outputdirectory}: Project main code compiles output directory, default is target/classes/

${project.testoutputdirectory}: Project test main code output directory, default to Target/testclasses/

${PROJECT.GROUPID}: Project's GroupId

${project.artifactid}: Project's Artifactid

${project.version}: Version of the project, equivalent to ${version}

${project.build.finalname}: Project package output file name, default is ${project.artifactid}-${project.version}

  3. Custom Attributes

The following Account-aggregator Pom.xml, then the submodule that inherits this pom.xml can also use this custom attribute

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.youzhibing.account</groupId> <artifactid>account-aggregator </artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name >account aggrregator</name> <url>http://maven.apache.org</url>    <modules> <!--modules are written here--<module>account-register</module> <module>acco      unt-persist</module> <module>account-another</module> </modules> <properties> <!--define the spring version number--<spring.version>4.0.2.RELEASE</spring.version> <junit.version>4.7<      /junit.version> </properties> <dependencyManagement> <dependencies> <!--configuration is dependent-- <!--spring Dependent-<dependency> <groupId>org.springframework</groupId> <a       Rtifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-BEANS&L        t;/artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.spRingframework</groupid> <artifactId>spring-context</artifactId> <version>${spring.ve rsion}</version> </dependency> <dependency> <groupid>org.springframework</groupid&        Gt <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> &lt ;/dependency> <!--junit dependent-<dependency> <groupId>junit</groupId> & Lt;artifactid>junit</artifactid> <version>${junit.version}</version> <scope>test</ scope> </dependency> </dependencies> </dependencyManagement></project>
View Code

  4.Settings Properties

In the same vein as the POM attribute, users use settings. The Starting property references the value of the XML element in the Settings.xml file.

  5.Java System Properties

All Java System properties can be referenced with the Maven attribute, such as ${user.home} pointing to the user directory.

  6. Environment variable Properties

All environment variable properties can be used with Env. The Maven attribute reference, such as ${env, begins. Java_home} refers to the value of the JAVA_HOME environment variable.

The relationship between aggregation and inheritance

1. Aggregation is mainly to facilitate the rapid construction of projects, inheritance is mainly to eliminate duplication of configuration;

2. For an aggregation module, it knows which modules are aggregated, but those that are aggregated do not know the existence of the aggregation module, and for an inherited parent Pom, it does not know which submodules inherit it, but those submodules must know what their parent Pom is;

3. Aggregate Pom and the packaging of the parent pom in succession must be pom, and the parent module in the aggregation module and inheritance has no actual content except the POM

Conclusion

Maven more and more popular, this aspect of information is more and more, "maven combat" gave me the feeling is quite good, the content of this blog is mostly taken from it, online information is more and more, on the blog park there are many;

The last point of emphasis: It is good to see, practice better, write blog record it is the best!

Elements modules, parent, properties, and import in Maven Pom.xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.