Intellij : import project as library for another project - java

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.

Related

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)

Include another project using Gradle? [duplicate]

I created a library project in Android Studio (currently 0.5.2) by choosing File > New Project... > "Mark this project as a library".
I have two other non-library projects that I would like to add a dependency to this library project.
-My Library
-Project 1 (depends on My Library)
-Project 2 (depends on My Library)
My goal is to keep each project independent and avoid duplicating modules/code. How can this be done without copying the library module into the other projects?
Update:
Android Studio 0.6.0 allows you to Import a module, though, this simply copies the module source into the Project.
You can also refer to a library outside of your project folder using the project().projectDir property. If your external library is relative to your project like so
- MyLibrary
- library
- MyProject
- app
in MyProject/settings.gradle
include ':library'
project(':library').projectDir = new File(settingsDir, '../MyLibrary/library')
in MyProject/app/build.gradle
dependencies {
compile project(':library')
}
This is very similar to this question:
Sharing an Android library between multiple Android apps using Gradle
Instead of pushing to maven central you can push to your local maven repository (mavenLocal() in build.gradle)
Another route (if you don't want to deploy the library somewhere) is to use your VCS and check out the library within your project. Git has submodules for that, Mercurial has subrepos and SVN has external to name a few examples.
Then add it to your Gradle build using a project dependency.

In Eclipse, in a dynamic web project that is dependent on a maven project, how do I pick up jars from the maven project?

In Eclipse, I have two projects:
archangel.core - a Maven project
ArchangelWEB - a Dynamic Web Project (built for Tomcat).
The first, archangel.core has all of the base code and uses Maven to resolve dependencies. The second is the Web addition on top of the core project. This only has code specific to presentation/view. I want to keep them separate because I may have other projects in the future that will rely on the core, and I don't want the core project to have Web Library dependencies.
Right now, in ArchangelWEB's build path, I have the archangel.core project, and I also have archangel.core in its Deployment Assembly. This allows me to refer to code from archangel.core in ArchangelWEB without any build or runtime problems.
One of archangel.core's dependencies is apache-commons-lang. Within archangel.core, I can reference classes like ExceptionUtils. However, I cannot automatically reference this jar dependency from ArchangelWEB. If I try to import class from apache-commons-lang in ArchangelWEB, it doesn't know what I am talking about.
What is the best way to import/reference dependency jars from a maven project into this other project (my Dynamic Web Project), which uses the maven project as a dependency?
Dynamic Web Projects resolves dependency only by manually placing the Jars in the WEB-INF/libs folder. There is no other way.
So you need to place the dependent jars of your Dynamic Web Project into the libs folder manually.
Don't forget to do Right-click the Jars in Lib -> Add to Build path after adding them
This is why we use maven these days instead of the old Dynamic Web Projects.

How do I add a java gradle submodule to my Android module?

Suppose I have an Android application project in IntelliJ Idea with Gradle. Call it MyApplication.
What I want to do is to add a plain java library module (not Android library project) as a submodule to MyApplication. Lets call this module a testlib.
And then when whole project is built, I want this module compiled to jar and included to /libs folder of MyApplication.
What I tried: I've created a submodule testlib and included it as a dependency to MyApplication, but I get a following warning:
Warning:Gradle: module 'testlib' won't be compiled. Unfortunately you
can't have non-Gradle Java module and Android-Gradle module in one
project.
Is this even possible?
If you've created your project in IntelliJ as an Android-gradle project, then when you go to the "Add new module" window you should see an "Gradle: Java library" option like this:
IntelliJ IDEA does not support Gradle and Non-Gradle modules in one single project. A comment from JetBrains in their forum states this clearly.
The solution is to either convert the Maven modules into Gradle
modules or split the project in two. With the latter approach, if you
build the plain java library module with Maven before building the
Gradle project, you will still be able to use it as a dependency in
the Android application project.
Beware of using different Java language source levels in multi-module
Gradle projects. IntelliJ IDEA does not handle them correctly due to a
bug in either IntelliJ IDEA or Gradle. (There is a pending discussion
whether this should be fixed in the IDE or Gradle.)

How to share code between two projects?

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 :)

Categories