How do you organize the non-source resources available on the classpath in your java project?
In a software project written in java, you often have resources that are part of the project and must be included in the classpath. For example, some templates or images that should be accessible via the classpath (getResource). These files must be included in the prepared JAR file.
It is clear that these resources need to be added to version control. But in which directory do you put these files? In parallel with java-source files or in another directory that also reproduces the required package structure?
source to share
Maven puts them in src / main / resources / (Java code goes to src / main / java /). The main reason is that Maven can compile code in different languages, and it makes sense to keep them separate, so one compilation doesn't get confused with files for another.
In the case of resources, Maven will replace the variables in them before they are added to the Jar, so they are also "compiled" sources.
source to share
Personally, I would prefer to have them along with the source code if they are strongly related to certain parts of the code, and if there are not that many of them.
But if it makes sense for these files to have their own organizational structure, or if there are hundreds of files that make it difficult to find the source code among them, I would keep them separately.
source to share
It depends on your preference. Mixing them with source files makes refactoring easier, such as renaming packages (as the source is moved to source with source), but with more than a few icons, images, and localization files, you easily end up with a mess of files.
With today's tools, it doesn't really matter either. Regardless of whether you are using Eclipse, Netbeans, or another tool, they all allow you to have binary packages with a different build than your source code. So in the end, you can do it however you want.
Personally, I try not to mix source with resources, because I usually rarely change resources, but source code very often.
source to share
I usually go like this:
project/src project/resources project/classes project/lib project/dist
Both src and resources have a package structure and the build file takes classes and resources as input to the jar which is placed in dist.
When run inside a class, the classpath looks like: lib; classes; resources;
If the application has an installation, the installer creates and places a resource directory in the installation.
source to share
You can put these things on the classpath if your build tool isn't confusing. Eclipse won't try to compile your jpegs and there won't be Ant, so nothing to worry about. The nice thing is that if you have a lot of related things, you can keep them together organized by functionality rather than file type.
We do this when I work; we have emails that require a bit of code and a few static resources. All of these things are kept together in one folder in the java source path; we can easily add and remove email from the system without forgetting things or making mistakes like src / resource paths, inconsistent or orphan files in one tree not present in another tree.
source to share
As stated, this is up to you and may differ on every project. For example, with Wicket it is quite convenient and convenient to store files .html
next to classes. In addition, I often retains some configuration with the source - META-INF
, WEB-INF
, logging.
To force Maven to grab resources from a source tree use this:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- Web - Wicket -->
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes><include>**</include></includes>
<excludes><exclude>**/*.java</exclude></excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<!-- Web - Wicket -->
<testResource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes><include>**</include></includes>
<excludes><exclude>**/*.java</exclude></excludes>
</testResource>
</testResources>
The main reason for keeping resources shared is (imho) separation of worker roles. For example. if you have a project with translation teams, you save resources with strings stripped from code and different SCM privileges.
source to share