Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven-3

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 where I want that file to go but moving it inside the config folder makes it disappear from the classpath.

like image 590
Java Ka Baby Avatar asked Jan 30 '12 11:01

Java Ka Baby


People also ask

Does maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest.

How does maven create the classpath?

Maven creates this classpath by considering the project's dependencies. The reported classpath consists of references to JAR files cached in local Maven repository. Even the JAR artifact of the project is referenced from local Maven cache and not from the /target directory one or the other might expect.


1 Answers

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:

 <build>     <resources>         <resource>             <directory>src/main/config</directory>         </resource>     </resources>  </build> 

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:

    <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-jar-plugin</artifactId>             <configuration>                 <excludes>                     <exclude>my-config.properties</exclude>                 </excludes>             </configuration>         </plugin>     </plugins> 

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).

like image 116
Yanflea Avatar answered Oct 23 '22 11:10

Yanflea