使用 Maven 的多模块项目未按预期工作?

发布于 2024-09-24 17:03:38 字数 1029 浏览 7 评论 0原文

你好 我正在尝试使用 m2eclipse 在 eclipse 中创建一个多模块项目。我遵循了一些教程,但它的工作方式不是我所期望的:

这是我的项目的结构,

  -Root
  -    webapps
  -          module1
  -          module2

我有用于根和模块的 pom.xml 。 (模块1和模块2相互独立) 在pom.xml(根)中,我

 <modules>
        <module>./webapps/module1</module>
        <module>./webapps/module2</module>
 </modules>

在module1的pom.xml中:

<parent>
        <groupId>{RootGroupId}</groupId>
        <artifactId>{RootArtifactId}</artifactId>
        <version>{RootVersionId}</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>

在module2中,它与模块1类似。

当我转到根并运行pom文件时,它将首先触发根的阶段,然后触发模块的阶段(构建根项目并构建模块项目)。对我来说这很好。

但是当我转到 module1 并运行 pom.xml 时,问题发生了。然后它也做同样的事情:触发Root pom.xml和module1的pom.xml。我不喜欢这个。 我想要发生的事情是仅触发 module1 的 pom 文件(仅构建 module1),root 的 pom 不会被触发(根项目未构建)。

请提供任何帮助。

Hi
I am trying to create a multi-modules project in eclipse with m2eclipse. I followed some tutorials but the way it work is not what i expect:

Here is the structure of my project

  -Root
  -    webapps
  -          module1
  -          module2

I have pom.xml for Root and modules. (module 1 and 2 are independent to each other)
In the pom.xml (Root), i have

 <modules>
        <module>./webapps/module1</module>
        <module>./webapps/module2</module>
 </modules>

In module1's pom.xml:

<parent>
        <groupId>{RootGroupId}</groupId>
        <artifactId>{RootArtifactId}</artifactId>
        <version>{RootVersionId}</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>

In module2, it is similar to module 1.

When I go to Root and run the pom file, it will trigger the Root's phases first and the module's phases later (build the root project and also build the module projects). To me it is fine.

But the problem happens when i go to module1 and run the pom.xml. Then it also so do the same: trigger the Root pom.xml and module1's pom.xml. i dont like this. What i want to be happened is ONLY the module1's pom file is triggered (ONLY module1 is built), root's pom will not be triggered (Root project is not be built).

Any help, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-10-01 17:03:38

更新:如果您不希望在从声明它的 POM 继承的 POM 中应用插件配置,请将 inherited 设置为

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <inherited>false</inherited> <!-- THIS SHOULD DO IT -->
    <executions>
      <execution>
        <id>read-project-properties</id>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
        <configuration>
          <files>
            <file>build.properties</file>
          </files>
        </configuration>
      </execution>
    </executions>
  </plugin>

参考


我尝试重现问题...但没有成功。我创建了一个类似的项目结构:

$ tree .
.
├── pom.xml
└── webapps
    ├── module1
    │   ├── pom.xml
    │   └── src
    │       └── main
    │           └── webapp
    │               ├── index.jsp
    │               └── WEB-INF
    │                   └── web.xml
    └── module2
        ├── pom.xml
        └── src
            └── main
                ├── resources
                └── webapp
                    ├── index.jsp
                    └── WEB-INF
                        └── web.xml

父 pom.xml 声明:

  <modules>
    <module>webapps/module1</module>
    <module>webapps/module2</module>
  </modules>

每个子项:

  <parent>
    <artifactId>Q3790987</artifactId>
    <groupId>com.stackoverflow</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>

从根构建会触发反应器构建:

$ mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] Q3790987
[INFO] module1 Maven Webapp
[INFO] module2 Maven Webapp
[INFO] 
[INFO] ------------------------------------------------------------------------
...

但构建子项不会触发父项上的任何内容:

$ cd webapps/module1/
$ mvn install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building module1 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...

一切都按我的预期工作。


(最初的答案被删除,因为我似乎误解了这个问题)

Update: If you don't want a plugin configuration to be applied in POMs which inherit from the POM where it is declared, set inherited to false.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <inherited>false</inherited> <!-- THIS SHOULD DO IT -->
    <executions>
      <execution>
        <id>read-project-properties</id>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
        <configuration>
          <files>
            <file>build.properties</file>
          </files>
        </configuration>
      </execution>
    </executions>
  </plugin>

Reference


I tried to reproduce the problem... but didn't succeed. I created a similar project structure:

$ tree .
.
├── pom.xml
└── webapps
    ├── module1
    │   ├── pom.xml
    │   └── src
    │       └── main
    │           └── webapp
    │               ├── index.jsp
    │               └── WEB-INF
    │                   └── web.xml
    └── module2
        ├── pom.xml
        └── src
            └── main
                ├── resources
                └── webapp
                    ├── index.jsp
                    └── WEB-INF
                        └── web.xml

Where the parent pom.xml declares:

  <modules>
    <module>webapps/module1</module>
    <module>webapps/module2</module>
  </modules>

And each child:

  <parent>
    <artifactId>Q3790987</artifactId>
    <groupId>com.stackoverflow</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>

Building from the root triggers a reactor build:

$ mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] Q3790987
[INFO] module1 Maven Webapp
[INFO] module2 Maven Webapp
[INFO] 
[INFO] ------------------------------------------------------------------------
...

But building a child doesn't trigger anything on the parent:

$ cd webapps/module1/
$ mvn install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building module1 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...

Everything works as expected for me.


(initial answer removed as it appeared I misinterpreted the question)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文