My Gradle project has a couple of maven dependencies. I don't want those included in my jar. It also has some dependencies on local jars. How can i include the classes from those in the output jar?
You can refer to a local repository (a directory in this case) with the following:
repositories {
ivy {
// URL can refer to a local directory
url "../local-repo"
}
}
To exclude the dependent jars from your final jar, you can use the compile configuration like this:
compileOnly files('libs/jgrapht-jdk1.6.jar')
compile 'org.simpleframework:simple-xml:2.7'
A more detailed answer can be seen over here:
Gradle dependencies local jar file
Related
I'm using the latest version (7.1.2) of the ShadowJar (Gradle) plugin to build the application's JAR. I'm trying to exclude some dependencies from the resultant JAR, so I have configured the plugin like:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
...
tasks.withType(ShadowJar) {
final def attributes = [
// ...all the attributes here
]
dependencies {
// Dependencies that are already provided in the lib/ folder of Apache Flink
exclude(dependency("org.apache.flink:flink-clients_2.12"))
exclude(dependency("org.apache.flink:flink-java"))
exclude(dependency("org.apache.flink:flink-streaming-java_2.12"))
}
manifest.attributes(attributes)
mergeServiceFiles()
minimize()
setZip64(true)
}
It's not clear to me what dependencies are actually begin excluded/included — when unzipping and visually analyzing the generated ZIP file(s). I've been trying to compare the generated (ZIP) -shadow file with the one generated by the distZip task, but the -shadow one doesn't have a single JAR on it...only classes.
So I'm wondering if there is a way to print the effective group of dependencies that are going to be part of the final JAR file.
Based on the ShadowJar's documentation, the default configuration is to merge all dependencies from the project's runtimeClasspath, but ./gradlew dependencies --configuration runtimeClasspath doesn't account for the excluded ones.
How can I include a local jar dependency and its corresponding sources jar (for my IDE, Intellij) in Gradle?
I have tried adding a flatDir to lib (a directory in the same parent directory as all the Gradle stuff) under repositories, where lib contains mylib-1.0.jar and mylib-1.0-sources.jar. I then put implementation name: "mylib-1.0" under dependencies. The jar with compiled classes was included, but not the sources.
I also tried creating a local maven repository. In this case, lib contained
lib/xxx/yyy/mylib/1.0/mylib-1.0.jar
and
lib/xxx/yyy/mylib/1.0/mylib-1.0-sources.jar
where xxx.yyy is the group ID. I added
maven {
url uri("lib")
}
under repositories and
implementation group: "xxx.yyy", name: "mylib", version: "1.0"
under dependencies. Still did not work--neither jars were included this time.
I also tried adding a minimal POM in the same directory as both the jars, but that did not change anything.
Any idea as to where I could be going wrong?
Note: I am no expert at using Gradle or Maven.
Edit: Rationale: in case somebody suggests it, I am aware I can just include as a dependency the jar with the compiled class and "link" the sources jar to it in Intellij, but then every time I refresh gradle I have to re-link them.
I provide below the following approaches.
Prior to gradle 5
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
Here libs is the directory which contains the list of jar files and it should be location inside your project base directory.
You can also use in the following manner if you have only one jar file. Here libs refer to the directory which contains only one jar file and libs is available in the project base directory.
dependencies {
compile files('libs/your jar file name.jar')
}
If you want to specify a list of jar files, you can use in the following manner.
dependencies {
compile files(‘libs/a.jar’,
‘libs/b.jar’,
‘libs/c.jar’
)
}
In Gradle 5
You have to use in the follwong manner.
dependencies {
externalLibs files('libs/a.jar', 'libs/b.jar')
}
So let's say I have project A, which requires certain dependencies. And I have project B, which has project A as one of its dependencies. So I have the following code in project B's build.gradle:
project B's build.gradle:
dependencies {
compile files("projectA_dir/build/libs/projectA.jar")
}
However, since Gradle does not include project A's dependencies in projectA.jar by default. I was wondering if there's a way to let project B successfully compile project A without creating a fatjar for project A.
The jar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module.
It means that, if you are importing a jar file using compile files you have to specify the dependencies also in your project.
You should use a maven repository, private or public, to avoid the issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
I have a huge legacy EAR project. Current build process uses Ant and I'm trying to convert to gradle.
The old Ant build uses a single source folder and everything is in there (EJB and WAR code together); then Ant uses different tasks to build EJB-JAR and WAR artifacts filtering by the package of interest (my.web.* for the WAR and my.ejb.* for the EJB).
It goes without saying that EJB and WAR heavily reference each other and I guess that's why they are compiled together even though separate artifacts are generated.
I have tried creating a parent EAR project, then separating my.ejb.* in an EJB subproject as well as my.web.* in a WAR subproject but gradle immediately complained about circular dependency and I haven't found a way around.
If above is not possible, then I am looking for recommendations on how to generate the EJB-JAR and WAR artifacts off the same codebase that would be compiled altogether, then include them in the EAR file as 'deploy' dependencies.
I have googled around but I am not very bright at gradle (obviously). Any help will be greatly appreciated.
I think you are falling into the trap that many of us have when trying to convert an existing ant project to Gradle for the first time - you are thinking of things the same 'ol ant way.
Instead, let Gradle do the work for you and it will be easier and the resulting build.gradle file much smaller and more concise.
Thinking in terms of having a parent ear project and a separate war project is good thinking however. But, you should take advantage of the plugins Gradle has to offer to simplify the task of constructing the archives.
Use the 'ear plugin':
https://docs.gradle.org/current/userguide/war_plugin.html
For example:
apply plugin: 'ear'
apply plugin: 'java'
repositories { mavenCentral() }
dependencies {
// The following dependencies will be the ear modules and
// will be placed in the ear root
deploy project(':war')
// The following dependencies will become ear libs and will
// be placed in a dir configured via the libDirName property
earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}
ear {
appDirName 'src/main/app' // use application metadata found in this folder
// put dependent libraries into APP-INF/lib inside the generated EAR
libDirName 'APP-INF/lib'
deploymentDescriptor { // custom entries for application.xml:
// fileName = "application.xml" // same as the default value
// version = "6" // same as the default value
applicationName = "customear"
initializeInOrder = true
displayName = "Custom Ear" // defaults to project.name
// defaults to project.description if not set
description = "My customized EAR for the Gradle documentation"
// libraryDirectory = "APP-INF/lib" // not needed, above libDirName setting does this
// module("my.jar", "java") // won't deploy as my.jar isn't deploy dependency
// webModule("my.war", "/") // won't deploy as my.war isn't deploy dependency
securityRole "admin"
securityRole "superadmin"
withXml { provider -> // add a custom node to the XML
provider.asNode().appendNode("data-source", "my/data/source")
}
}
}
Then for your war, use the 'war plugin'. You can create the war as a separate project:
configurations {
moreLibs
}
repositories {
flatDir { dirs "lib" }
mavenCentral()
}
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0#jar"
dependency ":providedCompile-transitive:1.0#jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0#jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0#jar"
testCompile "junit:junit:4.12"
moreLibs ":otherLib:1.0"
}
war {
from 'src/rootContent' // adds a file-set to the root of the archive
webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
classpath fileTree('additionalLibs') // adds a file-set to the WEB-INF/lib dir.
classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir.
webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
}
There is another helpful post on creating them as a single project, but that's definitely a bit more 'dicey':
https://discuss.gradle.org/t/single-project-with-jar-ejb-war-and-ear/5874
I'm trying to add org.apache.commons.lang3 to my build. I've downloaded the library which is directory containing jar files.
My group is using gradle to build the project, and I know just enough to maybe ask the right question. So what I think the build is doing is
copying a bunch of .bnds to the build directory
compiles the java we have in src/main/java (via source sourceSets.main.java.srcDirs?)
I would like to add the lang3 library, but I'm not sure how to go about doing that. Can I just dump it into src/main/java? Or do I have to tell gradle about it?
This is what I think is relevant from the current build.gradle
ext.releaseDir = "${buildDir}/release/${tpVersion.getProgramName()}"
ext.bundlesDir = "${releaseDir}/nucleus/bin/nucleus_java/bundles/"
dependencies {
compile fileTree(dir: bundlesDir, include: '*.jar')
bnd {
source sourceSets.main.java.srcDirs
include '**/*.bnd'
You could declare it as a dependency, if it exists in any remote repository. That's the way I would do it.
But if you want to use the local file, do not put it in src/main. Use an extra folder called lib or similar on the same directory level as src or you build script.
Then you can add the local dependency to the build.gradle as in this sample:
repositories {
//central maven repo
mavenCentral()
}
dependencies {
//local file
compile files('libs/toxiclibscore.jar')
//dependencies from a remote repository
compile 'java3d:vecmath:1.3.1', 'commons-lang:commons-lang:2.6'
}
The simplest way is to use maven repository for accessing dependencies.
You can also access this jar directly from filesystem with file dependencies.
dependencies {
compile files('libs/a.jar', 'libs/b.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}