Maven:添加对父pom项目的引用 [英] Maven: adding a reference to a parent pom project

查看:202
本文介绍了Maven:添加对父pom项目的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否理解如何正确使用父项目项目。 X-45454545.1454545 X- 20045 X-4545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X-
< groupId> com.example< / groupId>
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< packaging> pom< / packaging>

< modules>
< module> ../ child1< / module>
< module> ../ child2< / module>
< / modules> 200新X- 200 200 200 X- 200 200 X- 200 200 X- 200 200:20045 X- ):

 < parent> 
< groupId> com.example< / groupId>
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< relativePath> ../ Parent / pom.xml< / relativePath>
< / parent>

< modelVersion> 4.0.0< / modelVersion>
< artifactId> child1< / artifactId>

此设置工作正常,并在eclipse(m2eclipse)中正确解析。我可以将这些部署到我的本地存储库,最后使用以下结构,这应该是正确的:

   -  com $ b新评新新新新新旗新新新新旗新新旗旗新新旗新新旗旗新新旗人次:200 200 200新新新新旗新新旗新新旗新新旗200新新新新旗旗新1992新新新旗新新旗200新新新新旗200 200新新新旗200 Chan X- -0.0.1-SNAPSHOT 
--child1-0.0.1-SNAPSHOT.jar
--child1-0.0.1-SNAPSHOT.pom
--child2
--0.0 .1-SNAPSHOT
--child2-0.0.1-SNAPSHOT.jar
--child2-0.0.1-SNAPSHOT.pom
 <依赖关系> 
<依赖关系>
< groupId> com.example< / groupId>
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> pom< / type>
< / dependency>
< / dependencies>

当这样做时,项目在eclipse中没有显示任何错误,但没有工件被添加到我的类路径中:不新新旗新新新旗新新旗旗新新旗新新旗旗新新旗旗新新旗旗新新旗旗新新旗旗新新旗旗新新旗旗新新旗旗新新新旗200新新新旗200新新新旗200新新新旗200新新新旗200新新新旗200 200旗新新旗200新新新旗200新新新旗200新新新旗200新新新旗200新新新旗200新新新旗200新新新旗200新新200 200 200 200 CEol,只能引用其他罐子,然后可以在某个地方引用主人,但是我不知道这是如何实现的。

解决方案

   -  com 
- 示例
--parent
--0.0.1-SNAPSHOT
--parent-0.0.1-SNAPSHOT.pom
--child1
--0.0.1 -SNAPSHOT
--child1-0.0.1-SNAPSHOT.jar
--child1-0.0.1-SNAPSHOT.pom
--child2
--0.0.1-SNAPSHOT
--child2-0.0.1-SNAPSHOT.jar
--child2-0.0.1-SNAPSHOT.pom

我建议以下列方式:

  -  parent(pom.xml)
+ - child1
+ - child2

可以将父进入主控或版本控制(Git,SVN)。除了所有的孩子都必须这样引用父母:

 < modelVersion> 4.0。 0℃; / modelVersion> 

< parent>
< groupId> com.example< / groupId>
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< / parent>

< artifactId> child1< / artifactId>

在您的父母中,您可以使用这样的东西:

 < groupId> com.example< / groupId> 
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< packaging> pom< / packaging>

< modules>
< module> child1< / module>
< module> child2< / module>
< / modules>

此外,您可以使用公司pom,只需通过向项目父项添加父参考:

 < parent> 
< groupId> com.example< / groupId>
< artifactId> master-pom< / artifactId>
< version> 0.0.1< / version>
< / parent>

< groupId> com.example< / groupId>
< artifactId>父< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< packaging> pom< / packaging>

< modules>
< module> child1< / module>
< module> child2< / module>
< / modules>

在公司pom中,您可以(并且应该)固定不同的插件版本,并定义公司默认和建议如果你喜欢拥有一个超级pom的公司,你可以简单地创建一个单独的maven项目,它只包含包含你的配置(如plugins,dependencies)的pom文件等等,当然也可以将它检查为版本控制(Git,SVN等)。与其他项目分开。



