Indecision on which files to upload on GitHub - java

I have a java project made with Eclipse.
While working on a project, Eclipse creates a bunch of files and folders, what folder and files should I upload on GitHub?
I think that everything under the src folder should be uploaded. Am I right? Should I commit the .java or the .class files?

source control such as git are used to commit anything that can be called as source and not environment specific. So code, related resources should go but any IDE specific files.

Use gitignore either project specific or globally. The easiest way is to create a .gitignore file in your project root repository. For instance
# Eclipse
.classpath
.project
.settings/
# Intellij
.idea/
*.iml
*.iws
out/
artifacts/
# Mac
.DS_Store
# Maven
log/
target/

It depends on which files do you want to share too.
For example, in my current company almost all my colleagues and I use Eclipse, so we have repositoried .project" and .classpath too. It is very handy because changes in .classpath are done only once, and then propagated to all developers.
If your root folder is both the git repo root and your workspace, you won't probably want to upload the .metadata folder, since it contains settings specific to each different developer. And, by all means, you will want to ignore Eclipse's compiled directory (typically /bin).
Think about what do you want to share and/or version, and that will probably give you a list of things to upload/ignore.
Edit: as said before, upload ONLY .java files, .class files are products of your source code, and have to be generated, not stored.

Related

How to not push certain directories/files to Github?

