【问题标题】:Why isn't the child pom picking the right profile?为什么孩子 pom 没有选择正确的配置文件?
【发布时间】:2016-11-06 14:22:22
【问题描述】:

我已将这个项目精简为尽可能简单。我正在使用maven-echo-plugin1。这个项目所做的只是根据deluxe 配置文件是否处于活动状态来回显特定语句。如果我没有使用豪华配置文件,项目将打印出This is the base configuration。如果我使用的是豪华配置文件,项目会打印出This is the deluxe configuration

我有以下父 pom:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>name.weintraub</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>

    <profiles>
        <profile>
            <id>deluxe</id>
            <activation>
                <property>
                    <name>deluxe</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.soebes.maven.plugins</groupId>
                        <artifactId>maven-echo-plugin</artifactId>
                        <version>0.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>echo</goal>
                                </goals>
                                <phase>validate</phase>
                                <configuration>
                                    <echos>
                                        <echo>This is the deluxe configuration</echo>
                                    </echos>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
                <artifactId>maven-echo-plugin</artifactId>
                <version>0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <echos>
                                <echo>This is the base configuration</echo>
                            </echos>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

如果我跑:

$ mvn validate

这将打印出This is the base configuration

如果我跑:

$ mvn -Pdeluxe 验证

这将打印出This is the deluxe configuration

到目前为止一切顺利:

我现在创建一个子 pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <properties>
        <deluxe>true</deluxe>
    </properties>
    <parent>
        <groupId>name.weintraub</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>name.weintraub</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0</version>
</project>

请注意,我在 pom.xml 的开头将属性 deluxe 设置为 true

当我跑步时:

$ mvn validate

它打印出This is the base configuration

如果我这样运行:

mvn -Ddeluxe=true validate

然后打印This is the deluxe configuration。如果我这样做了

mvn -Pdeluxe validate

它打印出This is the deluxe configuration

所以,我可以看到子 pom 正在获取父配置文件,如果我直接在命令行上激活配置文件,或者使用属性。但是,即使我在 pom 本身中设置了属性,我的项目似乎也没有获取父母的个人资料。

为什么?


1 我使用的是 0.1 版,因为这是 Maven Central 中的最新版本,在 1.0 版中它被称为 maven-echo-plugin。其他方面的文档是一样的。

【问题讨论】:

  • 首先有一个较新版本的echo-maven-plugin,因为它必须被重命名...如果你在子节点中设置属性,父节点已经被读取和分析并且配置文件已经被激活还是不...
  • 顺便说一句:这个问题背后的想法是什么?您希望使用此个人资料解决什么样的问题?
  • @khmarbaise 是的,我知道该插件已重命名,但我在 Maven Central 中找不到更高版本,并认为旧版本足以满足此目的。
  • @khmarbaise 目的?我们使用公司 Pom 来设置我们想要执行的各种标准,并使我们的开发人员更容易。例如,公司 Pom 定义了所有站点报告。我们有一些项目需要较旧的源和目标 java 版本。如果我可以通过配置文件做到这一点,开发人员只需要在他们的 Pom 中设置属性,一切都会得到处理。但是,它不起作用,我不知道为什么。这是我不理解的 Maven 概念,还是我做的不对,或者这是一个错误?
  • @khmarbaise 这个 Pom 只是一个测试来了解配置文件的工作原理。这是我可以创建的最简单的项目,以了解发生了什么。

标签: maven maven-profiles


【解决方案1】:

只能使用系统属性(即命令行)/环境变量以及 JDK 和 OS 激活配置文件,而不是使用 pom 属性。 (请参阅Introduction to Build Profiles:“目前,此检测仅限于 JDK 版本的前缀匹配、系统属性的存在或系统属性的值。”)

所以,你的方法不能那样工作。您可能想看看非对称配置文件作为替代方案。

【讨论】:

  • 好吧愚蠢的问题:系统属性与属性到底是什么?当我在pom.xml 中设置一个属性时,这不是系统属性吗?
  • 系统属性是JVM提供的,比如user.home和直接在命令行定义的属性(通过-D)。 pom 中定义的属性只是简单/非系统属性。这种区别的一个原因在于,在模型构建的早期就选择了配置文件,此时属性尚不可用。模型插值(即稍后解析属性)。
  • 这似乎也不适用于文件激活。最后,我的父 pom 中的 maven-compiler-plugin 中的 &lt;source&gt;&lt;target&gt; 使用 ${javac.source}${javac.target},默认情况下定义为 1.7。我可以在子 pom 中设置这些值,它无需创建配置文件即可工作。我就是这样做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 2014-04-14
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多