2011-04-15 69 views
20

我试图激活使用内部pom.xml定义的属性行家简介:如何通过maven属性激活配置文件?

<project> 
    [...] 
    <properties> 
    <run.it>true</run.it> 
    </properties> 
    [...] 
    <profiles> 
    <profile> 
     <activation> 
     <property><name>run.it</name></property> 
     </activation> 
     [...] 
    </profile> 
    </profiles> 
    [...] 
</project> 

显然,这是行不通的。然而,激活从命令行起作用:

mvn -Drun.it 

它是“按设计”吗?如果是,可能的解决方法是什么?

+0

这是一个解决方法:http://stackoverflow.com/a/14386303/290918 – kldavis4 2013-01-17 21:40:59

+0

我已经创建了一个功能请求,它将'settings.xml'中的''标签添加到'pom。xml“,以便您可以定义要激活的配置文件。 https://issues.apache.org/jira/browse/MNG-6000 – flungo 2016-04-14 15:36:03

回答

22

编辑,完全重写,因为我现在明白了这个问题。

this forum post

轮廓激活基于SYSTEM性能。 的 构建计划已开始执行

+0

您能否再次查看该问题?我试图用'pom.xml'中定义的属性激活配置文件,而不是在命令行中。 – yegor256 2011-04-15 12:19:33

+0

事实上,我错过了这一点,看到activeByDefault为M. Jessup告诉 – moritz 2011-04-15 12:25:31

+0

我改变了我的答案 – moritz 2011-04-15 12:34:27

-3

后无法激活根据您的POM 您无法激活基于定义的系统属性配置文件定义的属性配置文件,我认为你的问题是与此类似。

Activation of maven profile based on multiple properties

如前所述,可以激活一个配置文件,并设置各种属性按您的要求和使用命令mvn -Prun-it设置属性为true。

<project> 
    [...] 

    [...] 
    <profiles> 
    <profile> 
    <id>don't-run</id> 
<properties> 
    <run.it>false</run.it> 
    </properties> 


     [...] 
    </profile> 


    <profile> 
    <id>run-it</id> 
<properties> 
    <run.it>true</run.it> 
    </properties> 

     [...] 
    </profile> 


    </profiles> 
    [...] 
</project> 
+1

请仔细查看我的问题。我有兴趣从'pom.xml'内部打开/关闭配置文件,而不是从命令行或'settings.xml'。 – yegor256 2011-04-15 14:19:04

+0

但是它对你有什么影响,请你详细说明一下?另外,正如Moritz所提到的,激活是基于系统属性而不是通过命令行完成的。请参阅此参考http://maven.apache.org/guides/introduction/introduction-to-profiles.html – Prabhjot 2011-04-15 14:26:16

-2

作为莫里茨Heuser先生解释,型材激活是基于系统性质。但是,您可以尝试类似的东西:

<project> 
    ... 
    <properties> 
    <run.it>true</run.it> 
    </properties> 
    ... 
    <profiles> 
    <profile> 
     <activation> 
     <activeByDefault>${run.it}</activeByDefault> 
     </activation> 
     ... 
    </profile> 
    </profiles> 
    ... 
</project> 

的想法是在<properties>定义activeByDefault标志。

+1

我不明白这有何帮助。在pom中定义run.it对于配置文件生效来说太晚了。每个https://jira.codehaus.org/browse/MNG-5235 – 2013-05-15 01:31:53

+0

我同意。有一个典型的鸡与鸡蛋问题:配置文件我的性质可能会定义其他属性。他们是否也应该递归地激活其他配置文件?如果配置文件已被激活,但新的属性定义指示将其停用,该怎么办? – 2013-06-06 09:57:41

+0

这与OP的示例不起作用的原因不一致(正如@moritz所解释的) – flungo 2016-04-14 15:42:59

5

怎么样使用激活这样

<profile> 
     <id>gwt</id> 
     <activation> 
      <file> 
       <exists>uses-gwt.marker</exists> 
      </file> 
     </activation> 

,并添加文件“使用-gwt.marker”源代码控制,旁边的pom.xml。这为所有开发人员提供了相同的状态,并允许面向方面的pom。我们在父pom中使用这种技术,并将hte标记文件放在孩子svn中。不理想,但有效。

0

如前面的答案中所述,配置文件激活仅适用于系统属性。

但是,有了一点创造力,您可以使用pom属性实现类似的结果(条件插件执行)。为了实现这个目标,使用插件的相位标签要有条件地执行:

<project> 

    ... 

    <properties> 
     <run.it>none</run.it> 
     <!-- <run.it>compile</run.it> --> 
     <!-- <run.it>package</run.it> --> 
    </properties> 

    ... 

    <build> 

     ... 

     <plugins> 
      <plugin> 
      <artifactId>your-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>${run.it}</phase> 
       </execution> 
      </executions> 

      </plugin> 
     </plugins> 

     ... 

    </build> 

</project> 

唯一的区别是,你必须使用相名称,而不是真/假。但是您可以更改属性或将其作为属性自由覆盖。

相关问题