2009-02-04 140 views

回答

54
  • 使用Maven型材(http://maven.apache.org/guides/introduction/introduction-to-profiles.html
  • 创建一个 “dev的” 和 “PROD” 简档,选择一个替代资源集合的每个配置文件。使Dev成为默认激活状态。

    <profiles> 
        <profile> 
         <id>dev</id> 
         <activation> 
          <activeByDefault>true</activeByDefault> 
         </activation> 
         <build> 
          <resources> 
           <resource> 
            <directory>src/main/resources/dev</directory> 
           </resource> 
          </resources> 
         </build> 
        </profile> 
        <profile> 
         <id>prod</id> 
         <build> 
          <resources> 
           <resource> 
            <directory>src/main/resources/prod</directory> 
           </resource> 
          </resources> 
         </build> 
        </profile> 
    </profiles> 
    
  • 构建通过使用所需的个人资料: ​​ 或 mvn install -Pprod

+0

这似乎比我以前的优雅! – 2009-02-24 02:18:52

+2

很高兴听到它。 Maven可能看起来很棘手,但当沿着最佳实践路线使用时,听到像你这样的人会说“美丽!” – 2009-02-25 03:26:44

9

我使用maven-resources插件解决了这个问题,其中我创建了prod目录,其中包含生产环境的资源并将它们复制到了process-resources阶段的WEB-INF/classes目录。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.3</version> 
    <executions> 
     <execution> 
      <id>copy-prod-resources</id> 
      <phase>process-resources</phase> 
      <goals> 
       <goal>copy-resources</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>webapp/WEB-INF/classes</outputDirectory> 
       <resources> 
        <resource> 
         <directory>src/main/resources/prod</directory> 
         <filtering>true</filtering> 
        </resource> 
       </resources> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
5

上面的代码并没有为我工作 - 不得不将其更改为以下:

<plugin> 
    <artifactId>maven-resources-plugin</artifactId> 
    <executions> 
    <execution> 
     <id>copy-prod-resources</id> 
     <phase>process-resources</phase> 
     <goals> 
     <goal>copy-resources</goal> 
     </goals> 
     <configuration> 
     <!-- this is important --> 
     <overwrite>true</overwrite> 
     <!-- this as well (target/ was missing) --> 
     <outputDirectory>${basedir}/target/classes</outputDirectory> 
     <resources> 
      <resource> 
      <directory>src/main/resources/prod</directory> 
      </resource> 
     </resources> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
3

最后响应正在工作。 但是你需要给版本以使其工作。

<plugin> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.3</version> 
    <executions> 
    <execution> 
     <id>copy-prod-resources</id> 
     <phase>process-resources</phase> 
     <goals> 
     <goal>copy-resources</goal> 
     </goals> 
     <configuration> 
     <!-- this is important --> 
     <overwrite>true</overwrite> 
     <!-- target --> 
     <outputDirectory>${basedir}/target/classes</outputDirectory> 
     <resources> 
      <resource> 
      <!-- source --> 
      <directory>src/main/resources/prod</directory> 
      </resource> 
     </resources> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
相关问题