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

前端 未结 4 1009
南方客
南方客 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:32

    simply create file app.properties in src/main/resources with content like this

    application.version=${project.version}
    

    then enable maven filtering like this

    
        
            
                src/main/resources
                true
            
        
    

    That's all - in app code just read properties file

    ClassPathResource resource = new ClassPathResource( "app.properties" );
    p = new Properties();
    InputStream inputStream = null;
    try {
        inputStream = resource.getInputStream();
        p.load( inputStream );
    } catch ( IOException e ) {
        LOGGER.error( e.getMessage(), e );
    } finally {
        Closeables.closeQuietly( inputStream );
    }
    

    and provide method like this

    public static String projectVersion() {
        return p.getProperty( "application.version" );
    }
    

提交回复
热议问题