maven核心思想“约定大于配置”

1. 除了从maven的几个phase可以看出。

2. 寻找parent pom的“约定”

http://maven.apache.org/guides/introduction/introduction-to-the-pom.html


Scenario 1

The Scenario

As an example, let us reuse our previous artifact, com.mycompany.app:my-app:1. And let us introduce another artifact, com.mycompany.app:my-module:1.


  4.0.0
  com.mycompany.app
  my-module
  1

And let us specify their directory structure as the following:

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml

Note: my-module/pom.xml is the POM of com.mycompany.app:my-module:1 while pom.xml is the POM of com.mycompany.app:my-app:1

The Solution

Now, if we were to turn com.mycompany.app:my-app:1 into a parent artifact of com.mycompany.app:my-module:1,we will have to modify com.mycompany.app:my-module:1's POM to the following configuration:

com.mycompany.app:my-module:1's POM


  
    com.mycompany.app
    my-app
    1
  
  4.0.0
  com.mycompany.app
  my-module
  1

Notice that we now have an added section, the parent section. This section allows us to specify which artifact is the parent of our POM. And we do so by specifying the fully qualified artifact name of the parent POM. With this setup, our module can now inherit some of the properties of our parent POM.

Alternatively, if we want the groupId and / or the version of your modules to be the same as their parents, you can remove the groupId and / or the version identity of your module in its POM.


  
    com.mycompany.app
    my-app
    1
  
  4.0.0
  my-module

This allows the module to inherit the groupId and / or the version of its parent POM.


2. 

However, that would work if the parent project was already installed in our local repository or was in that specific directory structure (parent pom.xml is one directory higher than that of the module's pom.xml).

But what if the parent is not yet installed and if the directory structure is

.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml
The Solution

To address this directory structure (or any other directory structure), we would have to add the  element to our parent section.


  
    com.mycompany.app
    my-app
    1
    ../parent/pom.xml
  
  4.0.0
  my-module

As the name suggests, it's the relative path from the module's pom.xml to the parent's pom.xml.

3.

The Scenario

Given the previous original artifact POMs, and directory structure,

com.mycompany.app:my-app:1's POM


  4.0.0
  com.mycompany.app
  my-app
  1

com.mycompany.app:my-module:1's POM


  4.0.0
  com.mycompany.app
  my-module
  1

directory structure

.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml
The Solution

If we are to aggregate my-module into my-app, we would only have to modify my-app.


  4.0.0
  com.mycompany.app
  my-app
  1
  pom

  
    my-module
  

In the revised com.mycompany.app:my-app:1, the packaging section and the modules sections were added. For the packaging, its value was set to "pom", and for the modules section, we have the element my-module. The value of  is the relative path from the com.mycompany.app:my-app:1 to com.mycompany.app:my-module:1's POM (by practice, we use the module's artifactId as the module directory's name).

Now, whenever a Maven command processes com.mycompany.app:my-app:1, that same Maven command would be ran against com.mycompany.app:my-module:1 as well. Furthermore, some commands (goals specifically) handle project aggregation differently.


4.

Example 4

The Scenario

But what if we change the directory structure to the following:

.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml

How would the parent pom specify its modules?

The Solution

The answer? - the same way as Example 3, by specifying the path to the module.


  4.0.0
  com.mycompany.app
  my-app
  1
  pom

  
    ../my-module
  
5. 

Example 5

The Scenario

Given the previous original artifact POMs again,

com.mycompany.app:my-app:1's POM


  4.0.0
  com.mycompany.app
  my-app
  1

com.mycompany.app:my-module:1's POM


  4.0.0
  com.mycompany.app
  my-module
  1

and this directory structure

.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml
The Solution

To do both project inheritance and aggregation, you only have to apply all three rules.

com.mycompany.app:my-app:1's POM


  4.0.0
  com.mycompany.app
  my-app
  1
  pom

  
    ../my-module
  

com.mycompany.app:my-module:1's POM


  
    com.mycompany.app
    my-app
    1
    ../parent/pom.xml
  
  4.0.0
  my-module

NOTE: Profile inheritance the same inheritance strategy as used for the POM itself.



你可能感兴趣的:(maven)