This article is followed by a previous piece of learning notes for continuing to record Maven: [url] http://jackycheng2007.iteye.com/blog/923791
[/Url]
Magic Weapon 2. Pom project object model-continue
Project inheritance-Project inheritance
As mentioned above, the project is regarded as an object in maven. In this case, there must be an inherited function. This function is useful, so that our project profile can be reused. Imagine if you have many similar projects, such as a series of projects developed using SSH, there must be a lot of duplicates in the POM of each project. With inheritance, you can extract all the public parts and then inherit each sub-project to achieve reuse. Originally, inheritance reflects the relationship between is-a. The subprojects under a supper project are inherently related to is-a with the supper project. This is not an abuse of inheritance, haha.
How can we inherit it?
Next, the last example in the previous article, if there is a sub-module that needs to inherit from it:
Java code
- <Project>
- <Parent>
- <Groupid> com. mycompany. app </groupid>
- <Artifactid> my-app </artifactid>
- <Version> 1 </version>
- </Parent>
- <Modelversion> 4.0.0 </modelversion>
- <Artifactid> my-module </artifactid>
- </Project>
This sub-module inherits the groupid and version information of the parent module. Wait. Here is a problem. Just rely on
<Groupid> com. mycompany. app </groupid>. How can I find the POM er POM in the submodule?
Yes, if the supper project has been installed in the local repository or the POM er Pom is in the agreed directory, that is, the sub-module's upper-level directory (as follows), you can.
Reference my-app | -- my-Module
| '-- Pom. xml
'-- Pom. xml
So, unfortunately, none of these two are true? For example:
Reference.
| -- My-Module
| '-- Pom. xml
'-- Parent
'-- Pom. xml
You can also specify the location:
Java code
- <Project>
- <Parent>
- <Groupid> com. mycompany. app </groupid>
- <Artifactid> my-app </artifactid>
- <Version> 1 </version>
- <Relativepath>.../parent/POM. xml </relativepath>
- </Parent>
- <Modelversion> 4.0.0 </modelversion>
- <Artifactid> my-module </artifactid>
- </Project>
Project aggregation-project set
Through project inheritance, we can use the POM of the parent project when managing or building sub-projects. In turn? If we want to build a complete project, do we often need to assemble multiple sub-projects for packaging. Project aggregation solves this problem.
How can this problem be solved? The following two conditions must be met:
The packaging value in the POM of the parent project is set to "pom ".
Customize the sub-project directory in the POM of the Parent Project
If we want to integrate my-module into my-app, make the following changes:
Java code
- <Project>
- <Modelversion> 4.0.0 </modelversion>
- <Groupid> com. mycompany. app </groupid>
- <Artifactid> my-app </artifactid>
- <Version> 1 </version>
- <Packaging> pom </packaging>
- <Modules>
- <Module> my-module </module>
- </Modules>
- </Project>
There is no write path above, because the maven directory convention is followed. If not, write:
Java code
- <Project>
- <Modelversion> 4.0.0 </modelversion>
- <Groupid> com. mycompany. app </groupid>
- <Artifactid> my-app </artifactid>
- <Version> 1 </version>
- <Packaging> pom </packaging>
- <Modules>
- <Module> ../My-module </module>
- </Modules>
- </Project>
It can be seen that conventions are a good way to save code and form consensus easily.
Project interpolation and variables-use variables
In pom, you may use the same string segment in multiple places, such as the path. Meven supports variable declarations and references, so that you can prevent hard code.
How to reference variables? Take a look at the following:
Java code
- <Version >$ {project. Version} </version>
The $ symbol is enclosed in braces and accessed hierarchically by vertices. Very convenient. If there is an inheritance relationship, the subclass value is used.
What are the available variables?
Project model variables
Any single-value element in POM can be referenced as a variable, for example:
Reference $ {project. groupid}, $ {project. Version}, $ {project. Build. sourcedirectory}
Special Variables
$ {Basedir}: directory of the current project
$ {Project. baseuri}: URI. Since Maven 2.1.0, directory of the current project
$ {Maven. Build. timestamp}, Project Build start time. Since Maven 2.1.0-M1
Properties
You can also reference the properties defined in POM as variables.
Java code
- <Project>
- ...
- <Properties>
- <Mavenversion> 2.1 </mavenversion>
- </Properties>
- <Dependencies>
- <Dependency>
- <Groupid> org. Apache. MAVEN </groupid>
- <Artifactid> Maven-artifact </artifactid>
- <Version >$ {mavenversion} </version>
- </Dependency>
- <Dependency>
- <Groupid> org. Apache. MAVEN </groupid>
- <Artifactid> Maven-project </artifactid>
- <Version >$ {mavenversion} </version>
- </Dependency>
- </Dependencies>
- ...
- </Project>
Through properties, you can customize some variables. Haha.