2017-04-21 55 views
0

我已经尝试过类似的努力的一些答案,但没有什么我想要实现比较适合...行家:属性路径不会被替换(读第二其它属性文件)

这是我m试图做:

我想基于(其他)属性文件生成(过滤)属性文件。

我们有一个属性文件,其中包含一般属性。该文件(现在是default.properties)被读取并解析为一个魅力。

然后,我们也想根据环境具体的属性。 (我知道,不是好的做法,但是这是最好的解决方案,因为我们有自主建立在不同的机器上,并产生所有属性​​的无处不在方便)

这就是我们陷入困境......

当指定一个包含属性的路径(在这种情况下为targetEnvironment)时,它不会被填充其值。

  • 我已经尝试切换阶段(生成资源/初始化/处理资源);与
  • 使用CLI属性:​​
  • 使用的配置文件调用:

<profiles> 
    <profile> 
     <id>nt</id> 
     <properties> 
      <targetEnvironment>NT</targetEnvironment> 
     </properties> 
    </profile> 
</profiles> 

这是(部分)当前POM(仅相关部分(聚甲醛内)显示):

<project> 

(some maven code) 

    <build> 

    (other maven code) 

     <plugins> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>properties-maven-plugin</artifactId> 
       <version>1.0.0</version> 
       <executions> 
        <execution> 
         <id>execution1</id> 
         <phase>generate-resources</phase> 
         <goals> 
          <goal>read-project-properties</goal> 
         </goals> 
         <configuration> 
          <files> 
           <file>src/env_properties/env/${targetEnvironment}/specific.properties</file> 
          </files> 
         </configuration> 
        </execution> 
        <execution> 
         <id>execution2</id> 
         <phase>generate-resources</phase> 
         <goals> 
          <goal>read-project-properties</goal> 
         </goals> 
         <configuration> 
          <files> 
           <file>src/env_properties/env/default/default.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>initialize</phase> 
         <goals> 
          <goal>echo</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <echos> 
         <echo>Animal: src/env_properties/env/${targetEnvironment}/specific.properties</echo> 
         <!-- prints: src/env_properties/env/NT/specific.properties --> 
        </echos> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-resources-plugin</artifactId> 
       <version>3.0.2</version> 
       <executions> 
        <execution> 
         <id>copy-resources</id> 
         <phase>process-resources</phase> 
         <goals> 
          <goal>copy-resources</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${basedir}/target/extra-resources</outputDirectory> 
          <resources> 
           <resource> 
            <directory>src/env_properties/base</directory> 
            <filtering>true</filtering> 
           </resource> 
          </resources> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

奇怪的是,

,如果我用文字“NT”它的工作原理替换$ {} targetEnvironment,所以机制的工作,只有不更换...

我也试图更改插件版本,但某些版本甚至使它更糟...

任何想法?

感谢,

S.

编辑:另一个奇怪的事情,如果我不建了-DtargetEnvironment=NT参数我得到一个构建失败的原因是:

[INFO] 
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (execution1) @ batch3 --- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 5.644 s 
[INFO] Finished at: 2017-04-21T15:55:11+02:00 
[INFO] Final Memory: 41M/459M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (execution1) on project batch3: Properties could not be loaded from File: C:\path\to\src\env_properties\env\${targetEnvironment}\specific.properties -> [Help 1] 
[ERROR] 

回答

0

我不知道该怎么解决原来的问题,但我发现了一些做我想做的事情:

如果你有一个属性文件(用值来替换其他值的值操作文件;例如:)

(顶级。属性)

ejb.server.url=not yet defined 
ejb.server.user=not yet defined 
ejb.server.pwd=not yet defined 

和特定的属性文件(基于通过调用例如​​)

(SRC \ env_properties \ ENV \ NT \ specific.properties)

garrafra.doesntbestaat=anotherTest 
在构建时提供的属性

那么第一个属性可以这样写:

<build> 

     (some maven code) 

     <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>src/env_properties/env/default/top_level.properties</file> 
         </files> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

     (other maven code) 

    </build> 

可以使用此文件中的属性来检索(例如路径)更具体的属性文件。

...和(当然在相同的标签)特定属性文件(一个或多个)可被读取(并且施加用于过滤)所示:

<build> 

     (some maven code) 

     <filters> 
      <filter>src/env_properties/env/${targetEnvironment}/specific.properties</filter> 
     </filters> 

     (other maven code) 

    </build> 

提供这在属性-maven-插件没有出于某种原因为特定的(与环境相关的)工作...

无论是一般(顶层)性能得到填补:

(vanilla.properties(前):)

java.naming.provider.url=${ejb.server.url} 
java.naming.security.principal=${ejb.server.user} 
java.naming.security.credentials=${ejb.server.pwd} 

blie=${garrafra.doesntbestaat} 

(后)

java.naming.provider.url=not yet defined 
java.naming.security.principal=not yet defined 
java.naming.security.credentials=not yet defined 

blie=anotherTest