gpt4 book ai didi

eclipse - 在 Eclipse 中创建多模块 Maven 项目

转载 作者:行者123 更新时间:2023-12-02 04:15:08 25 4
gpt4 key购买 nike

试图在eclipse中创建一个多模块maven项目。

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

子模块 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>

子模块 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>

如果我在父文件夹的命令行上使用 mvn clean install 我可以构建查找。所有项目均已成功构建。

但是在 Eclipse 中,我不断收到子模块 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

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

我该怎么办?我被困住了。请帮忙。

我正在使用 Eclipse Galileo 和 m2Eclipse

最佳答案

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

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

在您的 child POM 1 中。您需要在 /project/parent/version 中对版本进行硬编码2.你不需要指定 <version> (你继承它) 3. 使用 ${project.version}依赖项中的属性。
<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>

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

也可以看看
  • Missing artifact error in Maven
  • Eliminate Maven POM Redundancy
  • Maven 3 - Worth it???
  • 关于eclipse - 在 Eclipse 中创建多模块 Maven 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209119/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com