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

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

    The simplest way I know of doing that is to use Templating Maven Plugin.

    Add plugin declaration to your pom:

    
        org.codehaus.mojo
        templating-maven-plugin
        1.0.0
        
            
                filter-src
                
                    filter-sources
                    filter-test-sources
                
            
        
    
    

    If you're filtering main sources:

    • Create folder src/main/java-templates
    • Move the source files you want to be filtered to that folder. Reproduce package tree structure, as if you were in src/main.

    If you're filtering tests sources too:

    • Create folder src/test/java-templates
    • Move the source files you want to be filtered to that folder. Reproduce package tree structure, as if you were in src/test.

    Assuming that your sources contain valid placeholders like:

    package some.company;
    
    public class SomeVersion {
    
        public static String getVersion() {
            return "${project.version}"
        }
    
    }
    

    Now when you compile or test your project, those placeholders should be already valued.

    Hope it helps.

提交回复
热议问题