简体   繁体   English

如何从父pom.xml的配置文件中提取属性数据?

[英]How to extract properties data from a profile in a parent pom.xml?

I have a big Java project and each sub-part of the project has a child pom.xml. 我有一个很大的Java项目,该项目的每个子部分都有一个子pom.xml。 The parent pom has key data I want, username and password. 父pom有我想要的关键数据,用户名和密码。 My question is how can I extract this data from a parent pom.xml in my java program? 我的问题是如何从Java程序的父pom.xml提取此数据?

The parent pom.xml has numerous profiles in the profile section where the data is that I need. 父pom.xml在配置文件部分中有许多配置文件,这些配置文件是我需要的数据。 I found a similar example that said to simply code System.getProperty('property.name'); 我发现了一个类似的示例,据说该示例System.getProperty('property.name');编码System.getProperty('property.name'); When I tried this the code just returned null though. 当我尝试此代码只是返回null。 I think it is because I ran the code from the target directory and not through Maven, but I could be completely wrong about this. 我认为这是因为我是从target目录而不是通过Maven运行代码的,但是我对此可能完全错了。

Does anyone know what I am missing? 有人知道我在想什么吗?

Regards, 问候,

Maven is a build tool (and much more). Maven是一个构建工具(还有更多)。 Your program does not know its existence. 您的程序不知道它的存在。 The example you probably refer to, uses some system property which is available to both maven and your code. 您可能引用的示例使用了一些Maven和您的代码都可以使用的系统属性。 You can refer to system properties in maven. 您可以在Maven中引用系统属性。 Its easy. 这很容易。

see reference 参考

Any property which can be retrieved from the System.getProperty() method can be referenced as a Maven property. 可以从System.getProperty()方法检索的任何属性都可以引用为Maven属性。

My answer would clearly oppose the one that @grid suggested. 我的答案显然会反对@grid建议的答案。 Although Maven is a build tool, but it is also such a goodness that it can afford any functionality when you deeply think about it. 尽管Maven是一个构建工具,但是它也是一个很好的工具,当您深入考虑它时,它可以提供任何功能。

There was once a time where I needed to hook a custom properties file and ask Maven to filter it with project properties then read input that file content within the source code as a java.util.Properties . 曾经有一段时间,我需要挂接一个自定义属性文件,并要求Maven使用项目属性对其进行过滤,然后以java.util.Properties形式在源代码中读取该文件内容的输入。

Now that part is over I suppose, since all the burden is done for you by the Properties Maven Plugin which has a cutom goal, " set-system-properties ", that can make project properties (those used in by Maven and which are said to be build-time properties) available as runtime-properties and more precisely as System Properties . 我想现在已经结束了,因为所有的负担都由Properties Maven插件为您完成,该插件的目标是“ set-system-properties ”,可以使项目属性 (那些由Maven使用的属性称为可以作为运行时属性,更确切地说作为系统属性可用。

You can add a custom plugin configuration snippet within your build section, the one in your child project, that may look as follows: 您可以在构建部分( 子项目中的一个)中添加一个自定义插件配置代码段,如下所示:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <goals>
              <goal>set-system-properties</goal>
            </goals>
            <configuration>
              <properties>
                <property>
                  <name>property.name</name>
                  <value>${project.version}</value>
                </property>
              </properties>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Although you can have any value set to your property, note the usage of the placeholder ${...} so that Maven resolves the property when the project descriptors are interpolated. 尽管可以为属性设置任何值,但是请注意占位符${...}的用法,以便在插入项目描述符时Maven可以解析该属性。 You can the reference any property such a way, then have it available at runtime as you suggested in your main post: 您可以以这种方式引用任何属性,然后按照您在主帖子中的建议在运行时将其提供:

package com.github.tmarwen.stackoverflow.maven.properties.showcase;

public Main {
  public static void main( String... args ) {
    String value = System.getProperty( "property.name" );
    System.out.print( value );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM