【发布时间】: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