Gradle - How to force dependencies into the WEB-INF/lib - java

I'm looking to copy dependency jars into the war file's WEB-INF/lib directory whether they're transitive of the project or not. Is there a way to accomplish this?
My build.gradle is below and I've tried multiple options. Is there a way to ensure that a specific set of jars appears in the war no matter what?
build.gradle
plugins {
id 'war'
}
war {
manifest { attributes("Class-Path": "resources/") }
version = null
}
//Does not contain the jackson libraries
providedCompile project(":components:Manager")
dependencies {
//Doesn't work
implementation libraries.jackson_core
implementation libraries.jackson_databind
implementation libraries.jackson_annotations
providedCompile(
//Doesn't work
libraries.jackson_core,
libraries.jackson_databind,
libraries.jackson_annotations,
///
libraries.mybatis,
libraries.mybatis_cdi,
libraries.javax_ejb,
libraries.javax_rs
)
compile "javax:javaee-api:$jeeVersion" //The only jar that appears in the WEB-INF/lib folder
//Doesn't work
compile(
libraries.jackson_core,
libraries.jackson_databind,
libraries.jackson_annotations,
)
}

Solved my problem by setting up a custom warLib configuration
Credit
plugins {
id 'war'
}
configurations {
warLib
}
providedCompile project(":components:Manager")
dependencies {
providedCompile(
libraries.mybatis,
libraries.mybatis_cdi,
libraries.javax_ejb,
libraries.javax_rs
)
compile "javax:javaee-api:$jeeVersion"
warLib libraries.jackson_databind
}
war {
classpath configurations.warLib
manifest { attributes("Class-Path": "resources/") }
version = null
}

Related

Gradle WAR with compiled classes under WEB-INF/classes

I'm trying to port a legacy Java webapp project into gradle.
This is a snippet of my build.gradle
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
compile fileTree(dir: 'build/classes', include:'**')
}
To maintain the custom structure I want to put all my *.class files under WEB-INF/classes and it works, but I find also the same *.class files under WEB-INF/lib.
My goal it to keep jars and classes in separated war folder.
Any thoughts?
Edit: Added dependencies{} to the build.gradle snippet.
Problem get solved with commenting out builded classes from the dependencies:
def customBuildPath = 'build/classes'
war {
from(customBuildPath) {
into 'WEB-INF/classes'
}
from('WebContent') {
include 'Web/**/*'
into ''
}
}
dependencies {
compile fileTree(dir: 'projectlibs/lib', include:'*.jar')
// compile fileTree(dir: 'build/classes', include:'**')
}

Is there a way for Gradle to resolve dependencies using curl command? [duplicate]

I have tried to add my local .jar file dependency to my build.gradle file:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries
But the problem is that when I run the command: gradle build on the command line I get the following error:
error: package com.google.gson does not exist
import com.google.gson.Gson;
Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1
According to the documentation, use a relative path for a local jar dependency as follows.
Groovy syntax:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin syntax:
dependencies {
implementation(files("libs/something_local.jar"))
}
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs("libs")
}
}
dependencies {
implementation("gson-2.2.4")
}
However, being a standard .jar in an actual maven repository, why don't you try this?
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.2.4")
}
You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The following works for me:
compile fileTree(dir: 'libs', include: '*.jar')
Refer to the Gradle Documentation.
You can try reusing your local Maven repository for Gradle:
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar
Check that you have the jar installed into your ~/.m2/ local Maven repository
Enable your local Maven repository in your build.gradle file:
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation ("com.company:utility:0.0.1")
}
Now you should have the jar enabled for implementation in your project
A solution for those using Kotlin DSL
The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:
dependencies {
compile(files("/path/to/file.jar"))
testCompile(files("/path/to/file.jar"))
testCompile("junit", "junit", "4.12")
}
Remember that if you're using Windows, your backslashes will have to be escaped:
...
compile(files("C:\\path\\to\\file.jar"))
...
And also remember that quotation marks have to be double quotes, not single quotes.
Edit for 2020:
Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.
Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:
//gradle.build snippet
allprojects {
...
repositories {
//All sub-projects will now refer to the same 'libs' directory
flatDir {
dirs "$rootProject.projectDir/libs"
}
mavenCentral()
}
...
}
EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).
Shorter version:
dependencies {
implementation fileTree('lib')
}
The Question already has been answered in detail. I still want to add something that seems very surprising to me:
The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..
So don't rely on the output of this to check whether your referenced local lib files are working correctly.
A simple way to do this is
compile fileTree(include: ['*.jar'], dir: 'libs')
it will compile all the .jar files in your libs directory in App.
Some more ways to add local library files using Kotlin DSL (build.gradle.kts):
implementation(
files(
"libs/library-1.jar",
"libs/library-2.jar",
"$rootDir/foo/my-other-library.jar"
)
)
implementation(
fileTree("libs/") {
// You can add as many include or exclude calls as you want
include("*.jar")
include("another-library.aar") // Some Android libraries are in AAR format
exclude("bad-library.jar")
}
)
implementation(
fileTree(
"dir" to "libs/",
// Here, instead of repeating include or exclude, assign a list of paths
"include" to "*.jar",
"exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
)
)
The above code assumes that the library files are in libs/ directory of the module (by module I mean the directory where this build.gradle.kts is located).
You can use Ant patterns in includes and excludes as shown above.
See Gradle documentations for more information about file dependencies.
Thanks to this post for providing a helpful answer.
I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:
repositories {
mavenCentral()
// Not everything is available in a Maven/Gradle repository. Use a local 'libs/' directory for these.
flatDir {
dirs 'libs'
}
}
...
compile name: 'secondstring-20030401'
The best way to do it is to add this in your build.gradle file and hit the sync option
dependency{
compile files('path.jar')
}
The solution which worked for me is the usage of fileTree in build.gradle file.
Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can add jar doing:
For gradle just put following code in build.gradle:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
and for maven just follow steps:
For Intellij:
File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok
Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.
For Gradle version 7.4 with Groovy build file
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
If you are on gradle 4.10 or newer:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder
This steps will automatically add your dependency to gralde
Very Simple
Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.
For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.
An other way:
Add library in the tree view. Right click on this one. Select menu "Add As Library".
A dialog appear, let you select module. OK and it's done.

