Extracting Common Java Package - java

I have a project that Contains a bunch of java packages that I have recently understood are needed in a new project that I'm going to develop. I want these two projects to have the same code base so I don't have to update the common libraries in both. How can I achieve this in Exlipse?
my current set up is like this
Project1
CommonPackage
SpecifictProjectCode1
Project2
SpecifictProjectCode2
I want the following, I think:
Project1
CommonPackage
SpecifictProjectCode1
Project2
CommonPackage
SpecifictProjectCode2
CommonCodeProject
What is the best way to achieve this I understand I could extract a jar file or and include it in both project but I want to be able to debug the code and I also want to keep the code editable. Is it perhaps better to extract the code to a separate project and how do I go about doing that?
Any help is appreciated.

You can have three different projects in Eclipse: CommonCodeProject, Project1 and Project2. Then, Project1 and Project2 would have a dependency on CommonCodeProject.
To add a dependency on a project in Eclipse, go to Java Build Path and add the CommonCodeProject in the Projects tab.

Create 3 distinct projects:
Project1
Project2
CommonCodeProject
Go into the eclipse properties of Project1 > java build path >
choose the projects tab and add CommonCodeProject project to the dependencies
do the same for Project2
both project should now have as dependency your CommonCodeProject.

YES. Create third project(CommonCodeProject) as Java project and reference that new project in first two projects by navigation below:
Project1 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
Project2 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
This should serve your need.

if you would not be editing the common code then make it one project and import as a jar in the other two. or even if you are editing have 3 separate projects and build common code to create a jar which is used in other two projects

Aside from native Eclipse (as outlined in the other answers), I would use Maven+Eclipse with the m2e plugin (download this using the Marketplace). Depending on the complexity of your project, and it's application, you could even go as far as hosting something like Sonatype's Nexus with your common code deployed to as repository - this might be overkill though.

Related

Combine multiple maven projects into single in Eclipse

I have one maven project with pom.xml.
This project refers another 2 projects.
These 2 projects have their own pom.xml in their respective folders.
So when I build base pom.xml other 2 projects also get built.
Now here is what I did
a) Build base project (This built 2 subprojects as well).
b) Used mvn eclipse:eclipse to export base project.
c) Imported base project into eclipse using Eclipse menu File-> Import existing Maven project.
After importing it created 3 different projects in Eclipse.
As methods for 3 projects get called from each other it gives many errors.
What I want is after importing it should create single project and all the code will be in a single project.
Is there any way to do this?
You could make a multi-module project. This will allow you to define a top-level project and the structure of it's dependencies. It will likely still require some refactoring, but it should be relatively modest.
Maven website: Guide to Working with Multiple Modules
A third party guide
Simple answer is NO.
Unless you manually merge/refactor these 3 projects into one.
pom.xml is per project, here there are 3 poms.

Is there a way to "import" from other on-disk Java projects in Eclipse?

I have two Java Projects in Eclipse, and I'd like to make one be able to access the classes in the other, without making it difficult to tell which classes came from which -- or, ideally, without having to list the classes in the accessing project. In other words, it looks like this (but with many more files):
Proj01
[package]
Useful.java
Proj02
[package]
CurrentProject.java
And I want to have some sort of equivalent of "import Proj01.Useful;" in CurrentProject.java. How would I do this?
Add the project as a dependency to the other.
Rightclick project -> Build Path -> Configure Build Path -> Projects Tab -> Add... -> Select the other project -> Click Ok.
You should now be able to import classes from the other project.
Essentially what you want to do is package 'Proj01' as a JAR file and include that JAR as a dependency in 'Proj02'. This will allow you to import the required class(es) in Proj02.
Personally, I'd recommend checking out Maven. Maven helps manage dependencies in your project and also the build process (which in your case can be used for building the necessary JAR file[s]).
Alternatively, if you're using a tool like Eclipse, you can use it to package up a JAR of your project.

Setting up common classes for access from other projects

I'm trying to set up two Java projects to have access to classes in another Java project.
So far, the situation is visible in the following image:
(source: shrani.si)
However, I can't seem to import any classes from the Common project.
(source: shrani.si)
I could just have multiple packages in a single project, but as far as I know only 1 jar can be compiled per project.
Any help is greatly appreciated.
You need to add your "Common" project to libraries of project where you need to use classes from "Common" project.
For example right-click on "Clinet" project then "Properties"
In right menu choose "Libraries" and press button "Add Project..." and double-click on project "Common".
After that you can add import common.Item;
Also try to clean and build all your projects.

Dependency between two classes

Is it possible to create something like "dependency" between two classes from different projects in one Eclipse. The point is that in both of this projects I need to have exactly the same class. I'd like to do something which allow me to make a changes in one of them and all the changes will automatically put in second. I hope that I explained it well ;)
I would-
Create a core project
Add core project to Project A
Add core project to Project B
Pretty much like how we use external libraries.
Create your common classes in a separate project. In eclipse set your build path for the other two projects to depend on your common project. When you release your projects, you build your common project as a separate jar and add it to the class path of your other projects.

Adding custom maven project as dependency

I'm using eclipse with the m2eclipse plugin now I just want to resolve a - imho - easy problem: I've got two maven projects, I want to add project A as dependency to project B.
Well how do I achieve this in a manner way? If I add the project A to the build path of project B eclipse recognizes the classes but this project isn't resolved by eclipse on build time.
I got it working by installing project A to my local repo and adding this as dependency to my pom. This works but is cumbersome because I always have to install a new version of project A when something changed.
Shouldn't the plugin handle such a situation for me?
Providing that you have both maven projects open in your workbench then make sure you have the "Enable Workspace Resolution" option enabled in the Maven context menu.
Yes Eclipse handle this situations.
You can add both the Projects A and B in the same work-space.
I could help you creating a simple work-space from the scratch.
I am assuming that you have already installed the MAVEN plugin M2Eclipse for Eclipse.
Start a new Eclipse in a blank Work-space
Right click on Project Explorer --> Go to Import dialog and add a Maven Module.
Locate the POM directory and add that directory.
It will list all the projects in all the sub folders.
Add as many as Maven Based modules in a single work-space.
By doing this you dont need to install the dependencies. Any change will be reflected on the derived module.
Hope that will help you.
The way you did it is correct, because it assumes that project B will be using the dependency of project A that will be in the artifactory, so you can develop both independently.
And anyway, for the project A, if you are using maven, don't you use maven clean install for compiling and deploying? That way you are sure you always have the latest version
The other option is, in case both of the are more dependent of each other, you should consider make one of them as a module of the other, or maybe make a project C that contains both modules, but that would mean both of them are part of the same project (like an ear containing two jars), depends on the situation

Categories

Resources