是否可以使用 Maven 属性 来激活基于文件的配置文件?

Is it possible to use a maven property to activate a profile based upon a file?

当 JACOB dll 不在我的本地存储库中时,我想下载它们。

因此,我有这两个配置文件

    <profile>
        <id>use-jacob-dll</id>
        <activation>
            <file>
                <exists>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</exists>
            </file>
        </activation>
        <dependencies>
            <dependency>
                <groupId>${jacob.groupId}</groupId>
                <artifactId>jacob</artifactId>
                <type>dll</type>
                <classifier>x64</classifier>
                <version>${jacob.version}</version>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>download-jacob-dll</id>
        <activation>
            <file>
                <missing>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
            </file>
        </activation>

但是,即使 download-jacob-dll 已经完成了它的目标,对 mvn help:active-profiles 的调用表明以下内容

The following profiles are active:

 - tests-for-eclipse (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)
 - download-jacob-dll (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)

我怀疑是因为我在激活时使用了 ${settings.localRepository} 属性。

问题:是不是失败的原因?如果是这样,我如何才能仅在缺少依赖项时激活我的配置文件?

Is it possible to use a maven property to activate a profile based upon a file?

没有,如 Maven documentation on profiles

所述

Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.

然而,从 POM documentation 我们也得到

a given filename may activate the profile by the existence of a file, or if it is missing. NOTE: interpolation for this element is limited to ${basedir}, System properties and request properties.

因此,确实没有 Maven 属性 除了 ${basedir} 是允许的。


And if so, how can I activate my profile only when dependency is missing?

依赖项或相关文件的硬编码路径将是一个解决方案,即使不像您所指的解决方案那样可移植。

或者,您可以使用上面文档中提到的 request 属性,因此需要使用 属性 配置激活,然后必须从命令行传递(更便携但也更脆弱):

<activation>
    <file>
        <missing>${path}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
    </file>
</activation>

然后如下调用maven:

mvn clean install -Dpath=path_to_local_rep

上述解决方案在某些情况下可能是合理的,例如 Jenkins 作业。