How to get sources from parent project as dependency

I want to do something like this:
dependencies {
compile project(':projectA', classifier: 'sources')
}
But I couldn't find such functionallity, I've searched in the documentation but there was nothing besides specifying the classifier on real dependencies.
So it there a way on adding the sources from an parent project inside a Gradle multimodules project as dependency?
I'm not aware that there is a feature from Gradle for that, but there is a workaround:
Add an packageSources to the project which should provide sources
// projectA/build.gradle
project.task("packageSources", type: Jar) {
classifier = 'sources'
from { project.sourceSets.main.allSource }
}
Depend the task on compileJava where you need the sources
// projectB/build.gradle
compileJava.dependsOn tasks.getByPath(':projectA:packageSources')
Add the source as dependency
// projectB/build.gradle
dependencies {
compile files(tasks.getByPath(':projectA:packageSources').archivePath)
}
projectA/build.gradle
configurations {
sources
}
dependencies {
sources sourceSets.main.allSource
}
projectB/build.gradle
dependencies {
compile project(path: ':projectA', configuration: 'sources')
}
See DependencyHandler.project(Map)

Generate war file without libs directory in gradle

I have a gradle project and configured war plugin that is successfully generating the war file.
I want my dependencies jar files not to be the part of .war file in directory [/WEB-INF/lib/].
I will manually add all the dependencies in jboss's lib folder.
apply plugin: 'war'
sourceSets {
main {
java {
srcDir 'src'
}
}
}
war.baseName = 'web'
webAppDirName = 'WebContent'
war {
webXml = file('WebContent/WEB-INF/web.xml')
}
dependencies {
compile fileTree('../ThirdPartyJars')
compile project(':core')
}
This is generating a .war file that already contains all the dependency.
Is their any way to remove all these dependencies from war file in gradle.
This is an awful idea, IMO, but you can add the dependencies to the providedCompile or providedRuntime configuration rather than compile or runtime.

Add classpath in manifest using Gradle

