2016-07-07 61 views
1

这是我的老POM的<build>标记,它是工作:Maven的POM多环境profiels

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.0</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <wtpversion>2.0</wtpversion> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.axis2</groupId> 
       <artifactId>axis2-wsdl2code-maven-plugin</artifactId> 
       <version>1.6.2</version> 
      </plugin> 
     </plugins> 
     <finalName>test</finalName> 
    </build> 

现在我想对每个阶段3个不同的api.properties文件:PROD,开发,测试。所以我继续并遵循maven的<profile>教程。这就是我结束了只是督促和测试阶段(完全拆除旧<build>标签):

<profiles> 
     <profile> 
      <id>test</id> 
      <build> 
       <plugins> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <version>2.0</version> 
         <configuration> 
          <source>1.7</source> 
          <target>1.7</target> 
          <tasks> 
           <delete file="${project.build.outputDirectory}/api.prod.properties" /> 
           <delete file="${project.build.outputDirectory}/api.dev.properties" /> 
           <copy file="src/main/resources/api.test.properties" 
            tofile="${project.build.outputDirectory}/api.properties" /> 
          </tasks> 
         </configuration> 
        </plugin> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-eclipse-plugin</artifactId> 
         <version>2.9</version> 
         <configuration> 
          <wtpversion>2.0</wtpversion> 
          <downloadSources>true</downloadSources> 
          <downloadJavadocs>true</downloadJavadocs> 
         </configuration> 
        </plugin> 
        <plugin> 
         <groupId>org.apache.axis2</groupId> 
         <artifactId>axis2-wsdl2code-maven-plugin</artifactId> 
         <version>1.6.2</version> 
        </plugin> 
       </plugins> 
       <finalName>test</finalName> 
      </build> 
     </profile> 
     <profile> 
      <id>prod</id> 
      <build> 
       <plugins> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <version>2.0</version> 
         <configuration> 
          <source>1.7</source> 
          <target>1.7</target> 
          <tasks> 
           <delete file="${project.build.outputDirectory}/api.test.properties" /> 
           <delete file="${project.build.outputDirectory}/api.dev.properties" /> 
           <copy file="src/main/resources/api.prod.properties" 
            tofile="${project.build.outputDirectory}/api.properties" /> 
          </tasks> 
         </configuration> 
        </plugin> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-eclipse-plugin</artifactId> 
         <version>2.9</version> 
         <configuration> 
          <wtpversion>2.0</wtpversion> 
          <downloadSources>true</downloadSources> 
          <downloadJavadocs>true</downloadJavadocs> 
         </configuration> 
        </plugin> 
        <plugin> 
         <groupId>org.apache.axis2</groupId> 
         <artifactId>axis2-wsdl2code-maven-plugin</artifactId> 
         <version>1.6.2</version> 
        </plugin> 
       </plugins> 
       <finalName>test</finalName> 
      </build> 
     </profile> 
    </profiles> 

并没有真正但是工作。将生成一个.war而不是我想要的3个战争(test-prod.war,test-dev.war,...),并且生成的战争将包含所有3个.properties文件,而不是它应该拥有的那个。为什么和我能做些什么来解决它?我对maven很新颖......谢谢!

+0

你怎么跑了Maven构建?选择了全部三个配置文件?为每个配置文件运行一次应该为您提供三种不同的工件。 – Abaddon666

+0

@ Abaddon666:我只是像往常一样在日食中运行maven install ...我如何运行它“不同”? – Shiuyin

+0

我通常从命令行运行maven build,然后看起来像例如'mvn clean install -Ptest'用于运行测试配置文件。否则,你必须谷歌如何选择配置文件可用于您正在使用的maven插件 – Abaddon666

回答

0

使用

mvn clean compile -P prod,dev,test 

也最好是有你的个人资料标签之外构建标签,并引用你的插件里面的配置文件的属性。例如您可以在配置文件中指定属性的文件夹,然后在其他任何地方引用此属性。

<profile> 
     <id>dev</id> 
     <properties> 
      <properties.dir>YOUR_DEV_DIR</properties.dir> 
     </properties> 
    </profile> 

,那么你可以参考${properties.dir}其他地方