What is the difference between "Module Dependencies" and "Libraries" in IntelliJ IDEA? - java

What is the difference between "Module Dependencies" and "Libraries" in IntelliJ IDEA when you want to add a .jar library to your project? Also, What is the "Export" check box when you are adding your .jar library to the "Module Dependencies" in IntelliJ IDEA?
In each of these ways, how are the classes and code inside the included .jar library integrated into your final project (code) when creating the newly generated .jar file?

Module dependencies are classes, archives, libraries and resources that your module files references. While a library is a set of class files stored in an archive or directory.
Export check means if checked then this library will be implicitly added to the other module that references this one.
To create a .jar file you need create an artifact. Artifact is a placeholder of the building output. There's predefined templates for creating .jar, .war, .ear archives. You can choose jar to build a jar artifact. By default it's defined empty and you need to define content of the artifact. You can drag-n-drop compiled output to it but don't do it with library archives. Because libraries in this case will be packaged inside the .jar file and you will be required to create a separate classloader to load them before your application start. Instead you change the artifact type to Other and drag .jar and dependent libraries into output root. This way library archives will be copied along with created .jar. You also need to create a MANIFEST.MF and specify Class-Path there for dependent libraries. All files will be stored in the directory you specify for building the artifact. You can build it using Build Artifact menu.

If your project contains multiple modules, "module dependency" defines dependencies between these modules, but libraries are compiled classes (usually jar files, optionaly containing theirs sources and javadocs) that are used by your module.
Each module can have its own libraries and artifacts (for example a result jar file), and can depend on other modules without circular dependency.

Module Dependencies tab can contain Libraries, Export means that a library from the module will be also available to another module that depends on this module.
Final jar with all the dependencies can be created using Artifacts.

"In IntelliJ IDEA, libraries can be defined at three levels: global (available for many projects), project (available for all modules within a project), and module (available for one module)."
Global library is set via Project Structure\Platform Settings\Global Libraries
Project library is set via Project Structure\Project Settings\Libraries
Module library is set via Project Structure\Project Settings\Modules\Dependencies

Related

Create JAR file using Net Beans including all dependent libs and folders

I have created a JavaFX application using NetBeans IDE and below is my folder structure.
I want to a build a single jar file including all dependencies for this jar to work properly.
This jar requires testplanner and batch folder from project root directory and files inside dist folder to work properly.
How can I package all this to a single jar file?
Theoretically JAR files cannot contain dependencies within, as java does not support it out of the box. WAR and EAR archives can. What You want to do is not standard, but is named fat jar. Fat jars are used i.e. by spring-boot maven plugin, but you could try this:
https://dzone.com/articles/how-build-fat-jar-using
And some more explanation:
NetBeans - deploying all in one jar
Use tecreations Deploy. Put all your sources into a path declared as Tecreations.getProjectPath(), run BuildAll to create your corresponding classes, put your jars in projectpath/jars and select the appropriate settings, whether to include jars, sources or classes. Select your main class and click Deploy. Unsigned and signed output jars are produced in user/Documents.
Download: https://tecreations.ca/java/downloads/release.

How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.
2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository
3) When I unzipped my .JAR, I only saw my single class that I created custom.
This brings up two questions:
1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?
and
2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?
Thanks
SOLUTION:
The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.
I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.
No, they are never included (unless you use a special plugin which does that).
See 1.
If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.
For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

how to refer java web project library from current project?

I have a web project Web1.war which have some jar file in its libraries (let's call it lib.jar). That is Web1.war --> lib.jar.
Then I did convert to jar file that web project: Web1.jar. Afterwards add the Web1.jar file to another java web project Web2.war.
How to refer that library lib.jar from current project (Web2.war) through jar file (Web2.war --> Web1.jar --> lib.jar) ?
Java only supports JAR level dependencies. You mention having converted you 'web project' to a JAR but don't give the details of how you did this.
By default, Java uses the classpath to find dependencies. In a 'web project' (WAR) the classpath is the WEB-INF/classes directory and any JARs in the WEB-INF/lib.
If you have classes in one 'web project' (WAR) that you need to use in another 'web project' (WAR) then you have a couple of options, both of which will involve modifying your build:
put shared classes are in their own jar, which you include in both 'web projects' (WARs).
or
unpack the first 'web project' (WAR) adding the classes (and/or libraries) to the second 'web project' (WAR).
Update
You cannot covert a WAR to a JAR as you have suggested in your edits. While it's possible to create a custom archive format, a JAR cannot (and should not) contain other JARs.

Creating jar issue

This question may be theoretical but I could not find any proper solution.
Suppose I am making a module which uses 3 jar file(hibernate,log4j,jackson).
Now I want to compile my module and create a jar such that my module can be used by any other module and that module should not require the three jars(hibernate,log4j,jackson) to again.
i.e my module jar file should not have any dependencies.
I am using eclipse.I am able to create a jar(project->export->jar) but it does not include the jars in it
Guide me how can I do that.
Is apache ant of any use here?
Eclipse's Runnable Jar Wizard
Eclipse's Runnable Jar Wizard (File → Export… → Java → Runnable Jar File) allows developers to create executable jars from an existing run configuration:
The wizard includes 3 options for handling dependencies:
Extract required libraries into generated jar: unarchives library
dependencies and repackages them into your executable jar. This
option has the advantage of simplicity and does not require a custom
class loader. However repackaging library jars can cause other
problems and does not preserve the signatures of signed jars. This
option may also violate the license terms of the libraries you are
using.
Package required libraries into generated jar: creates a "fat jar"
with a custom class loader. The resultant jar contains
the application's classes and resources
library jars required to launch the application
a small custom class loader that knows how to find jar libraries inside another jar archive
Copy required libraries…: creates the application archive and copies
any required library dependencies to the destination folder.
I think the second option suits your present purpose.
You will need to include all classes from your dependencies into your jar file.
Since a jar file is merely a zip file, you can use any archive manager such as Winzip to explore them, then copy the contents of the jars you depend on into your own jar, taking care to keep the directory structure intact.
That way you have everything in one jar.

Project Build classes do not include external project imports

I have a project which imports another projects to its build path. When I clean the project to compile classes, the classes imported from external projects are not showing up as part of the compiled classes directory.
Do anyone know how to make sure these external import classes gets compile and included into my build classes?
Thanks
I don't think it's possible. But when you deploy your project, you'll certainly want to create a jar file to hold all your classes. And Eclipse has a wizard to export a jar file and choose to embed dependencies in the jar.
The usual way, however, is to have each project generate its own jar, and to use all the jar of the project + the jars of the dependencies as the classpath of the deployed project.
#Bitmap,
is this a WAR or EAR project?
In EAR project you have to specify clearly the "JAVA EE Module dependency" to include the referenced projects.
If you include a project as a build dependency, it will be for compile-time only.
If these are simple java projects you may want to look at "JAR JAR" enter link description here
to achieve this.
HTH

Categories

Resources