Linking a project as a library to other projects - java

I am new to Java and have been learn as I go.
I am having a problem where I have developed a library of objects in one project and I now want to use them in two other projects. I started doing this by copying the packages from the first project into the second and third projects, however I would now like to make changes to the library and I would like these to be reflected in the second and third project, which obviously they are not.
Project1
-package1
-package2
-package3
Project2
-packageA
-packageB
-package1
-package2
-package3
Project3
-packageC
-packageD
-package1
-package2
-package3
I would like for the packages to be in the second and third project to be updated automatically when I update them in the first project, although I also don't mind doing the refresh manually. At the moment it is just me working on all off the projects but it could be the case in the future where more people are working on them, therefore I don't mind suggestion which would have this move onto some collaborative platform (I'm aware of SVN but have never set one up)

In eclipse you can do this as follows.
Import your project to your workspace.
Mark it as library project
Right click your project you want to make as library and select Properties. Select Android on the left and tick the checkbox IsLibrary. Done.
Reference the marked project
Right click your project in which you want to add the library project and select Properties. Select Android on the left and Add... your marked project. It will be added to the list and is ready to use. Now you are able to access all classes and ressources (e.g. drawables, strings) from the referenced, marked project. Awesome, eh? :)

Related

How do I run a gitlab / github cloned project in eclipse? (When it is not an eclipse project)

Wanted to get some oppinions here. I've asked a lot of friends for an answer on this and no-one havs ever gotten a good solution to it. I recently cloned a java project from gitlab and have no clue how to run it. I had to import it as a general project because its not an actual eclipse project. But then I have no clue how on earth it was uploaded in this file structure in the first place?
In case of very simple project, often you can right click on the source folder (could be more than one) and select "use as source", then you need to add all of the libraries by right cliking on them and using "add to build path".
If it is other kind of project like maven, gradle etc... they have own tasks to eclipse-ize the project, check documentation.
There is no single and straightforward way because there is nothing like one and only valid standard of project structure and/or dependency management.

How am I supposed to work with multi-module Maven projects with EGit?

