Fat jar is missing with junit dependency - maven

I am building a fat jar using maven-assembly-plugin. The jar is generated successfully. The only dependency I have for my project is: JUnit. But in the fat jar, I am unable to see this included (using jar tf command). Is JUnit dependency not included in fat jar usually ?

junit is usually a test scoped dependency, and then it will not be included.
Dependencies with scope test are meant to used during the build for tests, so there is no need to include them into the final JAR.
If, for some reason, you need junit in the final JAR, you need to change the scope to compile.

Related

Maven build include obfuscated jar as dependency

I have a maven project that should be obfuscated by proguard plugin.
This works fine when my dependency jar is not obfuscated.
But the result is that the dependency jar (also owned by me) is included in my obfuscated app not obfuscated. The opposite when i include the already obfuscated jar in my project i get compile errors for not finding packages and classes.
How can i obfuscate both my app and my dependency jar?

Is there a method to include dependency with provided scope in the FAT Jar

I am using maven shade plugin to package a FAT jar. I have certain dependencies which are marked scope - provided, is there a way to include those as well in the FAT Jar?

Include the bytecode of a JAR file in a gradle library vs. just adding it as a dependency

If I add a JAR file to a gradle project's depenencies, the code compiles just fine, but after publishing to maven (publishToMavenLocal), the classes from the JAR are not found.
Obviously, this is because the jar is added as a "dependency" and not part of the project itself. Is there a way to get the contents of the JAR file to merge into the library? Do I need to make a separate maven repo for each JAR?
You can always try to create a fat jar which includes dependencies. You can follow the instructions provided here https://www.baeldung.com/gradle-fat-jar

gradle and dependency on spring-boot subproject

I've created a project with several gradle subprojects, including: "app" and "tests".
Tests have "app" in their dependencies. Tests use classes from "app"
When I run:
./gradlew clean test build
Everything works, tests run and pass.
But when I run:
./gradlew clean build
then the tests compilation fails with an error saying that a class is missing - in this case it's a spring-boot configuration class. I run this with --debug and it turns out that in the failing case app:bootRepackage task is executed before tests:test, the jar generated by app compilation is altered and that's why the classes cannot be found.
How can I make "./gradlew clean build" work properly?
Using: spring-boot 1.5, gradle 4.0 (and 4.1 too), io.spring.dependency-management plugin 1.0.0.RELEASE
Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.
To configure a classifier of exec … when using Gradle, the following configuration can be used:
bootRepackage {
classifier = 'exec'
}

How to include jar dependency classes in gradle?

We are building one jar and that jar depending on many jars. Currently we are adding dependency jars explicitly like below.
compile files('libraries/amqp-client-3.1.3.jar')
compile files('libraries/antisamy-1.5.1.jar')
compile files('libraries/antlr-2.7.7.jar')
compile files('libraries/aopalliance-1.0.jar')
So, instead of adding all the dependency jars explicitly, we need to take required classes from dependency jars and those should be included as part of jar preparation. Can any one tell me how to achieve? thanks.
Depending on your build system, you want the shade plugin (maven): http://maven.apache.org/plugins/maven-shade-plugin/ or the shadow plugin (gradle): https://github.com/johnrengelman/shadow
If you are using ant, I would try to leverage the maven plugin: http://maven.apache.org/ant-tasks/index.html
However, see the warnings in my answer here: Gradle shadow jar with conflicting dependencies

Resources