Difference between Jar and Plugin in Java and when to use them - java

What is the difference between Jar and Plugin in Java? Both looks the same by achieving the same purpose and when do use Jar and when do we use Plugin.

A plugin is an extension to Maven, something used to produce your artifact. Plugins are used only to make maven process successful. They are not directly connected to your application. plugins do not include in your last war/jar file for the service or client.
A dependency is a library that is needed by the application you are building, at compile and/or test and/or runtime time. the classes you used from jars will include in your final war/jar.
This will be helpful for you,
What is the difference in maven between dependency and plugin tags in pom xml?
Difference between plugin and external jar file

plug-in is a software component that adds a specific feature to any computer program.It specially use to customize any computer program.But .jar file is a java executable file which can only run on an environment which Java installed.

Related

How are java dependencies deployed on server machine

Did a lot of research but could not find a proper answer. My question is simple - I am building an executable jar file which has few external dependencies like spring etc. Now I want to deploy my executable jar file to server machine. Is there a easy and safe way of achieving it? Few options I am aware of:
Build an uber jar with all the dependencies bundled along with application code and deploy it
Deploy the source code executable jar and then manually add all the dependency jar files to the class path
Is there any other better way? Any tools which can help here? How are dependency jar upgrades handled? - Are they manually replaced on server machine?
If you 'just' have an executable jar and some other jar files as dependencies (this is the most common case actually), you can follow best practice standards and create a zip file containing them all. Check how various open source projects offer their stuff for download.
If you use some framework that might also guide you about deployment. As an example, the servlet specification tells you how to create that zip file in chapter 10.
If you want another way more compliant to the OS package manager, you could take a look at JPackage. It also bundles a Java Runtime so you have tight control not just about the jar dependencies but also the runtime.

How to include libraries during war file built in spring project?

I have a spring project and it is running well. The problem is, whenever I perform a new deployment using the war file in tomcat, I need to manually copy the libraries in the lib folder. I am wondering if there is any way to add those libraries inside the war file so that every time I don't need to waste my time by copying the libraries in the server. It needs to be automatically included from the generated war file.
This answer depends on how you are building your WAR but I assume you are using one of the popular build tools for Java. Given that your war does not have a populated lib folder I'm assuming you have either not configured the right plugins for these build tools or you are compiling and managing your classpath from the command line like a madman. If the later is the case then I highly suggest you start using a build tool for your projects (All IDE's do this by default), if the former is the case I refer you to the individual pages for the plugins:
Maven:
https://maven.apache.org/plugins/maven-war-plugin/index.html
The war plugin does this by default.
Gradle: https://docs.gradle.org/current/userguide/war_plugin.html
The War plugin for Gradle also does this by default.
Ant: https://ant.apache.org/manual/Tasks/war.html
A glossary reading for the Ant tasks did not reveal if it did so by default but I'm assuming it does.

What is the advantage to build an executable JAR

Today at work I came across something interesting. Say i have an old java project that were compiled with an ant build file and we have converted this project into a maven project. So now to build this project, we only need to do a mvn install.
When i do call
mvn install
I get a myproject.jar under the target folder, along with all the dependencies under a lib folder inside the target folder.
To run the executable of this jar I need to do something like :
java -classpath $classpath com.myproject.Mainclass $myArgs
Where $classpath is the path to all of my external libs and where $myArgs is the arguments that is passed to the main function.
I came across this website and I'm really considering to use the spring boot maven plugin to package my executable jar.
Wouldn't it be easier to execute it if all the dependencies are packaged in a single jar file ?
Why would I use the manual configuration vs the Spring Boot Maven Plugin for the executable jar ?
What are the pro and the cons of doing this ?
As the article you had linked covers with pros and cons how to do such single jar file packaging, I'll write out things that you need to consider if you want to use this approach.
Pros:
1. Simplicity of deployment
Users don't have to maintain any dependencies. All they need to do to run the app is get the jar file and execute java -jar file.jar.
2. No easy way to update dependencies by user
If your app uses some external dependencies, you can be sure they are in version that you have chosen. Using "classic" approach user can easily update it by himself to the version that may require some migration steps in your app.
Cons:
1. Size of final package
If your app has large dependencies, every update will require users to download the whole package,
even if dependencies haven't changed.
2. No easy way to update dependencies by user
To change a version of any dependency you will need to update the whole package, where using the old way you could update only the dependency jar.
Summarizing, if your app doesn't have any heavy (in sense of file size) dependencies, I'll personally use single jar file approach. Even if your dependencies changes frequently. It's a lot easier to change a single file, no matter if your app has to be updated or some of its dependency.

Where to place java packages like htmlparser?

I am beginning to use java packages like HTMLParser, I have downloaded it and finding that there are many files and directories in it.
I wander, where to place them in my linux system? Is there a convention or a standard?
The quick and dirty answer is "anywhere on the classpath", where the classpath is set either as a system property on the client machine (not recommended), as a temporary system property for the CLI session used to start the JVM (workable from a startup script), or as a commandline parameter to the JVM (usually the preferred choice).
First and second set the CLASSPATH environment variable, see the JDK or JRE documentation for the exact syntax and your operating system's and/or shell scripting documentation as well. Third uses the -cp commandline variable to the Java runtime and compiler, see their documentation for exact syntax.
Where to place the files on the filesystem? For development purposes I typically use a central folder on my computer containing all such libraries and link to that from my IDE or other development environment. For deployment/packaging to end users, it is traditional to have a "lib" subfolder to the product folder that contains all distributable content, and put the jar files in that.
Java packages come in two forms. Source code - all the files and directories you mention - and packaged as jars. A common convention in Java projects is that the project has a lib directory that contains all the jars that the project depends on. These projects often use a shell script which adds all the jars to the Java classpath prior to executing the project code.
However many projects are switching from this method of dealing with dependencies to using a build tool like Apache Maven which automatically handles dependency management. Other alternatives include Ivy or Gradle. For an introduction see the 5 minute introduction to Maven or the Maven 3 tutorial.
Here you write a pom.xml (project object model file) which specifies which libraries (jars) your project uses. Maven then stores all the jars for your different projects in a .m2 directory in your local directory, keeping track of where it obtained them, and their versioning information.
This makes developing much easier as you do not need to create the lib directory or manually manage dependencies. You also avoid a lot of the complexities of setting the classpath, as Maven automatically does this for you during common lifecyle stages such as compilation and test. Recent versions of Eclipse can read the Maven pom and automatically configure your classpath from it.
Once you have built the project, Maven can also help create "fat jars" that contain all the jars your project depends on, via the assembly plugin or the Shade plugin. This makes distributing the code easier when you are building an executable that you want someone to use. If you are distributing a jar, then your pom.xml describes the dependencies of your project, avoiding the need to distribute the jars it depends on.
For laying out files in general on a Linux system consult the Linux Filesystem hierarchy standard.

Separating external jars from plugin lib direcory

I have a plugin that depends on external jar files.
I've made an update site to install the plugin into any eclipse.
These external jar files are shipped with the plugin.
I want to separate these external dependencies so that the plugin needs them during the installation and get them at the installation time. or search for them in a specific location away from the plugin jar file
Is that possible?
If so, How can I achieve that ?
Create separate plug-in's for each of the external jar's?
Like the way Eclipse handles things like Apache components.

Categories