Is there a way to use maven property in Java class during compilation

前端 未结 4 1008
南方客
南方客 2020-12-04 21:58

I just want to use maven placeholder in my Java class at compile time in order to reduce duplication.

Something like that:

pom.xml



        
4条回答
  •  青春惊慌失措
    2020-12-04 22:42

    If you are working with Spring, you can inject a property. The steps are:

    1. Inside POM file you define all profiles needed and each profile must have your custom property, in your case

    
    	dev
    	
    		Dev Value
    	
    

    1. In the section build of your profile, you define the filtering injection.
    2. Under the your project resources directory, you create a properties file (any mnemonic christian name) and put your prop to be injected:

    custom.some.version=${some.version}

    1. On the spring-context file you define the properties placeholder and define your bean or beanProperty:

    
    ...
    
    	
    
    ...

    1. Create your class.
    package com.brand;
    
    public class CustomConfig {
      private String someVersion;
    
      public getSomeVersion() {
      return this.someVersion;
      }
    
      public setSomeVersion(String someVersion) {
      this.someVersion = someVersion;
      }
    }
    
    1. Inject where you want to use. This example is with autowired bean, but you can use and autowired property too.
    package com.brand.sub
    
    public class YourLogicClass {
      @Autowired
      private CustomConfig customConfig;
    
      // ... your code
    }
    

    On the final compilation, you have the correct values.

提交回复
热议问题