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.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

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

<project>
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

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.

<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 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 <relativePath> element to our parent section.

<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>

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

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

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

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

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.

<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>

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 <module>my-module</module>. The value of <module> 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.

<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>
5. 

Example 5
The Scenario

Given the previous original artifact POMs again,

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

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

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

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

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

<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>

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

<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>

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



Maven是一个项目管理工具,它可以帮助我们自动导入和配置jar包,使得在JavaWeb开发中更加方便。\[1\]使用Maven的好处是可以遵循约定大于配置核心思想,简化项目的配置过程。 在使用IDEA进行Maven安装和配置时,建议使用自己下载的Maven而不是IDEA自带的Maven。因为IDEA自带的Maven可能会随着IDEA的升级而升级,导致一些意料之外的问题。\[2\]以下是在IDEA中安装和配置Maven的步骤: 1. 打开IDEA,点击File -> Settings。 2. 在弹出的窗口中,选择左侧的Build, Execution, Deployment -> Build Tools -> Maven。 3. 在右侧的Maven设置中,找到Maven home directory,选择你自己下载的Maven的安装路径。 4. 勾选Override选项,然后在User settings files中选择Maven安装路径下的config文件夹中的settings.xml文件。 5. 在Local repository中选择你自己创建的本地仓库路径,可以在Maven安装路径下创建一个文件夹作为本地仓库。\[3\] 完成以上步骤后,你就成功安装和配置Maven。现在你可以在IDEA中使用Maven来管理你的项目了。 #### 引用[.reference_title] - *1* *2* *3* [IDEA maven的安装与配置(超详细)](https://blog.youkuaiyun.com/m0_56183421/article/details/125123817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值