【问题标题】:Confused that order matters when defining conditional profiles in maven在 Maven 中定义条件配置文件时,混淆了顺序很重要
【发布时间】:2012-02-23 13:46:10
【问题描述】:

我有一个可配置的属性 line.ending,我在项目构建的组装阶段使用它来指定我的应用程序属性文件的行尾类型。为此,我创建了两个配置文件 LF_DOS 和 LF_UNIX,以便在启动时:

mvn install 

或

mvn install -P LF_DOS

line.ending 等于“dos”,当我启动时:

mvn install -P LF_UNIX

line.ending 等于 'unix'。

我第一次尝试这样做很简单:

    <profile>
        <id>LF_UNIX</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <line.ending>unix</line.ending>
        </properties>
    </profile>
    <profile>
        <id>LF_DOS</id>
        <activation>
            <property>
                <name>!line.ending</name>
            </property>
        </activation>
        <properties>
            <line.ending>dos</line.ending>
        </properties>
    </profile>

不幸的是,无论是否设置了 LF_UNIX,这总是给我 line.ending=dos。奇怪...但是,更让我困惑的是,我只是通过更改配置文件声明顺序来解决问题,如下所示:

    <profile>
        <id>LF_DOS</id>
        <activation>
            <property>
                <name>!line.ending</name>
            </property>
        </activation>
        <properties>
            <line.ending>dos</line.ending>
        </properties>
    </profile>
    <profile>
        <id>LF_UNIX</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <line.ending>unix</line.ending>
        </properties>
    </profile>

这完全符合我的要求。

我的问题是:这是一个错误吗?或者是否需要了解 maven 配置文件,一种使配置文件订单声明在这种情况下特别重要的限制?

【问题讨论】:

    标签: maven maven-3 profiles


    【解决方案1】:

    困惑在于您对个人资料激活的工作原理的理解。

    你认为:

    <activation>
      <property>
        <name>!line.ending</name>
      </property>
    </activation>
    

    表示如果我没有设置名为“line.ending”的 maven 属性,请激活此配置文件。如果我没有在命令行上指定 -Dline.ending=X 真正意味着什么,请激活此配置文件。所以除非你运行这样的东西:

    mvn clean install -Dline.ending=unix
    

    您正在激活此配置文件,因此将值设置为 dos。

    【讨论】:

    • 谢谢!多亏了该文档,我终于得到了相同的解释:docs.codehaus.org/display/MAVENUSER/Profiles,其中有效地明确了激活属性/缺少属性仅适用于命令行(据我所知,开发人员中没有提到这一点Sonatype 的参考指南)...一旦您了解它与命令行有关,一切都会变得清晰。正要发布它,但你先做了:)。
    • 是的,我之前也遇到过同样的问题。我试图从 settings.xml 设置一个属性并使用它来触发不同的配置文件。它起作用了,但只是因为我碰巧在 settings.xml 和 profiles.xml (maven 2.2.1)中将配置文件命名为相同的事实,所以它同时激活了两者(顺便说一句,如果你命名一个配置文件,它会全面激活它们,包括如果您默认激活了一个,则任何带有该名称的东西现在都处于活动状态)。我终于发现激活仅适用于 -D 属性。然后这一切都说得通了。
    猜你喜欢
    • 2016-06-30
    • 2022-10-05
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2011-02-26
    • 2018-01-21
    • 2015-05-20
    • 1970-01-01
    相关资源
    最近更新 更多