首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >依赖层次结构没有使用父pom中的属性吗?

依赖层次结构没有使用父pom中的属性吗?
EN

Stack Overflow用户
提问于 2013-03-27 19:47:21
回答 1查看 4.2K关注 0票数 1

我有一个问题,不知道是否有人能帮我解决。我在我的项目中有一个模块结构,其中我使用Maven解析依赖项。对于这种结构,我使用分类器来区分具有不同内容的版本。对于每个分类器,我在父pom中定义了一个配置文件,并在属性中使用该分类器的字符串。通过这种方式,在我的模块中,我使用了这个属性,并且是我定义的配置文件,它决定了分类器常量。我现在遇到的问题是,当依赖项继承自我在某个模块的pom中定义的依赖项时,依赖项层次结构无法识别分类器。例如,如果我有项目A,B和C,B依赖于A,C依赖于B,从C我使用分类器得到B,而不是A。如果我使用父pom中的属性,就会发生这种情况。如果我直接使用常量字符串,则可以正确地捕获依赖关系。我看到的唯一解决方案是在每个pom上使用配置文件来定义它们内部的依赖关系。但是我有5个个人资料!没有其他方法来解决这个问题了吗?我使用带有m2e插件的STS3.8作为我的集成开发环境。

提前谢谢你!

我加上了poms

父pom:

代码语言:javascript
复制
<profiles>
    <profile>
        <id>TRUNK</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <svnBranch />
        </properties>
    </profile>
    <profile>
        <id>MON</id>
        <properties>
            <svnBranch>MON</svnBranch>
        </properties>
    </profile>
    <profile>
        <id>LOLA</id>
        <properties>
            <svnBranch>LOLA</svnBranch>
        </properties>
    </profile>
    <profile>
        <id>NBA</id>
        <properties>
            <svnBranch>NBA</svnBranch>
        </properties>
    </profile>
    <profile>
        <id>TEST</id>
        <properties>
            <svnBranch>TEST</svnBranch>
        </properties>
    </profile>
    <profile>
        <id>PROD</id>
        <properties>
            <svnBranch>PROD</svnBranch>
        </properties>
    </profile>
</profiles> 

项目A:

代码语言:javascript
复制
<parent>
    <groupId>com.myproject</groupId>
    <artifactId>pom</artifactId>
    <version>1.0.10</version>
</parent>

<artifactId>core-services</artifactId>
<version>1.1.0.41-SNAPSHOT</version>

项目B:

代码语言:javascript
复制
<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>pom</artifactId>
    <version>1.0.10</version>
</parent>

<artifactId>olb-services</artifactId>
<version>1.1.0.41-SNAPSHOT</version>

<properties>
    <module.core-services.dependency.version>1.1.0.41-SNAPSHOT</module.core-services.dependency.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>core-services</artifactId>
        <version>${module.core-services.dependency.version}</version>
        <classifier>${svnBranch}</classifier>
    </dependency>
</dependencies>

项目C:

代码语言:javascript
复制
<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>pom</artifactId>
    <version>1.0.10</version>
</parent>

<artifactId>nba-services</artifactId>
<version>1.1.0.41-SNAPSHOT</version>

<properties>
    <module.olb-services.dependency.version>1.1.0.41-SNAPSHOT</module.olb-services.dependency.version>
    <module.core-services.dependency.version>1.1.0.41-SNAPSHOT</module.core-services.dependency.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>olb-services</artifactId>
        <version>${module.olb-services.dependency.version}</version>
        <classifier>${svnBranch}</classifier>
    </dependency>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>core-services</artifactId>
        <version>${module.core-services.dependency.version}</version>
        <classifier>${svnBranch}</classifier>
    </dependency>
</dependencies>

在每个依赖项的分类器标记中使用${svnBranch}不起作用。在项目B中,当被项目C引用时,属性${svnBranch}是空的,但它来自父pom。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-28 19:31:21

在maven中,如果您只能在构建时通过传递-D{activation.property}=value或-P{profile.id/s}来激活在父级别定义的配置文件,则只能在子pom中使用该配置文件。

您不能在您的父母中定义配置文件,并尝试在您的子pom中激活它,因为配置文件不能被继承(根据您的示例,您甚至没有尝试在您的子pom中激活)。

换句话说,除非配置文件在缺省情况下被激活,否则maven不知道它(在您的情况下,您可能想要在缺省情况下激活所有内容,但请记住,在默认情况下,当时只能激活一个配置文件)

你的问题是,来自主干的${svnBranch}只存在于你的子pom中,没有任何价值,因此maven只作用于GAV,而不是分类器。为了证明这一点,请检查您孩子的有效pom (mvn help: effective -pom)。您还可以检查哪些配置文件处于活动状态,哪些不处于活动状态(mvn帮助:所有配置文件)。

对于你正在做的事情,我不认为使用配置文件是最好的方法。一种更好/更简单的方法可能是只在父级的普通属性中声明分支名称。

代码语言:javascript
复制
<properties>
 <svnBranch.lola>LOLA</svnBranch.lola>
 <svnBranch.nba>NBA</svnBranch.nba>
</properties>

然后你的孩子可以根据需要使用。

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>olb-services</artifactId>
        <version>${module.olb-services.dependency.version}</version>
        <classifier>${svnBranch.lola}</classifier>
    </dependency>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>core-services</artifactId>
        <version>${module.core-services.dependency.version}</version>
        <classifier>${svnBranch.nba}</classifier>
    </dependency>
</dependencies>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15658040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档