Maven父POM变量未解析

4
我将为你翻译下面的内容:

我有以下的POM结构:

./foobar-common/pom.xml ./abc-window/pom.xml ./abc-datasource/pom.xml

两个 abc-window 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>hu.abc.ringcore</groupId>
    <artifactId>abc-window</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>abc-window</name>

    <parent>
        <relativePath>../foobar-common/pom.xml</relativePath>
        <groupId>hu.abc.ringcore</groupId>
        <artifactId>foobar-common</artifactId>
        <version>1.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>hu.abc.ringcore</groupId>
            <artifactId>abc-datasource</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

abc-datasource的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>hu.abc.ringcore</groupId>
<artifactId>abc-datasource</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>abc-datasource</name>

<parent>
    <relativePath>../foobar-common/pom.xml</relativePath>
    <groupId>hu.abc.ringcore</groupId>
    <artifactId>foobar-common</artifactId>
    <version>1.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
    </dependency>
    <dependency>
        <groupId>hu.abc.ringcore</groupId>
        <artifactId>abc-base</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>hu.abc.ringcore</groupId>
        <artifactId>abc-event</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>hu.abc.ringcore</groupId>
        <artifactId>abc-variable</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>hu.abc.ringcore</groupId>
        <artifactId>abc-variableintf</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>hu.foobar.ring</groupId>
        <artifactId>RateSorszamIntf</artifactId>
        <version>${ringcore-services.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
    </plugins>
</build>

foobar-common pom.xml 的内容如下:

<project>
    <modelVersion>4.0.0</modelVersion>
    <name>Maven Default Project</name>
    <groupId>hu.foobar.ringcore</groupId>
    <artifactId>foobar-common</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.10</junit.version>
        <spring.version>3.1.2.RELEASE</spring.version>
        <spring-security.version>2.0.6.RELEASE</spring-security.version>
        <commons-logging.version>1.1.1</commons-logging.version>
        <dom4j.version>1.6.1</dom4j.version>
        <javaee-api.version>6.0</javaee-api.version>
        <mycila-event.version>3.0</mycila-event.version>
        <ringcore-services.version>1.3.4-SNAPSHOT</ringcore-services.version>
        <commons-lang3.version>3.1</commons-lang3.version>
    </properties>

</project>

我的问题是,如果在abc-datasource中执行mvn install,则依赖项版本将从父POM中的属性中提取,这是预期的。但是,如果我从引用abc-datasourceabc-window中执行mvn install,则无法解析版本,出现以下错误:
[ERROR] Failed to execute goal on project abc-window: Could not resolve dependencies for project hu.abc.ringcore:abc-window:jar:1.0-SNAPSHOT: Failed to collect dependencies for [junit:junit:jar:4.10 (test), hu.abc.ringcore:abc-datasource:jar:1.0-SNAPSHOT (compile)]: Failed to read artifact descriptor for org.apache.commons:commons-lang3:jar:${commons-lang3.version}: Could not transfer artifact org.apache.commons:commons-lang3:pom:${commons-lang3.version} from/to central (http://repo.maven.apache.org/maven2): IllegalArgumentException: Illegal character in path at index 70: http://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/${commons-lang3.version}/commons-lang3-${commons-lang3.version}.pom

这两个项目都有相同的父级pom,为什么它们不能正常工作呢?

2个回答

5
在父POM中使用dependencyManagment
<dependencyManagement>
  <dependencies>
    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>${junit.version}</version>
    </dependency>
    <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>${commons-lang3.version}</version>
    </dependency>
    </dependencies>
  </dependencyManagement>  

在父POM中添加所有版本,并在子模块中使用不带版本的依赖项。
/pom.xml                 <-parent pom
/abc-window/pom.xml     <-child module  
/abc-datasource/pom.xml <-child module  

父POM应该包含

<project  
...  
   <properties>   
    ... 
   </properties>  
   <modules>  
      <module>abc-datasource</module>
      <module>abc-window</module>  
   </modules>   
   <build>
      <pluginManagment>   
       ...
      </pluginManagment>
   </build>
   <dependencyManagment>
    ...  
   </dependencyManagment>  
</project>

好的,我会尝试,但您能解释一下我的方法有什么问题吗?原始问题是这样的,因为我想学习。 - jabal
1
@jabal,你的项目结构有些奇怪。Maven 项目结构应该是这样的:请参见更新。 - Ilya
谢谢,我会在解决主要问题后更新结构。 - jabal
1
@jabal,我已经检索到你的POM文件,并且构建成功。 - Ilya

1

abc-window 的父级被列为:

    <parent>
            <relativePath>../foobar-common/pom.xml</relativePath>
            <groupId>hu.abc.ringcore</groupId>
            <artifactId>foobar-common</artifactId>
            <version>1.0</version>
        </parent>

abc-datasource中的父级被列为:

<parent>
    <relativePath>../foobar-common/pom.xml</relativePath>
    <groupId>hu.ratesoft.ringcore</groupId>
    <artifactId>foobar-common</artifactId>
    <version>1.0</version>
</parent>

所以,它们没有相同的 groupId。


1
这只是一个笔误,当我将公司名称更改为abc时发生了。这肯定不是问题的原因。 - jabal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接