2015-12-16 78 views
5

我有3个不同的简档的Maven应用,下面在Maven中替换文件的正确方法是什么?

<profiles> 
     <profile> 
      <id>dev</id> 
      <properties> 
       <profileVersion>DEV</profileVersion> 
       <webXmlFolder>${id}</webXmlFolder> 
      </properties> 
     </profile> 

     <profile> 
      <id>test</id> 
      <properties> 
       <profileVersion>1.0.0-RC1</profileVersion> 
       <webXmlFolder>${id}</webXmlFolder> 
      </properties> 
     </profile> 

     <profile> 
      <id>prod</id> 
      <properties> 
       <profileVersion>1.0.0-Final</profileVersion> 
       <webXmlFolder>${id}</webXmlFolder> 
      </properties> 
     </profile> 
    </profiles> 

指定我有Maven的结构是这样的:

的src /主/配置/默认/ WEB-INF/web.xml中

的src /主/配置的/ dev/WEB-INF/web.xml中

的src /主/配置/测试/ WEB-INF/web.xml中

的src/main /配置/生产/ WEB-INF/web.xml中

的src /主/ web应用/ WEB-INF/

我的任务是要设置指定的web.xml中为web应用/ WEB- INF在构建时取决于指定的配置文件。如果未指定配置文件,则web.xml将从默认文件夹复制。

我有插件,但它不工作。

<plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-resources-plugin</artifactId> 
      <version>2.4</version> 
      <executions> 
       <execution> 
        <id>copy-prod-resources</id> 
        <phase>process-resources</phase> 
        <goals> 
         <goal>copy-resources</goal> 
        </goals> 
        <configuration> 
         <overwrite>true</overwrite> 
         <outputDirectory>${project.build.outputDirectory}/classes/WEB-INF</outputDirectory> 
         <resources> 
          <resource> 
           <directory>src/main/config/${webXmlfolder}/WEB-INF</directory> 
           <filtering>true</filtering> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

任何想法?我花了很多时间处理这个问题,现在我很困惑。

+2

所有定义的配置文件依赖于$ {id}属性,它是否在POM的其余部分中定义? –

+0

我的错误是,$ {id}占位符值是定义的应用程序的全名,例如[groupId:artifactId:version]而不是简档ID名称。 :) –

回答

3

好的,现在一切正常。这是我的最终代码,它的工作原理如下:

<properties> 
    <webXmlFolder>default</webXmlFolder> 
    <profileVersion>defaultVersion</profileVersion> 
</properties> 

<profiles> 
    <profile> 
     <id>dev</id> 
     <properties> 
      <profileVersion>DEV</profileVersion> 
      <webXmlFolder>dev</webXmlFolder> 
     </properties> 
    </profile> 

    <profile> 
     <id>test</id> 
     <properties> 
      <profileVersion>1.0.0-RC1</profileVersion> 
      <webXmlFolder>test</webXmlFolder> 
     </properties> 
    </profile> 

    <profile> 
     <id>prod</id> 
     <properties> 
      <profileVersion>1.0.0-Final</profileVersion> 
      <webXmlFolder>prod</webXmlFolder> 
     </properties> 
    </profile> 
</profiles> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-resources-plugin</artifactId> 
      <version>2.4</version> 
      <executions> 
       <execution> 
        <id>copy-web.xml</id> 
        <phase>package</phase> 
        <goals> 
         <goal>copy-resources</goal> 
        </goals> 
        <configuration> 
         <overwrite>true</overwrite> 
         <outputDirectory>${basedir}/target/classes/WEB-INF</outputDirectory> 
         <resources> 
          <resource> 
           <directory>src/main/config/${webXmlFolder}/WEB-INF</directory> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
+0

我有一个问题,当web.xml文件更改时如何处理?示例:'web.dev.xml'' web.preprod.xml'' web.prod.xml' – Mercer

+0

'mvn clean install -P dev' - 在控制台中键入的此命令通过dev配置文件启动Maven生命周期。您可以通过“-P配置文件”指定配置文件。 –

+1

感谢您发布您的解决方案! –

相关问题