【问题标题】:Accessing Project Properties from settings.xml从 settings.xml 访问项目属性
【发布时间】:2011-11-18 23:46:04
【问题描述】:

我正在尝试通过 pom 的 groupId 的值激活我的 settings.xml 中的配置文件。为了实现这一点,我有:

<settings>
    <profiles>
      <profile>
    <id>imf</id>
    <activation>
      <property>
        <name>project.groupId</name>
        <value>myId</value>
      </property>
    </activation>
    <properties>
     . . .
    </properties>
      </profile>
   </profiles>
 </settings>

但这不起作用。不能从设置中访问项目属性吗? reference material 表示可以。

为了验证我对属性激活器元素的使用,我使用命令行中的属性集进行了完整性检查。实际上,如果我将 -Dproject.groupId=myId 传递给 mvn 命令行,我的激活就会起作用。这让我相信 settings.xml 文件中根本没有项目属性。

【问题讨论】:

    标签: maven properties settings


    【解决方案1】:

    我有类似的要求 - 根据项目的 groupId 在配置文件之间进行选择(在远程存储库之间进行选择)。然后我注意到我的 groupIds 实际上总是我的 java 类层次结构中的包名。包名只是文件系统中的简单目录。所以我对这个问题的解决方案是使用 actication/file/exists 标签:

      <profiles>
        <profile>
            <id>client1</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <file>
                    <exists>${basedir}/src/main/java/hu/client1</exists>
                </file>
            </activation>
            <repositories>
                <repository>
    
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>client2</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <file>
                    <exists>${basedir}/src/main/java/hu/client2</exists>
                </file>
            </activation>
            <repositories>
                <repository>
    
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>client3</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <file>
                    <exists>${basedir}/src/main/java/com/client3</exists>
                </file>
            </activation>
            <repositories>
                <repository>
    
               </repository>
            </repositories>
        </profile>
    </profiles>
    

    【讨论】:

    • 我更感兴趣的是为什么 project.properties 在这种情况下不起作用。
    【解决方案2】:

    您的特定要求似乎无法以您尝试的方式实现。

    project.groupId 作为属性名(或键)对 maven 没有任何意义。 maven 理解(并扩展)${project.groupId}settings.xmlpom.xml 中的类似值。

    【讨论】:

    • 很抱歉,但实际情况并非如此。配置文件激活的属性元素采用属性的名称和该属性的指定值,作为激活配置文件的条件。
    • 并且,请注意,我在我的问题中包含了一个健全性检查——我使用 -D 参数传递了属性;在这种情况下,它工作正常。这验证了我的语法,除非我弄错了。
    • @chad。当与 -D 一起传递时,该属性被解释为与任何其他属性一样 - 没有特定的语义与 project.groupId 相关联。您不妨将&lt;name&gt; 指定为myGroup,并且在使用-DmyGroup=myId 运行时它会起作用
    • 我不确定我是否关注你。撇开我的健全性检查的细节不谈,您是否仍然建议我需要在 name 元素中使用 ${} 引用符号?
    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多