如果要引用一个jar文件,换句话说就是您的多模块构建的工件,您必须使用正确的groupId, artifactId和版本。如果您想在其他项目中使用child1,则需要给出以下内容:

 依赖> 
<依赖关系>
< groupId> com.example< / groupId>
< artifactId> child1< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< / dependency>
< / dependencies>

重要的是要了解多模块构建的父代不是通常的工件使用的原因是基于包装类型 pom 它不会真正创建一个工件(jar文件)。



更新:
您可以仅限于使用范围 import ,但这只适用于depdencyManagement而不是依赖关系。
另一个解决方案可能是创建一个单独的虚拟项目,它将依赖关系也是两个子节点作为传递deps。但是这不是真正的美丽。



所以解决方案只是直接添加您在项目中使用的两个或多个依赖项,就是这样。


I'm not sure if I'm understanding how to use a parent pom project correctly. I have the following parent pom defined:

<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>../child1</module>
    <module>../child2</module>
</modules>

And then the children pom reference the parent (each child has their own set of dependencies which are not shown):

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../Parent/pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>child1</artifactId>

This setup works fine and resolves correctly in eclipse (m2eclipse). I can deploy these to my local repository and end up with the following structure, which should be correct:

--com
   --example
      --parent
        --0.0.1-SNAPSHOT
          --parent-0.0.1-SNAPSHOT.pom
      --child1
        --0.0.1-SNAPSHOT
          --child1-0.0.1-SNAPSHOT.jar
          --child1-0.0.1-SNAPSHOT.pom
      --child2
        --0.0.1-SNAPSHOT
          --child2-0.0.1-SNAPSHOT.jar
          --child2-0.0.1-SNAPSHOT.pom

My issue is that I now want to reference the parent project in a different project (not parent, child1, or child2) and thus pull in all of the parent's children. I can add a reference to it in my other project:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>

When doing this the project shows no errors in eclipse but no artifacts are being added to my classpath: not child1, child2, or any of their dependencies.

I keep thinking there must be a way to have a "master" pom project which isn't a jar in itself but has only references to other jars and then be able to reference that "master" somewhere, but I cannot find out how this is accomplished.

解决方案

The first thing i would suggest to change your structure of the projects.

--com
   --example
      --parent
        --0.0.1-SNAPSHOT
          --parent-0.0.1-SNAPSHOT.pom
      --child1
        --0.0.1-SNAPSHOT
          --child1-0.0.1-SNAPSHOT.jar
          --child1-0.0.1-SNAPSHOT.pom
      --child2
        --0.0.1-SNAPSHOT
          --child2-0.0.1-SNAPSHOT.jar
          --child2-0.0.1-SNAPSHOT.pom

I would suggest to go the following way:

  --parent (pom.xml)
      +--child1
      +--child2

The parent can be checked in into a version control (Git, SVN) either into the master or into the trunk. Apart from that all childs have to reference the parent like this:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>child1</artifactId>

and in your parent you can use things like this:

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

Furthermore you can use a company pom just simply by adding a parent reference to your project parent:

<parent>
    <groupId>com.example</groupId>
    <artifactId>master-pom</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

In the company pom you can (and should) pin different plugin versions and define company defaults and suggestions for dependencies with their appropriate versions.

If you like having a company super-pom you simple create a separate maven project which contains only pom file which contains your configurations like plugins, dependencies etc. and of course check it into a version control (Git, SVN etc.). separately from the other project.

If you want to reference a jar file which means in other words an artifact of your multi-module build you have to use the correct groupId, artifactId and version. If you like to use child1 in an other project you need to give the following:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>child1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

It is important to understand that the parent of a multi module build is not a an artifact which is usually used, cause based on the packaging type pom it does not really create an artifact (jar file).

Update: You can do that in a limited way only for the dependencyManagement area with the scope import but this is only intended for using in the depdencyManagement and not for dependencies. An other solution might be to create a separate dummy project which has the dependencies too two childs as transitive deps. But this is not really beauty.

So the solution is simply to add the two or more dependencies directly which you are using in you project and that's it.

这篇关于Maven:添加对父pom项目的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