How can I write maven build to add resources to classpath?

后端 未结 3 1123
鱼传尺愫
鱼传尺愫 2020-12-04 21:56

I am building a jar using maven with simple maven install. If I add a file to src/main/resources it can be found on the classpath but it has a config folder whe

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 22:30

    A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.

    For instance, place your property file in a new folder src/main/config, and add the following to your pom:

     
        
            
                src/main/config
            
        
     
    

    From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:

        
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    
                        my-config.properties
                    
                
            
        
    

    so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).

提交回复
热议问题