Dependency between two classes - java

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.

Related

Java project structure: module dependencies in sources vs binaries

I've seen projects where one single project divided into modules, and each module is maven project itself. These modules integrated through one module which contains references to all other modules. To launch project user must import all modules in IDE. So, why people using this approach? Isn't much easier to package all modules to jar and include as dependencies to some module? Is there any benefit to use projects instead jars? Drawbacks of using projects are: user needs to keep all modules in IDE, may accidentally change source code, and if IDE starts to compile all that modules it takes a lot of time.
If you accidentally change one file, only this file gets recompiled, that's not a big deal.
Usually, you need to modularize your project to cut interdependencies and make everything more controllable. Often, the parent project doesn't even have any source files of its own; instead, it is only used to aggregate the modules. Modules are just used here to separate a big project into pieces.
You could develop those pieces as separate projects, but to implement a change in one module and make it available for other modules that use it, you'd have to build that module and actualize a dependency in the client module. That's cumbersome. It's much more practical to keep them as one big project where you just change the code you need to change, and it's available to all the modules that depend on it.

How create properties (variables) in sbt which are reusable across builds?

With maven I can create a parent pom.xml and extends it everywhere. Also in parent pom I can define some general properties like library version and other properties which are wanted to share across several imdepeneded projects. How to do this with sbt if projects are different and they do not build from one root project/folder?
Important
The question is not about multi project build!
Not tested, but should work:
Create a normal SBT project, define your desired settings, etc. there. Let's say it's in the path ~/parent.
Note that the project folder of an SBT project is itself a project.
You can depend on external projects from file system or VCS in SBT. In the builds where you want to reuse parent project, create project/project/Build.scala (or project/build.sbt) if it doesn't exist yet, define the project as usual and and add a dependency on ProjectRef(file("~/parent"), "project").

Maven / eclipse project structure

I am still fairly new to Maven, I finally have it how I want but now I need to break it all over again.
Here is my scenario:
I need to write two different server applications, which use identical core functionality; just, what is done with that framework is very different. One server application is very easy/simple - it's already done - whereas the other is a lot more complicated.
The code is written in a dependency injection style (using Guice, if it matters), so it should be extremely easy to break apart.
My question is this: how would you structure the projects in Eclipse, using Maven? Would you set up three different projects, something like:
server-core
server-appEasy
server-appComplicated
where each server would have it's own pom. Or, would you keep it all in one project? I need to be able to easily recompile appEasy in, say, a month from now, while I work on appComplicated. The classes for appEasy are already in a subpackage. Note: core would not work by itself without at least a mock dependency injection. It doesn't have a main class.
All thoughts appreciated, even on things I haven't thought of.
I would have a structure like this:
/server
/server-core
pom.xml
/server-appeasy
pom.xml
/server-appcomplicated
pom.xml
pom.xml
So each project has its own pom.xml that allows you to build that project in isolation.
However the parent folder also has a pom.xml, which will build all the projects if run. You can do this by including the projects as modules in the parent pom.
E.g. In the parent pom.xml
<modules>
<module>server-core</module>
<module>server-appeasy</module>
<module>server-appcomplicated</module>
</modules>
You can also use managed dependencies in the parent pom tio allow you to centralise external dependency and plugin version numbers.
I would suggest to structure all as a Maven Multi Module project.
The parent project, would have the three projects as modules, the 3th party dependency versions, and the version of your project as a property.
Then, in the server-appComplicated and server-appEasy I would add a dependecy to the server-core.
In this way you will gain:
1- A root project to compile (the parent), that it will generate the two servers and the core-lib.
2- A point where to handle the version numbers and the common dependencies.
I hope it helps
Im not a maven expert but here is my 2 cents.
Each project needs its own pom.
Do you need to build all the projects together? In that case it might make sense to have a parent pom , which has all the common dependencies.
EDIT: In that case, I feel just have three separate 3 pom files for each project.
There are multiple ways to do this, depending on how you need it when it comes to deployment. Assuming that 'server-core' is a shared artifacts among your 'server-appEasy' and 'server-appComplicated' artifacts, I would suggest something as below
Create a Maven Project 'server-core'
Add two module projects
by name 'server-appEasy' and 'server-appComplicated'
Make sure the module projects have their parent set as the 'server-core'
In the end you should have three projects (each has separate pom.xml), where
a. Building 'server-core' will also build the modules
b. Building either of the 'easy' and 'complicated' modules independently on need basis will also build the server-core.
Hope this helps!

Extracting Common Java Package

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.

java world terminology

I come from the .NET world, so I will ask in .NET terms in order to understand what is the java-world terms. I have experience with java.
I need to create asolution with two projects: library project and web application.
The library project uses hibernate and the web application should have reference to the library application.
I am using intellij and I saw there are various things like project and module. I don't know this terminology - can anyone explain me what king of 'projects' I need?
I saw that if I create a new project while working on project it opens a new window.
While I add a new module it adds the module project in the big project.
The terminology is IntelliJ's rather than a Java standard. (Other IDEs use similar terminology)
There is not much more to projects and modules than you have realised already. A single projects which is opened at once contains several modules which break up your work logically. A single modules can appear in multiple projects, but this can be more confusing than useful.
You can have one project with one module which is the simplest way to start.
You may want to look at using maven as this a portable way to manage your dependencies and build your project (all IDEs support maven and it can be run stand alone)
Even though I am a heavy IntelliJ IDEA user I always start from maven project, which I then import to IDE.
In maven you create one parent project (with pom packaging) and two submodules: one with war packaging and dependent one with jar (default). After importing you'll see two modules in IntelliJ.

Categories

Resources