I tried my best to get a multi-module Maven project synced with GitHub using EGit, but I've failed.
The structure of the project is like this:
parent (pom)
child-one (jar)
child-two (jar)
I tried this simple approach:
Completely blank copy of Eclipse JDT and an empty workspace
Create a new GitHub repository and initialize it with a README.md file
Clone that repository to Eclipse
Create the parent (pom) project
Create two children (jar) projects, add simple main classes, everything works at this point
Now comes the tricky part. Do I share just my parent project? Or children as well? Well, it seemed logical to me to share the parent only. However when I did, my build path went completely crazy and Eclipse could no longer find main classes. Then I restarted Eclipse. Issues persisted. Then I "disconnected" the project with the repository and logically EGit randomly deleted one of the children on the file system, and made the Eclipse project completely unusable (no classes would display, all structure was gone).
I previosly had an episode where EGit randomly deleted one of my repositories and that time it wasn't just a test, it was my actual latest changes to the codebase.
Is this plugging completely bugged out or am I missing something?
(I followed the basic tutorial on setting it all up, and have my SSH keys configured properly.)
What should I do to properly push a local Maven multi-module project to GitHub, and be able to clone it on a completely different PC out of nothing?
Actually it's 100% possible to do this!
Here's an even simpler approach than before:
Clone your repository
When creating a new Maven project, don't place it in the default Workspace location, place it in the *subdirectory** of the repository
Done! You can create as many sub-modules as you'd like, Eclipse has automatically detected your Git repository.
* the subdirectory is automatically created for you when you choose the default location, and it is named after your module. It's not automatically created when choosing a different location (which is a bit inconsistent, but makes sense). If you just choose your repo as your location, Eclipse/Maven will then generate all the top-level project files and directories (pom.xml, .project, src/, .settings, ...) in your repo's root directory - which might be what you wanted, but most likely isn't.
For a more complicated explanation of why it wasn't working, read on...
It's just a bit complicated and very unintuitive. Here's a picture of how it looks:
Steps I took (make sure to backup anything important):
Create a new repository on GitHub (with README)
Install Eclipse JDT (the "Java" download, with Maven and Egit preinstalled, but it would work in any Eclipse version I presume)
Clone the repository in Eclipse. At this time, there are no projects to import or anything, there's just the repository. To clone, go to Window -> Open perspective -> Other -> Git Repository Exploring
Create your projects in Eclipse using Maven. I first created one child and one parent. The parent has to have packaging type of pom. The child can have it as a jar and has to be a Maven module, not a project (but it's essentially the same thing). At this point I created a simple main class in this child module, and saved everything
Share your parent project to the repository you cloned before (right click on parent -> Team -> Share project -> Git -> select your repository). Now, things will start falling apart, but this is normal. Wait for Eclipse to finish. What happened is that Eclipse moved the project from the place you saved it previously (for me it was my workspace) to the git repository - and since the parent has the children inside it, it also moved the children repositories with it. However, it didn't update its definitions accordingly, so the child project is now just shown as an empty directory. You can commit now
Delete the missing child project (not the parent!)
Just import the projects again
Sadly, Eclipse does not really support what you are trying to achieve because it assumes the project hierarchy is single-level; while in your case the parent project contains the child projects as well. For details see this long bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=35973 (at the end there are some nice summary comment about the status).
As workaround I can suggest two things:
Do not import your parent project into Eclipse.
Put your parent project as a simple project in the hierarchy, where child projects are referenced with hierarchic paths
The projects I am involved in mostly follow the second approach (see e.g. the EMF-IncQuery project, where the parent is stored in releng/org.eclipse.incquery.parent in http://git.eclipse.org/c/incquery/org.eclipse.incquery.git/tree/), but if your parent pom rarely changes, the first option could work well as well.
Note that all the trouble and distress discussed here is specific to using the 'sharing' features of Eclipse in which the code is directly checked in and out of the workspace from source control. You don't have to use those features -- all of this works perfectly, with no magic and no stress, if you do not.
If you do a plain old git (or svn (or whatever)) checkout outside your workspace, and then use Eclipse's feature of opening an existing project leaving its source outside the workspace, everything will be fine. You will still have VCS operations in the resource tree, you can still commit or merge from inside Eclipse if you like, and your multi-module-Maven project will simply work with no special incantations.

Call three projects from onther project,Java

I want to ask something. I have done three small projects on Java at Netbeans. Can I make a project which it calls theese three projects ? I want to have all theese projects in one programm. Thank you !
Basically, yes.
Expand the "master" project and right click on the "Libraries" node.
From the popup, select "Add Project". Browse to the location of the project you want to link and select "Add Project JAR Files".
The project will now be linked to your project and you should be able to access it's classes.
Note, if the child project has any dependencies of it's own, you will need to add those to the "master" project as well.
You can either add those projects into your new program build path or make the libraries and use it.

Package projects in eclipse

Lets say I have n projects in eclipse and they are only conceptually connected.
How to add every one of these projects as a package in a new umbrella project?
In Eclipse you can hook projects together using the Project tab in your build path configuration dialog:
right-click on your higher-order project
select Build Path
select Configure Build Path...
select the Projects tab and add the other projects as references
You might also look into Maven as a build management system, which would allow you to create consumable libraries. Each of your projects would use Maven to generate a JAR artifact, and your umbrella project would use Maven to reference them as dependencies. I believe Ivy would also help you in this regard.
In order to logically combine the elements (in this case, projects) together, you can create a "Working Set" in Eclipse. There can be different type of working set.
Here are steps to create a Java working set (as the question is tagged as Java):
File - New - Others..
Select "Java Working Set" under Java.
Select the project you want to add into the working set and give a
Once the working set is created, you can use it at many place in eclipse. Personally I use them a lot in search to narrow the search results and on Package/Project Explorer to see only the projects I am interested in at the moment.
To emphasize that the Java working sets are just logical grouping of the projects, here are couple of examples.
Working sets "Batch Projects" and "Web Projects" based on type.
Working sets "SVN Projects" and "CVS Projects" based on the source repository.
Read more on the offical eclipse documentation.

How do I manage git submodule subprojects in Eclipse?

(In case it matters, these are Java Android projects)
Project A is a library project.
Project B depends on A. For ease of project management across the board for other people instead of just me, A is contained within B via a git submodule.
This means, if I want to make changes to both A and B, I will have the following:
Project A itself
Project B
B's submodule of A, let's call it A'. Same code, same project name, different location on disk.
Ideally I would like to be able to meet the following conditions:
Be able to modify A itself directly within Eclipse.
Be able to modify B and have it build, referencing A', all within Eclipse.
I don't really need A' as an imported project / no need to modify it, but I'd like it to automatically build its jar file and have that be referenced by B.
What I've tried:
Simply importing both A and A': Doesn't work, because Eclipse cannot import two projects with the same name. I can't simply change the name either because it's a submodule; would have to do that every git update, unacceptable.
Adding External Library reference to A', keeping it out of Eclipse entirely: actually not too bad of a solution, but requires at least one external build of it from the command line (using ant) before it will work...not the most convenient.
Doing a Source Link: Not the cleanest because A (and subsequently A') have multiple source folders within them, so I'd have to update each time I add or remove source folders within A. I'd rather just use the jar directly.
Is there a way to keep this all in Eclipse, or should I do something like #2 but with an added external build command to call ant on A'?
I have a similar situation. Import B as existing (android) project into eclipse after you git clone it. Make sure project A is initialized and updated (via git).
Now, do file->import existing project (android if A is an Android project) and go to where the submodule A project is inside of your project B and add it.
Now you have two separate projects in eclipse. You can change both projects, and it will all be under Project B, because that is where both projects are located.
Since this is specifically for Android, have you looked into Android libraries?
In my case I have a free (B) and a paid for (C) version of my app. Both share a common code base (A).
In eclipse I make A an Android library and both B and C use this library and all 3 are separate git repos. With this setup, changes I make in A are automatically available in B and C and I only need to maintain one code base for each project. No fancy build scripts/configs.
I think this sort of simplicity is what you were after? Apologies if I have misunderstood the question.
I'm not sure if i got your setup right:
Project A : src
+ /projecta.jar (binary of project A)
Project B : /src
+ /lib/Project A/src as submodule
+ /lib/projecta.jar (binary of project A)
..now you change A-A' and compile it, wanting to test it before pushing/commiting, right?
is it ok for you to just have the jar in B, or do you need the source within project B updated to A'?
first case:
have an ant file compile in project A and deploy it to B as well
that ant file is not part of project a, its just a local workaround
(its useful to have it in the ant view of eclipse)
Sketch:
<property name="projectb.libdir" value="..."/> set appropriate place in project B
<javac fromdir="./src" todir="./build" ...><fileset ... /></javac>
<jar /> from build to projecta.jar
<copy /> projecta.jar to projectb.libdir
Alternative is to create a .jardesc in eclipse, so you can run "create jar" on rightclick.
is that what you wanted to do?
Eclipse Mars Milestone 5 (4.5.0M5) has Multiproject-support. You only need to import "Existing Project" from Subfolder.
Just use a project build system to track your dependencies (that is what they are good for).
I would you suggest to use gradle (preferable IMHO but check which one fits better on your context) or maven, both can handle subprojects pretty well and can generate the eclipse configuration for you.

Categories