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.
Related
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?
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/
I am using Maven to manage a console application project. On my machine, I type mvn exec:java and Maven handles everything. What I want is, however, to execute the same application on a different machine without the help of Maven.
In NetBeans, Ant projects have a dist directory with all the necessary files. All you have to do is to type java -jar dist/App.jar. How can I make Maven generate such distributable directory or archive?
PS: Although seems relevant, this is not a duplicate of Create a standalone application with Maven.
I have used in maven.
http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/
The Application Assembler Plugin is a Maven plugin for generating scripts for starting java applications. All dependencies and the artifact of the project itself are placed in a generated Maven repository in a defined assemble directory. All artifacts (dependencies + the artifact from the project) are added to the classpath in the generated bin scripts.
and http://maven.apache.org/plugins/maven-assembly-plugin/
The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.
You can build an executable jar file with the maven-jar-plugin; more info on their examples page here: http://maven.apache.org/shared/maven-archiver/examples/classpath.html
That will simply create an all-in-one jar that can be executed through java -jar