2017-04-13 138 views
0

我们使用ant来构建(一个巨大的)项目,并且(由于遗留原因)我们非常深入地嵌套属性文件中的属性。属性中的嵌套属性(在属性中)(ant to maven migration)

(里面的build.xml例如)

<ant dir="${object.build.dir}" target="deploy" /> 

(里面的一些属性文件:)

object.build.dir=${base.dir} 

(里面的一些其他属性文件(有时甚至是相同的)):

base.dir=${env}/some/dir 

那么,因为蚂蚁不再是最近的事了,我们现在正在转向maven,这将是一场噩梦......

的问题是:你怎么看那些内部性能嵌套属性,只要5级深有时候的结果属性...

(作为测试)

过滤器/ TOP_LEVEL。属性:

env=test 

过滤器/测试/ mid_level.properties:

specific=spec 

过滤器/ top_level.properties还可以读取(另一个环境):

env=prod 

等过滤器/生产/ mid_level.properties:

specific=spock 

的问题归结为:

如何读取/使用在另一个属性文件中为其定义路径的属性文件中定义的“特定”属性?) (而且这种嵌套遍布各处?)

此外,这不涉及'资源'(这些也涉及到,但不是主要问题,资源是应用程序使用的属性,我谈论的属性是仅在构建时用于指定构建情况)

我找到了答案,但如果有一个更好的我会很高兴选择为最佳答案...

S.

+0

您的属性看起来像一个设置为不同的环境,这从来都不是一个好主意,既不在Ant中也不在Maven中使用属性.. ..可能你可以看看这里:https://github.com/khmarbaise/multienv-maven-plugin...Furthermore我假设你建立每个时间为每个属性,这意味着开发,产品等。这将在Maven中不工作...只需构建一次,然后在一次运行中生成所有需要的输出... – khmarbaise

+0

嘿,谢谢,我肯定会看看它,S。 – Bamboomy

回答

0

Maven中你有properties:read-project-properties插件。

如果你像这样使用它,嵌套属性将不会被插入到后代: (maven仍然会抱怨它找不到“filters/$ {env}/mid_level”。属性”属性文件

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>properties-maven-plugin</artifactId> 
      <version>1.0.0</version> 
      <executions> 
       <execution> 
        <id>execution1</id> 
        <phase>initialize</phase> 
        <goals> 
         <goal>read-project-properties</goal> 
        </goals> 
        <configuration> 
         <files> 
          <file>filters/top_level.properties</file> 
          <file>filters/${env}/mid_level.properties</file> 
         </files> 
        </configuration> 
       </execution> 
      </plugin> 
    </plugins> 
</build> 

但是,如果你喜欢这个单独执行(+呼应 '特定' :)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>test</groupId> 
    <artifactId>test</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>test</name> 
    <description>test</description> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>properties-maven-plugin</artifactId> 
       <version>1.0.0</version> 
       <executions> 
        <execution> 
         <id>execution1</id> 
         <phase>initialize</phase> 
         <goals> 
          <goal>read-project-properties</goal> 
         </goals> 
         <configuration> 
          <files> 
           <file>filters/top_level.properties</file> 
          </files> 
         </configuration> 
        </execution> 
        <execution> 
         <id>execution2</id> 
         <phase>initialize</phase> 
         <goals> 
          <goal>read-project-properties</goal> 
         </goals> 
         <configuration> 
          <files> 
           <file>filters/${env}/mid_level.properties</file> 
          </files> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

      <plugin> 
       <groupId>com.soebes.maven.plugins</groupId> 
       <artifactId>maven-echo-plugin</artifactId> 
       <version>0.1</version> 
       <executions> 
        <execution> 
         <phase>install</phase> 
         <goals> 
          <goal>echo</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <echos> 
         <echo>Animal: ${specific}</echo> 
        </echos> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

与ENV =督促输出的值:

[INFO] --- maven-echo-plugin:0.1:echo (default) @ test --- 
[INFO] Animal: spock 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.663 s 
[INFO] Finished at: 2017-04-13T21:32:30+02:00 
[INFO] Final Memory: 11M/164M 
[INFO] ------------------------------------------------------------------------ 

问题解决:)

你'只是'需要有尽可能多的财产执行深度嵌套+重构属性让他们在地层,但appart从这个问题'很容易'解决...