Download jar with dependencies using Maven - java

I would like to use Maven to download a "fat" jar- one that includes all of its dependencies. (The same sort of jar that is built using the jar-with-dependencies descriptor Ref in the maven-assembly-plugin)
The default behavior of mvn dependency:copy is to download the jar and all of its dependencies in separate files. This is not what I want. I want the dependencies to be included in the jar itself.
How do I achieve this using Maven? If it's not possible, is there some sort of manual way of assembling the fat jar myself given all its dependency JARs?

Related

Difference between plugins and dependency in maven tool (unpack jar)

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.

Configure maven to package fat jar without unpacking dependencies in a vertx project, like spring boot does

How can I tell maven to include dependency jar files while building a fat jar, rather than unpacking them to .class files?
I have a vertx 3.6.0 project producing a fat jar. I am using vertx-maven-plugin:1.0.13, and I run mvn clean package to build. In order to take advantage of Veracode SCM (static scanning), the dependencies inside my fat jar have to be intact, meaning the original dependency jar files have to be contained inside my fat jar. Maven is unpacking all dependencies though, so all I have are class files.
We have another spring boot project which works as expected. It seems the final spring boot repackage goal puts all the dependency jar files in the BOOT-INF dir inside the jar file.
Final 2 goals using vertx-maven-plugin:
maven-jar-plugin:2.4:jar
vertx-maven-plugin:1.0.13:package
Final 2 goals using spring-boot-maven-plugin:
maven-jar-plugin:2.4:jar
spring-boot-maven-plugin:2.1.2.RELEASE:repackage
I've searched the docs for vertx-maven-plugin and all over https://maven.apache.org and elsewhere without any luck so far.
Is there a way to get this same repackage behavior for a non spring boot app?
You can't do that with Vert.x because it doesn't do fancy classloading.
But since it's embeddable, you can create a SpringBoot app that just starts Vert.x. Then you'll get your dependency scanner working.
But I would first check with Veracode if it' can't inspect your POM or Gradle build file instead of scanning a JAR.
If your packaging requirement is for the veracode scan piece, you would probably try packaging your project artifacts into a tar, or zip using the maven-assembly plugin.
Veracode couldn't scan a dependency jar inside another jar for a non-spring boot project. So, you can try one of the following. i) tar packaging using maven-assembler plugin, or ii) convert it to spring-boot project if thats simpler and you could ,or iii) uber-jar creation using maven-shade plugin and need to make some configuration changes for not getting unpackaged to .class files. Still would suggest you can explore with the tar packaging option - (reference:) https://maven.apache.org/plugins/maven-assembly-plugin/

How to use jenkins to take a module maven project and turn it into a .jar

So I'm working on a java project located here. I have it on a jenkins server and I want it to compile all the different modules under one jar. I tried copying what another project did but am unable to produce a jar so I believe it has something to do with how I need to configure jenkins in order for it to make the jar. The jar doesn't need to be executable.
The maven-assembly-plugin can create your jar: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
Your project has 4 JARs and they don't depend on each other.
If NameLayer depends on NameLayer1_8_R1 you have to
add the dependency NameLayer1_8_R1 to the pom.xml of NameLayer and
the maven-assembly-plugin as descrbibed here to create a fat JAR (including all depending JARs).
Do the first step for as many dependencies as you need.

Maven: jars from zipped artifact as test dependencies

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?

Maven: antrun plugin builds JAR file -> How can i install/deploy it in my repository?

I am in the middle of an Ant -> Maven migration project and I have a question (I'm not really a Maven expert), since I'm stuck at a particular point:
Within one of my pom.xml files I have to use the maven-antrun-plugin to call an external ANT file, which builds a jar file and puts it in a temporary folder. There is no alternative to this call. Everything is working fine - the ant script works as it should, but how can I "package" this jar in the usual Maven workflow?
I know that I could manually call the mvn install:install-file, but isn't there a possibility to configure my pom.xml in a way that the above generated jar file IS actually the artifact of that pom.xml?
you use the build helper maven plugin's attach artifact goal to attach your extra *.jar to the maven module that triggered its creation.
since having a single maven module produce more than one artifact is generally a bad idea it would be best if you isolate this in a maven module of type pom so that this would be its only artifact

Categories