I would like my Gradle build script to add the complete Classpath to the manifest file contained in JAR file created after the build.
Example:
Manifest-Version: 1.0
Class-Path: MyProject.jar SomeLibrary.jar AnotherLib.jar
My build script already add some information to the manifest this way:
jar {
manifest {
attributes("Implementation-Title": project.name,
"Implementation-Version": version,
"Main-Class": mainClassName,
}
}
How do I get the list of dependencies to add to the manifest?
This page of Java tutorials describes more in detail how and why adding classpath to the manifest: Adding Classes to the JAR File's Classpath
Found a solution on Gradle's forum:
jar {
manifest {
attributes(
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
}
Source: Manifest with Classpath in Jar Task for Subprojects
In the latest versions of gradle, compile and runtime becomes deprecated. Instead, use runtimeClasspath as follows:
'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
EDIT:
Note that if you are using Kotlin DSL, you can configure the manifest as follows:
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
manifest {
attributes(
"Manifest-Version" to "1.0",
"Main-Class" to "io.fouad.AppLauncher")
}
}
tasks.withType(Jar::class) {
manifest {
attributes["Manifest-Version"] = "1.0"
attributes["Main-Class"] = "io.fouad.AppLauncher"
}
}
Place this at the end of the buid.gradle file. Change the com.example.Main to your own Main class.
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path': configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'com.example.Main')
}
}
}
}
The top answers helped me a lot. Here is what worked for me:
jar {
manifest {
attributes "Main-Class": "your.package.classWithMain"
attributes "Class-Path": configurations.compile.collect { it.absolutePath }.join(" ")
}
}
So, instead of name, I had to use absolutePath. This may or may not work for you. Some suggest using runtime instead of compile. I used compile because, I have a compile section in dependencies in my build.gradle. So, the jar step picks up dependencies from there. The best thing to do is pick up something that you think will work, do a gradle build, then find the JAR file and expand it to find the META-INF/MANIFEST.MF file. You should be able to see all the directories separated by spaces. If not, you should try something different. Autocomplete feature of your IDE should be helpful in seeing what all methods or fields are available under configurations/compile etc. All this can be done easily in IntelliJ.
Oh.. and if you want to see where the library JARs are physically located on your disk, right click on your project->open module settings->Libraries and then click on any library.
This is another solution for Kotlin DSL (build.gradle.kts).
When running your app, the library files are supposed to be in libs/ subdirectory of the app.
tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
manifest.attributes["Class-Path"] = configurations
.runtimeClasspath
.get()
.joinToString(separator = " ") { file ->
"libs/${file.name}"
}
}
I know this is likely trivial for the groovy people here, but in my case, I wanted to change the location of the Class-Path in the manifest file depending on whether I was going to run in the production environment or local environment. I did this by making my build.gradle's jar section look like this:
jar {
from configurations.runtime
manifest {
attributes ('Main-Class': 'com.me.Main',
'Class-Path': configurations.runtime.files.collect { jarDir+"/$it.name" }.join(' ')
)
}
}
In this case, the argument to gradle build is passed like so:
$ gradle build -PjarDir="/opt/silly/path/"
Looks like gradle has evolved. This is another answer that looks similar to others, but there is a key difference: if you use a new keyword implementation in the dependencies, none of the other answers will work and you'll get an empty class path
dependencies {
// ensure the keyword here matches what
// you have in the jar->manifest->attributes
// that is "implementation"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
// ...
}
// by default, implementation cannot be referenced,
// this allows us to use it below
project.configurations.implementation.setCanBeResolved(true)
jar{
manifest {
attributes(
"Main-Class": "app.Program",
"Class-Path": configurations.implementation.collect { it.name }.join(' ')
)
}
dependsOn ('dependencies')
}
If your project has external library dependencies, you could copy the jars to a folder and add the classpath entries in the manifest.
def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
from configurations.compile
into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
manifest {
attributes('Main-Class': 'com.example.gradle.App',
'Class-Path': configurations.compile.collect { 'dependencies/' + it.getName() }.join(' ')
)
}
with jar
}
More details can be read here
I managed to create a custom Jar file with its own manifest file like so:
task createJar(type : Jar) {
manifest {
attributes(
'Manifest-Version': "1.0",
'Main-Class': "org.springframework.boot.loader.JarLauncher",
'Start-Class': "com.my.app.AppApplication",
'Spring-Boot-Version': "2.2.4.RELEASE",
'Spring-Boot-Classes': "BOOT-INF/classes/",
'Spring-Boot-Lib': "BOOT-INF/lib/"
)
}
def originDir = file("${buildDir}/unpacked")
def destinationFile = "${buildDir}/repackaged/${project.name}-${version}"
entryCompression ZipEntryCompression.STORED // no compression in case there are files in BOOT-INF/lib
archiveName destinationFile
from originDir
archiveFile
}
I had a similar yet not identical problem.
I was publishing my lib jar L into Artifactory, and later fetching it as a dependency of module M, but the transitive dependencies, the ones which L need for compile and runtime, did not arrive with it. It took me sometime to realize that my jar was published into Artifactory with an empty pom file, hence gradle was not able to know which are L's transitive dependencies to be fetched.
The missing piece was an instruction, in the L's build.gradle, to publish the pom.
As often with gradle, the connection between the name of the instruction, and its meaning, is completely:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost/tmp/myRepo/")
}
}
}
Source: uploading_to_maven_repositories

Categories

Resources