Maven配置文件中使用变量定义属性

3

我正在学习MAVEN配置文件,有一个关于使用变量设置属性的问题。目前,我正在使用以下配置:

<profile>
  <id>Action type D restriction</id>
  <activation>
    <property>
      <name>actionType</name>
      <value>D</value>
    </property>
  </activation>
  <properties>
    <restriction.actionType>D</restriction.actionType>
  </properties>
</profile>

<profile>
  <id>Action type W restriction</id>
  <activation>
    <property>
      <name>actionType</name>
      <value>W</value>
    </property>
  </activation>
  <properties>
    <restriction.actionType>W</restriction.actionType>
  </properties>
</profile>

我的问题是:是否有可能使用一个带有变量的配置文件来完成相同的工作,例如:

我的问题是这个; 是否可以以某种方式使用具有变量的一个配置文件来执行相同的任务,类似于:

<profile>
  <id>Action type restriction</id>
  <activation>
    <property>
      <name>actionType</name>
      <value>[D,W]</value>
    </property>
  </activation>
  <properties>
    <restriction.actionType>${project.profiles.profile.activation.value}</restriction.actionType>
  </properties>
</profile>

或类似的内容。

你为什么要先使用一个配置文件?不如直接传递“-Drestriction.actionType=D”而不是“-DactionType=D”。 - Tunaki
2个回答

3
也许您需要类似这样的东西。
<profiles>
    <profile>
        <id>Action type restriction</id>
        <activation>
            <property>
                <name>actionType</name>
            </property>
        </activation>
        <properties>
            <restriction.actionType>${actionType}</restriction.actionType>
        </properties>
    </profile>
</profiles>

好的,但我还希望编译器在actionType的值不是'D'或'W'时抛出一个错误。 - Lazaruss
你可以使用maven-enforcer-plugin来实现这个功能。 - toolforger

0

谢谢 Tunaki。这样做就可以了。现在我的设置是这样的:

<project>
...
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <ActionType/>
</properties>
...
<profiles>
    <profile>
        <id>Action-type profile</id>
        <activation>
            <property>
                <name>actionType</name>
                <value>[D,W]</value>
            </property>
        </activation>
    </profile>
</profiles>
...
<build>
    <pluginManagement>
      <plugins>
        <plugin>
            <groupId>properties.plugin</groupId>
            <artifactId>Lazaruss-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>

            <configuration>
                <actionType>${actionType}</actionType>
            </configuration>
            ...
        </plugin>
      </plugins>
    </pluginManagement>
</build>

使用以下命令运行:

mvn groupId:artefactId:version:goal -DactionType=W

现在它将我的类中的变量设置为W :)

附注:请注意,我的类中的变量也被命名为'actionType'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接