How to share code between two projects? - java

I have two Java projects called A and B. Both of them are web apps deployed as war files. I created them in Eclipse workspace separately. Project B uses a class in project A named MusicMapper. I added the project A to project B's build path in Eclipse as suggested in this post. So now project B can compile without any errors and the class MusicMapper can be seen in project B by importing it in project B:
import com.projectA.MusicMapper;
Everything seems to be fine before I launched the web app of project B. However, when I launched the project B and called the code that references class MusicMapper in project A, I got the following runtime error:
java.lang.ClassNotFoundException: com.projectA.MusicMapper
So this error seems to be caused by unfound class com.projectA.MusicMapper which is imported from project A. Since I already added project A to project B build path and project B compiles fine without any errors, why does it report this error at runtime?
Another approach I took was: I've also tried using the following Maven import in project B's pom.xml:
<dependency>
<groupId>com.projectA</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>/path/to/projectA.jar</systemPath>
</dependency>
where projectA.jar is the jar file I exported from project A. But it still gave me the same ClassNotFoundException. How can I make the class in project A usable by project B? It seems that neither of the two approaches I've tried works.

First of all if you have two projects which are both deployed as wars it's not a good idea to include one project into another as a dependency. Because you will be puling in lot of other stuff that you don't need in project B.
A better approach will be to create a Third java project lets say "Common" this should be just a java project and NOT a Dynamic Web Project and should compile as a jar. Move all the stuff that is shared between Project A and Project B into Common project.
Now back your problem Just adding some thing in project build path in eclipse does not mean you have added the dependency to your project outside of eclipse as well. Eclipse don't complain because it can resolve and find the project because you added in eclipse build path. You will have to compile common dependency project using some build tool like ant, maven or gradle and package the jar in your war file when war is built.
If you don't want to use a build tool a simple route would be just export the third project from eclipse as jar. Copy the common jar file in WEB-INF/lib folder of your Project A and Project B before you export the war file from your eclipse.
I hope it helps :)

Related

Intellij : import project as library for another project

I am developing a project A (Java Maven) which uses a library B (a JAR file)
I have the source code of library B, and I want to change code in project B, while it's used as library for project A.
Is that possible in Intellij to replace a JAR library by its source code ?
I'd configure a multi-module Maven project with the parent aggregate pom.xml and 2 sub-modules: for the app and for the library with their own pom.xml files.
Define a dependency on the lib module for the app module. IDE will resolve such dependencies via the sources and you will see the changes in the project instantly without the need to compile. Refactorings will also work across the modules so that you can rename a method from the usage in the app and it will automatically rename it in the lib.

Eclipse - Project Referencing another Project instead of .jar File

I have a Java project in eclipse:
Project A contains an Ivy dependency on Project B.
Locally, I checked out and made changes to Project B. I then wanted to test the new functionality from Project B in Project A, so I added Project B as a project dependency of Project A by adding it to the Java Build Path (Java Build Path->Projects->Add...).
I no longer wish to have that dependency on Project A, but simply removing it from the Projects Build Path hasn't worked. With that dependency removed, I would expect that any references to the new local code would result in a syntax error, since those changes have not been incorporated into a new artifact for Ivy to pull in.
How can I remove any references to Project B from Project A? I've tried deleting both projects and re-checking them out and that did not work.
The way to resolve this issue is to:
Close Project B in eclipse.
On Project A run Project-Clean
Resolve Ivy dependencies on Project A
Now the dependencies from Project B should use the Ivy artifact rather than the local changes.

How do I run a java class, using Intellij, that needs configuration files from another project/module?

I'm trying to replicate in Intellij something easily done in Eclipse.
I want to run this Main class from the mainProject (nevermind the errors due to the use of fictional names):
Now, the problem is I need to be able to import a configuration folder from another project, in order to run the Main class. This is easy in Eclipse:
But I don't know how to do this is Intellij. Ideas?
This is actually why I despise the Eclipse workspace. It lets developers cheat and use another project's source as a dependency of another project. This is a problem because this isn't how it works outside of the IDE.
What you need to do is create a jar of classes you depend on, then include that jar as a dependency of the project that depends on them. If you use dependency management and have a local repository (like nexus or artifactory) you can publish your jar to your local repository and then in your other project just include it in your pom.xml if you are using Maven or build.gradle if you are using Gradle.
If you are instead including libraries in your source folder, copy the jar to your project, then right click on the jar in IntelliJ and select "Add as Library...". You can also add a dependency through File->Project Structure->Modules->Dependencies tab. Add as Library is a shortcut to adding a library here and the dependency shows up here if you use Add as Library.
IntelliJ does let you import a module from another project, but again this is cheating because it will just confuse you down the road because it will only work from within the IDE, not running as a standalone application. (File->Project Structure->Modules->Plus (+) Sign->Import Module)

How to build Maven dependencies without a parent project in NetBeans

I'm using NetBeans 7.4 RC1 to create Maven projects.
Let's say I create two Maven projects, named A and B, and a third named X which includes the other two projects as dependencies.
On building X Maven searches the local Maven repository (and the connected company-proxy) for the dependencies and obviously does not find any initially.
If I run the build command of NetBeans on projects A and B, it performs a maven install, which then installs the projects into my local repository. Now, when I build X it will find the dependency and all is well.
Is there a way to configure Maven (or NetBeans) to automatically build the dependencies (and install it to my local repository)? I know there's a solution to do this with POM Projects (Reactor POM) where the modules (in this case A, B and X I suppose) can be added to it, and the POM project specified as a parent in all the modules - this will allow me to select the "Build with Dependencies" option, which will then build all the modules in the POM project. But this is not a good solution if say project A is a utility project, used in many other projects.
Essentially what I'd like to avoid is building all the dependencies for a project every time I want to test my project; especially when I'm working on the project and its dependencies at the same time.
The key is to try and to this in NetBeans itself, and without modules, if it's possible.
I hope I'm missing the point.
With Compile on Save turned on, Netbeans will attempt to link the opened projects together no matter what reactor they belong to. However this will not build the projects (eg. doesn't create an updated jar file in local repository), just link the current project's maven build to the target/classes of these projects.

Maven, GWT, Google Eclipse plugin, can not copy jar to WEB-INF/lib when dependency project is war

I have a maven project, project A depends on project B, A is a GWT web project, in project A's pom:
<dependency>
<groupId>com.mydomain</groupId>
<artifactId>b</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
and B project's packaging is jar. Everything works fine till now. But i need to add some integrated test on project B, which need to turn project B's packaging to war, so it can setup a web environment that i can run the integrated test cases.
Then when i run/debug project A using Google Eclipse plugin, project B's jar never copied to project A's target/A-0.0.1-SNAPSHOT/WEB-INF/lib, and runtime class not foundexception thrown.
My questions is how to solve problem like this, i need the integrated testcases in B and i also would like to debug in project A.
Any help are appreciated.
hmm, not sure I fully understand-- but I would suggest packaging B into jar to be used by A, and then independently package and deploy B as a WAR.
This separation will allow both A and B.WAR to depend on B.jar as opposed to A depending on B.WAR.
After reading more carefully about http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html and http://maven.apache.org/plugins/maven-war-plugin/plugin-info.html
I thought i have come up with a solution: leave project B as jar packaging as it should be, then bind maven-war-plugin's exploded goal to maven package lifecycle, then when installing project B, it create a exploded war directory just before running integrated tests even it is not a war project. And also thanks to the people who give solutions.

Categories