Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are project layout resources kept separate from Java sources?

Tags:

Why does maven keep resources in a separate "source folder" from the Java sources?

From my experience, in Java the resource files are mostly treated like Java source files which, when "compiled", just need to be copied as-is with the classes, and eventually packaged in the jar, and accessed by the classloader's methods getResource/getResourceAsStream, via the classpath.

Personally I find it a useless complexity to keep resource files separate from Java sources.

  1. What do you think?
  2. Is there a good reason why maven keeps resources separate from sources?
  3. Is there any counter indication in not using src/main/resources and src/test/resources and keeping resources in src/main/java and src/test/java using maven?
like image 993
Luigi R. Viggiano Avatar asked Jan 28 '11 16:01

Luigi R. Viggiano


People also ask

Why do Java projects have so many folders?

This is due to recommended packaging structure. In large projects, so many packages/libraries are used and in order not to put source files into same folder with another library, programmers put their source codes into unique folders.

What is Java project resources?

what is a resource? a resource is a file in the class path folder structure for your project. this is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy — both in your target folder.

What is src folder in maven project?

The src directory contains all of the source material for building the project, its site and so on.

What is src main Java?

As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here. Its subdirectories are: src/main/java – Java source code for the artifact.


2 Answers

One point that hasn't been brought up yet is that you are obviously used to seeing projects with only java sources in them. however, if you throw in some other source file types, i think the organization makes more sense, e.g.:

  • src/main
    • resources
    • java
    • groovy

each sub-dir has a specific classification of files:

  • java -> things that are compiled as java files
  • groovy -> things that are compiled as groovy scripts
  • resources -> uncompiled data used for whatever... (also, these may be filtered to add compile-time info)

also (as i've noted in some comments already), i don't usually make the resources directory flat. files may be nested into a package-like structure or into other sub-directories as appropriate (to my sense of organization).

like image 106
jtahlborn Avatar answered Nov 04 '22 12:11

jtahlborn


for simplicity and easier access we keep some resources as well in the java source path. This makes it convenient for us when developing on the GUI level as velocity templates and such are close to the Controller Java Code. But you have to tell maven to include non java stuff which is in src/main/java by adding something like this to your pom file.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/java</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                      <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.htm</include>
                        <include>**/*.html</include>
                        <include>**/*.css</include>
                        <include>**/*.js</include>
                      </includes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                      <excludes>
                        <exclude>**/log4j.xml</exclude>
                      </excludes>
                    </resource>
                </webResources>
                </configuration>
            </plugin>
like image 42
guido Avatar answered Nov 04 '22 14:11

guido