how to get property value from pom.xml?

后端 未结 4 982
渐次进展
渐次进展 2021-01-17 19:51

I have added a node in my pom.xml:


        1.5

         


        
4条回答
  •  感情败类
    2021-01-17 20:23

    So you have a property like this.

    
        1.5
    
    

    Create a file as follows in your Maven project.

      src/main/resources/project.properties
    

    Or as follows if it is for tests only.

      src/test/resources/project.properties
    

    Add this line inside the new file. Please note that you should not prefix with "properties" (e.g. don't write "properties.getdownload-webapp.version").

      version=${getdownload-webapp.version}
    

    Note that you can also add flags like this to the file.

      debug=false
    

    If not already done, you have to enable Maven filtering for your project. It is the feature that will look for placeholders inside the files of your project to be replaced by values from the pom. In order to proceed, you need add these lines inside the tag of your pom.xml file. This is how to do with src/main:

    
        
            
                src/main/resources
                true
            
        
        ...
    

    And here is how to do for src/test:

    
        
            
                src/test/resources
                true
            
        
        ...
    

    Finally, in your source code (MyClassName.java), add a block like

      Properties props = new Properties();
      props.load(MyClassName.class.getClassLoader().getResourceAsStream("project.properties"));
      String version = props.getProperty("version");
    

    You can add as many variables as you want to the project.properties file and load each one using this method.

提交回复
热议问题