Could somebody explain me the exactly differences between these two Maven plugins and when it is useful to work with one of these plugins?
Maven jar plugin simply creates a jar files with all SOURCE files [.class files compiled from .java files] packed in it. However, a jar itself cannot be deployed as it generally has dependencies on 3rd party jar files or other library jar files which are needed to execute SOURCE jar file.
This is where Maven assembly plugin comes into picture. It creates a package of an extension of your choice like .zip, .tar, .gz which is a fully deployable package with all dependencies packed in it. You can also specify directory structure in assembly plugin which should be created when package is deployed on server.
So a assembly plugin is always used in combination with jar plugin.
Maven Jar plugin provides the capability to build and sign jars.The plugin use Maven Archiver to handle jar content and manifest configuration.
Maven Assembly Plugin is used to create all assemblies.
For More Info visit- http://maven.apache.org/plugins/maven-jar-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/
Related
I'm new to the maven tool, below is what I have understood about plugins and dependency:
Plugin is a Jar file which executes the task, and dependency is a Jar which provides the class files to execute the task.
What is the difference in maven between dependency and plugin tags in pom xml?
When I define something in the dependency tag, nothing is downloaded to my target folder. Whereas the same thing defined in the plugin tag downloads it in the target folder. Why plugin unpacks the jar file?
Update:
Plugins were unpacked as it was defined in the goal of the plugin.
Plugins and dependencies are completely different things.
Plugins are used by Maven during the build. They form the different parts of the build.
Dependencies are artifacts that should be used by the Java program you create in your build.
So you e.g. need the Maven compiler plugin to compile the code, but add guava as a dependency if your application wants to use guava.
When i define something in dependency tag, nothing is downloaded to my
target folder.
Exactly, all dependencies are placed into $USER_HOME/.m2/repository. They can be used by other mvn projects.
Whereas same thing defined in plugin tag downloads it in target folder. Why > plugin unpacks the jar file?
Can you share your pom.xml? It may depend on your configuration.
I was reading about how to create a jar file with Maven, but I saw some pom.xml files using Maven Source Plugin and others using Maven Jar Plugin.
In the Apache Maven Project page I found these descriptions:
Apache Maven JAR Plugin
This plugin provides the capability to build jars. If you like to sign jars please use the Maven Jarsigner Plugin.
Apache Maven Source Plugin
The Source Plugin creates a jar archive of the source files of the current project. The jar file is, by default, created in the project's target directory.
Reading these descriptions, I stayed in doubt about when use one or other plugin, and what are the differences or benefits, because I understood that both make the same thing.
The Apache Maven JAR Plugin is used to build jar files containing .class files in order to distribute applications or libraries in bytecode format.
The Apache Maven Source Plugin is used to build jar files containing source files (.java files) in order to allow IDE to show the source code when debugging. This jar file is used in combination with the jar file containing .class files.
maven jar plugin
The jar plugin creates a JAR file from your Maven project. The jar goal of the jar plugin is bound to the package phase of the Maven default lifecycle. When you type mvn clean install, Maven will execute all the phases in the default lifecycle up to and including the install phase, which also includes the package phase.
maven source plugin
The source plugin creates a JAR file with the project source code. It defines five goals: aggregate, jar, test-jar, jar-no-fork, and test-jar-no-fork. All these five goals of the source plugin will run under the package phase of the default lifecycle.
Unlike any of the plugins we discussed before, if you want to execute the source plugin with the Maven default lifecycle, it has to be defined in the project POM file, shown as follows. The super POM file does not define the source plugin; it has to be within your Maven project itself
What is the Difference
Both create JAR files; however, the jar plugin creates a JAR file from the binary artifact, while the source plugin creates a JAR file from the source code. Small-scale open source projects use this approach to distribute the corresponding source code along with the binary artifacts.
I'm building a Java project using the Maven Project package in Eclipse Java EE IDE. I'm using different dependencies, some of them will be provided by the system where the java program will be run on, others will not. I added the provided scope tag into the pom file to the one I know are provided by the system and I now I would like to export a runnable .jar. Eclipse exports the .jar package with all the dependencies (provided and not) but there's a way to have the runnable .jar file with just the not provided dependencies packaged?
If you exporting a jar with eclipse, eclipse Jar-Packager will be used and the runnable jar will contain all dependencies. Eclipse jar-builder don't know about pom.xml.
In your case you should use mvn build, for example:
mvn clean package
Could anyone would be so kind and explain how to make a single jar file with all maven dependencies in Netbeans Maven Java Application? In Eclipse user can Export to Runnable jar file and select Package required libraries into generated JAR, so all dependencies within project comes in created jar file. In Netbeans there is no such option.
I have checked other answers, but the only thing i understand is that I have to add code to Build.xml file which is not even in the project.
The last time I had to do this I used the Maven Shade Plugin. It allows you to create a single JAR file and also handles dependency clashes.
A simpler solution (which doesn't handle dependency clashes) is to use the Maven Assembly Plugin.
Note that these are pure Maven solutions which should work in any IDE.
I have a project A that depends on a distribution of another project B. The distribution is a zip file which contains some resources and jars. It is contained as a .zip artifact in a maven repository.
I do not need the distribution of project B for compilation but for running tests for project A.
I use the dependency-plugin and the goal dependency:unpack to download and unzip the distribution.
1. Is there a possibility to get all .jar files contained in the .zip distribution in the classpath for the surefire plugin without naming every single jar explicitely?
2. If possible I also would like to include those jar files in the classpath of a eclipse project generated by the eclipse-plugin.
You can configure the classpath of the surefire plugin right? And of your eclipse project?
If so, you can use the maven plugin as shown here and filter to only copy jars and use the output directory that you configured in your classpath.
Does that answer your question?