如何通过maven属性将属性设置为gwt.xml文件?

问题描述:

我在root pom.xml文件中有一个属性:gecko1_8。我想把它放到gwt.xml文件中。如何通过maven属性将属性设置为gwt.xml文件?

所以我把这个属性来gwt.xml:

我增加了以下建部:

<resources> 
    <resource> 
    <directory>src/main/resources</directory> 
    <filtering>true</filtering> 
    <excludes> 
     <exclude>VAADIN.themes/*</exclude> 
    </excludes> 
    </resource> 
</resources> 

但最后构建失败,出现错误:

ERROR: Invalid property value '${gwt.user.agents}'

ERROR: Failure while parsing XML

如何通过属性将值从pom.xml放到gwt.xml文件中?

修订

有趣的事情。当我使用“mvn resources:resources”时,属性的值正确写入gwt.xml文件,但是如果我运行“mvn clean install -pl com.myproject.module:submodule”,它将以“invalid property value”失效。

有一件事我没有提但它似乎太重要了。在构建期间使用的插件可以有自己的目标。其中一个目标可以“重做”maven-resource-plugin的功能。

所以我做了一个插件vaadin - Maven的插件:

<plugin> 
<groupId>com.vaadin</groupId> 
<artifactId>vaadin-maven-plugin</artifactId> 
<version>${vaadin.plugin.version}</version> 
<configuration> 
    ... 
</configuration> 
<executions> 
    <execution> 
     <goals> 
      <goal>resources</goal> 
      <goal>compile</goal> 
     </goals> 
    </execution> 
</executions> 
</plugin> 

和去除的<goal>resources</goal>固定的问题。现在mvn clean install正确地过滤了我的gwt.xml中的属性。

希望这个解决方案可以帮助那些在这样的问题上运行的人。

你必须定义一个maven配置文件(最好定义每个案件的具体配置文件)在你的POM这样的:

<profile> 
     <id>gecko</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
     </activation> 
     <properties> 
      <user.agent.gecko> 
       <![CDATA[<set-property name="user.agent" value="gecko,gecko1_8" />]]> 
      </user.agent.gecko> 
     </properties> 
     <build> 
      <resources> 
       <resource> 
        <directory>src/main/resources</directory> 
        <includes> 
         <include>**/*YourGWTModule.gwt.xml</include> 
        </includes> 
        <filtering>true</filtering> 
       </resource> 
      </resources> 
      <defaultGoal>process-resources</defaultGoal> 
     </build> 
    </profile> 

<properties> 
    <!--<user.agent>ie6,ie8,gecko,gecko1_8,opera,safari</user.agent>--> 
    <user.agent.all> </user.agent.all> 
    <user.agent.ie6> </user.agent.ie6> 
    <user.agent.ie8> </user.agent.ie8> 
    <user.agent.gecko> </user.agent.gecko> 
    <user.agent.opera> </user.agent.opera> 
    <user.agent.safari> </user.agent.safari> 
</properties> 

然后将其设置在YourGWTModule.gwt.xml这样的:

<set-property name="locale" value="default" /> 
<!-- Specified through pom.xml profiles --> 
${user.agent.all} 
${user.agent.ie6} 
${user.agent.ie8} 
${user.agent.gecko} 
${user.agent.safari} 
${user.agent.opera} 

</module> 

最后运行Maven与简介:

mvn -P gecko install 
+1

但是如果我有几个使用GWT的模块呢?我必须在每个模块pom.xml中编写(复制 - 粘贴)这些配置文件?我的意思是,如果我想用-pl选项构建一个特定的模块。我认为将属性放在根目录pom.xml中应该足够了...有一些额外的配置。 – Dragon

+0

@Dragon我认为你的解决方案很好,或者你可以定义一个父pom。 –

+0

注意(对于后代;)):如果您有Spring Boot应用程序,则应使用@ .... @而不是$ {...},如下所述:https://*.com/questions/36501017/ Maven的资源 - 过滤 - 不工作,因为-的弹簧引导依赖性 – machinery