在Eclipse中创建多模块Maven项目 [英] Creating multi module Maven Projects in Eclipse

查看:171
本文介绍了在Eclipse中创建多模块Maven项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



父Pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"> 
< modelVersion> 4.0.0< / modelVersion>
< groupId>父< / groupId>
< artifactId> proj1< / artifactId>
< version> $ {myversion}< / version>
< packaging> pom< / packaging>
<属性>
< myversion> 1.0.0< / myversion>
< / properties>
< modules>
< module> child1< / module>
< module> child2< / module>
< / modules>
< / project>

子模块1 Pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema -instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"> 
< modelVersion> 4.0.0< / modelVersion>
< parent>
< artifactId> proj1< / artifactId>
< groupId>父< / groupId>
< version> $ {myversion}< / version>
< / parent>
< groupId>父< / groupId>
< artifactId> child1< / artifactId>
< version> $ {myversion}< / version>
< / project>

子模块2 Pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"> 
< modelVersion> 4.0.0< / modelVersion>
< parent>
< artifactId> proj1< / artifactId>
< groupId>父< / groupId>
< version> $ {myversion}< / version>
< / parent>
< groupId>父< / groupId>
< artifactId> child2< / artifactId>
< version> $ {myversion}< / version>
<依赖关系>
<依赖关系>
< groupId>父< / groupId>
< artifactId> child1< / artifactId>
< version> $ {myversion}< / version>
< type> jar< / type>
< scope> compile< / scope>
< / dependency>
< / dependencies>
< / project>

如果我在父文件夹中使用mvn clean install命令行,我可以构建find。所有项目都成功建成。



但是在eclipse中,我继续收到Child Module 2的错误pom.xml

 描述资源路径位置类型
父项:child1:1.0.0:jar缺少:
----------
1)parent:proj1:pom: $ {myversion}
----------
1缺少需要的工件。

为工件:
父:proj1:pom:$ {myversion}

从指定的远程存储库:
中央(http:// repo1 .maven.org / maven2,releases = true,snapshots = false)
pom.xml / child2行1 Maven问题

我需要实现2个东西
1.有一个maven属性定义父pom文件中的版本。
2.使用此属性定义所有子模块中的版本。



我该怎么办?我被卡住了请帮助。



我正在使用Eclipse Galileo与m2Eclipse

解决方案

这无法工作,您无法从父级获得父级的版本(实际上,属性不会在父元素中替换,请参阅 MNG-624 )。所以你需要对版本进行硬编码,你的父POM应该是:

 < project xmlns =http:// maven .apache.org / POM / 4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0 .0 http://maven.apache.org/maven-v4_0_0.xsd\"> 
< modelVersion> 4.0.0< / modelVersion>
< groupId>父< / groupId>
< artifactId> proj1< / artifactId>
< version> 1.0.0< / version>
< packaging> pom< / packaging>
< modules>
< module> child1< / module>
< module> child2< / module>
< / modules>
< / project>

在您的孩子POM 1.您需要在 /项目/父/版本 2.您不需要指定< version> (您继承它)3.使用 $ {project.version} 依赖关系中的属性。

 < project xmlns = http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache .org / POM / 4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\"> 
< modelVersion> 4.0.0< / modelVersion>
< parent>
< artifactId> proj1< / artifactId>
< groupId>父< / groupId>
< version> 1.0.0< / version>
< / parent>
< groupId>父< / groupId>
< artifactId> child2< / artifactId>
<! - version> $ {myversion}< / version - > <! - 继承 - >
<依赖关系>
<依赖关系>
< groupId>父< / groupId>
< artifactId> child1< / artifactId>
< version> $ {project.version}< / version> <! - 在此使用内置属性 - >
<! - type> jar< / type - > <! - 这是默认值,您可以省略它 - >
<! - scope> compile< / scope - > <! - 这是默认值,您可以省略它 - >
< / dependency>
< / dependencies>
< / project>

Maven 3.1中将支持无版本的父元素。



另见




Trying to create a multi module maven project in eclipse.

Parent Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>proj1</artifactId>
  <version>${myversion}</version>
  <packaging>pom</packaging>
  <properties>
   <myversion>1.0.0</myversion>
  </properties>
  <modules>
   <module>child1</module>
   <module>child2</module>
  </modules>
</project>

Child-Module 1 Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>${myversion}</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child1</artifactId>
  <version>${myversion}</version>
</project>

Child Module 2 Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>${myversion}</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child2</artifactId>
  <version>${myversion}</version>
  <dependencies>
   <dependency>
    <groupId>parent</groupId>
    <artifactId>child1</artifactId>
    <version>${myversion}</version>
    <type>jar</type>
    <scope>compile</scope>
   </dependency>
  </dependencies>
</project>

If I use mvn clean install on command line from the parent folder I can build find. All the projects are build successfully.

But in eclipse I keep getting an error for Child Module 2 pom.xml

Description Resource Path Location Type
parent:child1:1.0.0:jar Missing:
----------
1) parent:proj1:pom:${myversion}
----------
1 required artifact is missing.

for artifact: 
  parent:proj1:pom:${myversion}

from the specified remote repositories:
  central (http://repo1.maven.org/maven2, releases=true, snapshots=false)
 pom.xml /child2 line 1 Maven Problem

I need to achieve 2 things 1. have a maven property define the version in the parent pom file. 2. use this property to define the version in all the child modules.

What should I do? I am stuck. Please help.

I am using Eclipse Galileo with m2Eclipse

解决方案

This can't work, you can't get the version of the parent to use from the parent (and actually, properties don't get substituted in the parent element, see MNG-624). So you need to hard code the version and your parent POM should be:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>proj1</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <modules>
   <module>child1</module>
   <module>child2</module>
  </modules>
</project>

And in your child POM 1. you need to hardcode the version in /project/parent/version 2. you don't need to specify the <version> (you inherit it) 3. use the ${project.version} property in dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>1.0.0</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child2</artifactId>
  <!--version>${myversion}</version--> <!-- Inherited -->
  <dependencies>
   <dependency>
    <groupId>parent</groupId>
    <artifactId>child1</artifactId>
    <version>${project.version}</version> <!-- use the built-in property here -->
    <!--type>jar</type--> <!-- This is a default, you can omit it -->
    <!--scope>compile</scope--> <!-- This is a default, you can omit it -->
   </dependency>
  </dependencies>
</project>

Versionless parent elements will be supported in Maven 3.1.

See also

这篇关于在Eclipse中创建多模块Maven项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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