Specifically for Java Eclipse projects.
Is there a reason to have anything other than src and lib directories on github??
How much value does providing /bin, /settings .classpath, .project, etc.??
I'd like to have them on my local, but not displayed on github. Is there a way to do this?
Thanks!
EDIT:
contents of my .gitignore file (which is located in my local git project directory):
bin/
.settings/
.classpath
.project
I did a git add (to add this new .gitignore file) and a git commit to my local repo.
However, when I push to my remote github (https://github.com/VKkaps/Breakout) now, I still see everything including the .gitignore file now? Help?
The answer is .gitignore file.
See: https://git-scm.com/docs/gitignore for more information.
If you don't want the .gitignore file in the repo another option is Git's exclude file. This file works similar to a .gitignore file but isn't committed to the repo.
From the Github docs:
You can use this technique for locally-generated files that you don't
expect other users to generate, such as files created by your editor.
Use your favorite text editor to open the file called
.git/info/exclude within the root of your Git repository. Any rule you
add here will not be checked in, and will only ignore files for your
local repository.
In Terminal, navigate to the location of your Git repository Using
your favorite text editor, open the file .git/info/exclude Add rules
to the exclude file as you would the .gitignore file

non build files of eclipse project in git repository

I have files that are associated with my Android project, that are not needed for the build. For example .svg files to generate icons, README file, apk files etc.
Since i would like these files to be part of my project's local git repository(and GitHub), where can i place them? If i place them inside the project folder, it shows up in the Project Explorer in Eclipse. I wonder if it then becomes part of the build and therefore the apk. Is there a convention for this?
If there are files/directories that you do not want to push to the repository, you should use the .gitignore file.
Create a .gitgnore file in the root directory of the git project. This is what a typical .gitignore file looks like in case of android.
# Built application files
*.apk
*.ap_
# Files for the Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
You can add options similar to these as required. E.g *.svg if you want to ignore svg files.
For files that are already added to your git repo, you'll need to use the git rm command to remove them from being tracked.
e.g git rm bin *.svg
Then use git add and commit as usual. All files matching the criteria in the .gitgnore file will not be tracked in any future commits.
Placing them in a subfolder in the project folder should work.
I have tested this by placing a subfolder svg with 19 .svg files totalling 100 kb in my project folder. The resulting .apk was the exact same size as before. So ADT's building process is intelligent enough to ignore miscellaneous folders.

Should Eclipse-specific files in an VCS be ignored, if using Maven?

I know why not to commit Eclipse/IDE-specific files into a VCS like Git (which I am actually using). That is one of the reasons I am using Maven and having it generating these files for you and not having them under version control.
But I wonder, if these files should be ignored in .gitignore which itself is under control of VCS/Git:
.classpath
.project
.settings/
target/
Is this okay?
What are the pros and cons since the .gitignore file itself becomes kind of IDE-specific as the files ignored there are IDE-spefific? Any best-practice?
With the team's I've worked on, the general rule has been that you don't check in anything that is generated by or obtained by Maven. Since the pom.xml contains everything you need to create the .project, .classpath, and .settings files, we don't check them in.
My .gitignore always contains .classpath, .settings, .project, and target for Maven projects.
Edit: As mentioned in my comment below, here is another suggestion: If you really want to avoid having Maven or IDE specific entries in your .gitignore, try writing your .gitignore so you list only what you DO want checked in.
*
!stuffIDoWantToCheckIn
I'm getting my information from the following article: https://help.github.com/articles/ignoring-files
That suggests that you can create a global gitignore file (suggest under ~/.gitignore_global) containing .project, etc. As the file is outside the repo, it won't show...
You register it as a global ignore file with the following command:
git config --global core.excludesfile ~/.gitignore_global
Alternatively, you can create a per-repo untracked gitignore entries in the .git/info/exlude file
I agree on not putting the IDE files under version control, this occasionally causes all sorts of pains, and as you mentioned using maven renders this unnecessary as any developer can simply import the project from the POM and get going
If these files are not put in the .gitignore they can easy be checked in by mistake
furthermore I do not find listing them in the .gitignore makes it IDE specific, you can list project files of eclipse, IntelliJ IDEA, and Netbeans, all in the same .gitignore if your team members use a mix of different IDEs. Over time you may accumulate a template .gitignore that ignores project files from all IDEs used in your team(s) to use whenever you create a new repository
If you are totally against putting these in the project .gitignore you can put them in the users .gitignore, but that in my mind is a bit looser as it depends on the individual development machines being configured correctly, and also these need to be maintained to be kept in sync with any new additions
Edit: I currently have an equivalent .hgignore, same concept different syntax, I converted it to git as an example of such a .gitignore file
/target/
/bin/
/build/
/.classpath
/.project
/.settings/
/.checkstyle
/atlassian-ide-plugin.xml
/.idea/
/*.iml
/*.ipr
/*.iws
*.orig
*.swp
*~
usually .project and .settings/ should likley be versioned and ignored!
.classpath and target should not be versioned but ignored.
This is a first inital boot-up on checkout-practice.
i.e.
You told everone to use four spaces as tab, this information is stored under .settings/xxx
but you give no restriction where they have to install their tomcat/jdk's (stored under .classpath)
ok?

How to prevent Eclipse from cleaning the bin folder for every build?

I am now doing a project using Eclipse, and I have some resource files (e.g., image, text) saved in the bin folder, and these files are needed by the program.
However, with every build, Eclipse would try to clean up the folder, then rebuild the project. When cleaning, it deletes the resource files in the folder. Is there anyway to stop Eclipse from doing this?
I know I could change the location of the files, but I am also curious why Eclipse would do this, and could this be prevented from happening.
Thanks!
Go to Options -> Java-> Compiler -> Building and uncheck Scrub output folders when cleaning projects.
That did the trick for me. In my project, I have an Ant task that adds a few configuration resources to the bin folder to include them in the classpath, without having them in src
I can't say exactly why it does it, but probably that's just how Eclipse does the build: empty the "output folder" and start compiling.
That said, if you put your files into a source folder, then Eclipse will simply copy the files over to bin on every build and they won't disappear. It will do this to any file it doesn't know how to compile, e.g. .xml, .xsd, .png, etc.
You can consider using a maven style project and add the resources to the resources folder.
Here is a link to maven directory layout.
What kind of project you are using in eclipse. You can turn off build automatically feature in the Project menu. Which would stop eclipse from cleaning up projects automatically.
Copy and paste your resources into the source folder. In eclipse, in package explorer, find your project, then paste into src. It then gives an option to copy the file or link to it. Click copy and it gets stored in /bin but won't get deleted.

Eclipse: copy the Java Build Path to a different workspace

In Eclipse how do I copy the Java Build Path to a different workspace?
Somebody somewhere wrote that if you copy the project then you have copied the build path also. This doesn't seem to be the case.
I've tried copying the entire workspace to a new directory. Opening Eclipse and pointing it to the new directory has all the projects and source files but the build path for my project is empty. It doesn't even have the basic Java source files to build against (java.lang.Object).
Eclipse 3.3.0 (Europa)
Any ideas?
The build path is stored in a file named .classpath in the project's root directory. I don't know of any different way, and copying the directory should also copy that file, of course. How do you copy the project? What OS?
Be very careful...
The workspace itself is just metadata in eclipse that points to your project folders. If the projects are not actually in the workspace directory, you aren't actually copying them, just references to them.
This can happen if you use "Import existing projects into workspace" that are folders in the file system without the "copy into workspace" checkbox checked, or if you create a project using an existing directory.
If you copy that workspace to a different machine, the projecs won't be there.
If you want to share projects, your best bet would be to use source/version control (subversion, for example) and have everyone hook up to the same repository.
Another note on the build paths -- if you have a java project reference an external jar, the absolute path of that jar is stored in the build path. This can be bad if other people who are sharing that project have the jar in a different location on their machine. If this happens, you should look into using Classpath variables or user libraries in eclipse.
Can you comment more on what you're attempting to do when you do the copy?

Categories

Resources