在 Maven 中定义条件配置文件时,对顺序很重要感到困惑

发布于 2025-01-08 05:40:32 字数 1978 浏览 3 评论 0原文

我有一个可配置属性line.ending,我在项目构建的组装阶段使用它来指定应用程序属性文件的行结束类型。为此,我创建了两个配置文件 LF_DOSLF_UNIX,这样当我启动 :

mvn install 

mvn install -P LF_DOS

line.ending 时等于“dos”,并且当我launch :

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 配置文件,这是一种使配置文件顺序声明在这种情况下特别重要的限制?

I have a configurable property line.ending that I used during the assembly phase of the building of my project to specify the line ending type of my application property files. For that I have created two profiles LF_DOS and LF_UNIX, so that when I launch :

mvn install 

or

mvn install -P LF_DOS

line.ending equals 'dos', and when I launch :

mvn install -P LF_UNIX

line.ending equals 'unix'.

My first attempt to do this was simply :

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

Unfortunately, this always gave me line.ending=dos, whatever LF_UNIX is set or not. Weird... But, the more confusing to me, is that I solved the problem just by changing the profile declaration order, like this :

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

This works exactly like I want.

My questions is : is this a bug ? Or is it something to know about maven profiles, a kind of limitation that makes profiles order declaration particularly matter in such a case ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2025-01-15 05:40:32

困惑在于您对配置文件激活如何工作的理解。

您认为这:

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

意味着如果我没有设置名为“line.ending”的 Maven 属性,请激活此配置文件。如果我没有在命令行上指定 -Dline.ending=X,则激活此配置文件的真正含义是什么。因此,除非您运行这样的命令:

mvn clean install -Dline.ending=unix

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

The confusion lies in your understanding of how profile activation works.

You think that this:

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

means if I don't have a maven property named "line.ending" set, activate this profile. What it really means if I didn't specify -Dline.ending=X on the command line, activate this profile. So unless you run something like this:

mvn clean install -Dline.ending=unix

You are activating this profile and thus having the value set to dos.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文