I am going to be using Subversion for source control on a new J2EE web application. What directory structure will you recommend for organizing code, tests and documentation?
I usually have
Project Directory
src - actual source
doc - documentation
lib - libraries referenced from source
dep - installation files for dependencies that don't fit in lib
db - database installation script
In work with Visual Studio, I'm not sure if this works the same in the java world. But i usually put stuff in different project folders in src. For each source project there's a separate test project. Build files go in the main project directory. I usually put a README there too documenting how to setup the project if it needs more than just checking out.
EDIT: This is the structure for a single working checkout of the project. It will be duplicated for each branch/tag in your revision control system (remember, in most SVN system, copies are cheap). The above example under Subversion would look like:
/project
/trunk
/src
/doc
/...
/branches
/feature1
/src
/doc
/...
/feature2
/src
/doc
/...
I found some old questions here on SO that might be interesting for you:
Whats a good standard code layout for a php application
Contains a link to an article on Scalable and Flexible Directory Structure for Web Applications (focus on PHP, though)
How to structure a java application, in other words: where do I put my classes?
Structure of Projects in Version Control
To expand on what Mendelt Siebenga suggested, I would also add a web directory (for JSP files, WEB-INF, web.xml, etc).
Tests should go in a folder named test that is a sibling of the main src folder - this way your unit test classes can have the same package name as the source code being tested (to ease with situations where you want to test protected methods or classes, for example... see the JUnit FAQ for this, and this question also on Where should I put my test files?).
I haven't had much use for it myself, but a Maven project will also create a resources folder alongside the src folder for non-source code that you want to package/deploy along with the main source code - things such as properties files, resources bundles, etc. Your mileage may vary on this one.
I use Eclipse for creating J2EE web applications and this will create the following project structure:
WebAppName\
\lib
\src
\tests
etc...
I would then create an SVN folder on our trunk called WebAppNameProject. Within this folder I would create folders called WebAppNameSource, Documentation etc. Within the WebAppNameSource folder I would place the project source generated by Eclipse. Thus I would have the following folder structure in SVN:
\svn\trunk\WebAppNameProject
\WebAppNameSource
\lib
\src
\tests
etc...
\Documentation
Hope this helps.
Related
I am new to Spring and just created a project using Spring CLI.
When I opened the project using STS i see the following folder structure.
There are two src folders.
What is the difference between them ? src/main/java and just src.
Also suppose I want to create a new folder like webapp under main, where should I create it ?
I looked a lot online but couldn't find any answers.
Please help.
There are two src folders
No, it's just one src folder and what you see is a way of displaying a Maven project under Package Explorer view of eclipse/STS. Change the view to Navigator and you find that it's just one src folder.
If you want to create a new folder "webapp", please don't. If you use Spring, you won't need it !
How you should read this project:
src/main/java is where you write your .java files that will go in production
src/main/resources is where you will put your *.properties file (mostly) and other files, that are not .java files.
src/test/java is where you write your .java file that will run tests against your src/main/java/**.java files.
target is where your java files will be compiled. You will find your .classes file, but also the files inside src/main/resources. Those files will be inside your .jar when you decide to create one.
You think you see 2 src folder, but in reality, it is your IDE that gives you this. If you go on your file system, I am sure you will have only one src folder. It is your IDE, thanks you maven and some default configuration, which "flags" different folder in category, to be able to compile and run your tests. There is 4 categories :
sources: src/main/java
resources: src/main/resources
Test sources: src/test/java
Test resources: src/test/resources (sometimes)
If you to build a webapp using Spring, go read this: https://www.baeldung.com/spring-mvc-tutorial. It is well explained, and you will be able to run a project in a short time.
I'm working on a framework-type project in Eclipse, that has a res folder attached with, as you guessed Resources inside of it. At the moment, they are crucial text-files that are to be read.
I also have a main test rig that is apart of it's own Eclipse project, as I plan to expand it in the future. This test rig has included the framework project on it's Build Path, so I can access classes from the framework as they are added.
But this is where my problems lie: If I want to import a class, that works fine. If I want to access a file through an InputStream, or Reader, it breaks. I know this is because the files included from the secondary project aren't placed inside the test rigs bin folder, rather they reside in their own bin in the other project.
Is there a way I can get my main project to access resources from the other without having to do ../../ trickery in my file paths? I plan on making this a real thing and don't want file paths doing things like that.
In eclipse do this,
Build Path ---> Configure Path ----> Click Project Tab ---> Add the project.
Also, you can package the other project in a jar and import it as an external jar.
Do anyone know what is wrong with this?:
-Launched Eclipse IDE(Java).
-Created new Java Project
-I got src folder (Source folder)
-Created a new Package in the src folder, and a java class in that Package
-And, I created a new source folder called Folder2
-And in Folder2 I created a new Package and Class file in it.
I tryed to connect these 2 classes from 2 different src folders, but the 2nd class is not found.
In the 1st class I used classTwo.main(null); , but the 2nd class from the 2nd source folder was not found.
I have tried same thing and its working for me.
See be tutorial Organizing sources
make sure setting is same as below:
When creating multiple source folders, and using the classes in them, you need to make sure that the source folders themselves are added to the Java Build Path settings in the project. To access these settings,
Right Click on Project Name in Package Explorer View -> Properties -> Build Path.
There you should see both of your folders shown as source folders.
This way, eclipse should take care of building the .java class files and generating the .class files into the build folder.
Another thing, I dont get why you are creating two different source folders for Java classes. The accepted and widely followed approach is to have multiple packages (even if there are hundreds of them) inside the src/main/java source folder for Java source files. The general project structure followed is:
src/main/java - Java Source packages with .java class files
src/main/resources - resource files like .properties, .xml files which are a part of the actual application code
src/test/java - Java Source packages for .java JUNIT tests.
src/test/resources - resource files needed for the execution of tests which are not part of the actual application running code. eg. specific setting files for JUNIT tests
I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.
Say I have developed a game, and placed it in the package structure:
com.dxmio.games.breakout
Where then is the 'best practice' place to put resources like audio and images that the game uses?
You can always adopt a standard Maven approach to your project and put all application source files in:
{home}/src/main/java/com/dmxio/games/breakout
and then your resources live in:
{home}/src/main/resources/com/dmxio/games/breakout
and your tests then live in:
{home}/src/test/java/com/dmxio/games/breakout
This structure is a standard convention to structuring projects in the Maven world. It brings a lot of advantages with it, which you may want to look at. You can check out the resources section here: Apache Maven Guides - How do I add resources to my JAR
Alternatively :) the other answer's approach here is just fine...
I have seen this handled in a number of different ways:
Place your resources directly in a subdirectory under com/dmxio/games/breakout (e.g. /com/dmxio/games/breakout/images/foo.gif and /com/dmxio/games/breakout/images/bar.gif)
Place your resources in a jar along with your class files (e.g. foo.gif and bar.gif bundled in breakout.jar)
Place your resources in a separate 'resources jar' in a subdirectory under com/dmxio/games/breakout (e.g. foo.gif and bar.gif bundled in /com/dmxio/games/breakout/images/images.jar)
I tend to favor the last option.
You can then use the java.lang.Class.getResource() method to retrieve your resources.