It's probably very simple, but only to people who know what they are doing.
I have a Java program that imports these two:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
As an aside, I don't want to use the lang3 package but the lang package.
I do not have anything in my Gradle file about these. When I try to build the file, it gives me errors for these two, saying the packages do not exist.
My questions are:
Do I need to add them as "compile" or as "api"?
What is the exact syntax? I have lines that look like this:
api group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
How do I find the right name (or should I just invent one)? and the version?
Anything your code needs (besides basic JRE classes) is a dependency for your code. Gradle manages these dependencies, usually downloading them from a repository.
First you need to find such a repository. You probably have repositories already configured in your build.gradle, like so:
buildscript {
repositories {
mavenCentral()
// maybe more repositories
}
}
That means Gradle will try to download dependencies from Maven Central. You can either do a web search for "gradle" and your dependency, or go to repository and search, or check the dependency's homepage.
You'll end up with a dependency name and version like 'org.apache.commons:commons-lang3:3.12.0'. This needs to go in your build.gradle.
Gradle has different dependencies:
buildscript dependencies provide code that Gradle needs to execute to build your project, e.g., a tool to pull in version control system information or generate code
implementation dependencies are dependencies your code needs to run, like a logging framework or JSON parser or PDF generator
test dependencies are dependencies needed to run your automated tests, like JUnit
Depending on where you need the dependency, you put it in the buildscript or the dependencies block.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.tmatesoft.svnkit:svnkit:1.9.+'
}
}
dependencies {
implementation 'pdfbox:pdfbox:0.7.3'
testImplementation 'junit:junit:4.+'
}
You don't need to repeat the implementation dependencies for the testImplementation btw, as it inherits them automatically.
You can define your own configurations as need; see the Gradle manual on dependencies, for example if you have different test suites (unit, integration, performance, ...) that need different dependencies.
you'll have to go to their official website and get the implementations then add those to the dependencies(you'll find it at the bottom of the file) in the build.gradle(module) it would look something like -
the $lifecycle_version might be somethiing like 1.2.3 or some version number.
this is what I got (not exactly sure if this is right)-
implementation 'org.apache.commons:commons-lang3:3.12.0'
got from library website in the gradle short tab
in the build.gradle(project), look for maven repo.
Once done, you'll be able to import the respective libraries.
Related
I want to use the sxcml-java library in my son's school's robotics code (currently a private repo).
The library uses Maven. I was able to successfully include the library in a test project using Maven.
However, I've just discovered that the existing robotics project code uses Gradle. I don't know either Maven or Gradle, and I haven't programmed in Java in almost 30 years.
How can I most easily use scxml-java - which itself has external 3rd party dependencies — in the robotics project?
This question is similar to this one, but the solution there was easy because both projects were using Gradle.
Provided the package is published in an artifactory, which is the case (See here), you can just include it as any other Gradle dependency (using groupId, artifactId and version), regardless of what build system was used to build it in the first place.
dependencies {
implementation 'com.nosolojava.fsm:scxml-java-implementation:1.0.1'
}
If you use IntelliJ IDEA, pasting the Maven dependency block into the build.gradle file will automatically convert it into the Gradle dependency format like the one above.
Please note however this does not apply to plugins, only to regular dependencies.
If You install your jar or third party jar into maven local repo like ~/.m2
you can add mavenLocal()
repositories {
mavenCentral()
// * Require by Use JAR install to Maven Local Repo your .m2
mavenLocal()
}
then add implementation to dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
implementation 'yourGroupId:yourArtifactId:yourVersion'
}
Please mapping yourGroupId , yourArtifactId, yourVersion from your pom.xml
If You only download third party jar into foler like /home/yourName/your-libs
you can add configurations
configurations {
sxcml-java-lib
}
then add dependencies
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
//sxcml-java-lib fileTree(dir: "${System.getProperty("user.home")}/libs", include: "*.jar")
sxcml-java-lib fileTree(dir: "/home/yourName/your-libs", include: "*.jar")
}
Dear StackOverflow users
I have a gradle project of which I want to turn the artifact into an osgi bundle. In this bundle I have:
packages that I don't want to export (may not appear in manifest's Export-Package entry)
dependencies that I want to embed (may not appear in manifest's Import-Package entry)
After a bit of tinkering I have come up with the following gradle.build file which does what I intend but maybe not in the cleanest way possible, leveraging bnd...
group 'com.mycompany'
version '1.0.0'
apply plugin: 'java'
apply plugin: 'osgi'
repositories {
jcenter()
}
dependencies{
compile 'org.osgi:org.osgi.framework:1.8.0' //provided
compile 'com.google.code.gson:gson:2.8.0' //embedded
}
jar {
//embedding the gson dependency
from({
def x = configurations.compile.find({
return it.getName().contains('gson')
})
def tree = zipTree(x)
return tree
})
//explicitly building manifest entries
manifest {
instruction 'Bundle-Vendor',
'My Company'
instruction 'Bundle-Activator',
'com.mycompany.mybundle.Activator'
instruction 'Import-Package',
'!com.google.gson',
'*'
instruction 'Export-Package',
/com.mycompany.mybundle;version="${version}"/
}
}
Is it possible to accomplish this in a cleaner way? I mainly want to avoid two things:
having to manually write the import and export-package entries
having to manually copy the contents of the embedded dependencies (gson) into my jar
I thought bnd (underlying the osgi plugin) could do that for me, but with what I have tried so far (even if I add them as private package) bnd still exports everything and imports the gson package as well as it won't add the gson classes to the jar
You would be better off using the Bnd Gradle plugin for OSGi. It is written and supported by the developers of bnd who know a thing or two about OSGi.
To specify a Maven dependency in my project, I provide a name, a group id, and a version. This has been enough for every dependency in my project, save one. Pig has multiple jars in the same artifact (not sure if I have the proper nomenclature; I'm still rather new to Maven), but I only need one.
Specifically, I need pig-0.13.0-h2.jar. However, when I provide the dependency
compile "org.apache.pig:pig:0.13.0"
in my build.gradle, only pig-0.13.0.jar, pig-0.13.0-sources.jar, and pig-0.13.0.pom are downloaded. I need the "*-h2.jar", because that's the correct one to work with my version of Hadoop.
Is there a way to tell Gradle (and, generally, Maven or whatever) that my compile dependency requires this exact jar, and that only this one should be included in the classpath?
What you need is to specify the classifier. The following script will do the job:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile "org.apache.pig:pig:0.13.0:h2"
}
task copyDeps(type: Copy) {
from configurations.compile
into 'deps'
}
Suppose we have a multi-project build. Eventually the entire project is bundled into a war. We know the web container will provide some utility jars, say, some logging-related jars, so we want to use a provided kind of scope. The war plugin offers the nice providedCompile and providedRuntime configuration. However, we want to use the war plugin only in the subproject that creates the war. So, the problem is, how can we use something like providedCompile and providedRuntime in other sub-projects? Ideally, we want to apply that scope to the logging jars in a subprojects {} closure.
update
My scenario is like below.
In the root build.gradle, I have something like this:
subprojects {
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.7' // already provided by the servlet container
compile 'javax.servlet:javax.servlet-api:3.1.0' // already provided by the servlet container
runtime 'ch.qos.logback:logback-classic:1.1.2' // already provided by the servlet container
runtime 'ch.qos.logback:logback-core:1.1.2' // already provided by the servlet container
...
These dependencies are used not just by the war sub-project, but they should be excluded in the war. Therefore, I need some mechanism to achieve this.
BTW, I have already worked out a solution.
There are a couple of ways to achieve a provided scope.
Option 1
Create a custom configuration provided. For example:
configurations {
provided
}
sourceSets.test {
runtimeClasspath += configurations.provided
}
The downside of this approach is that IDEs, for instance, IntelliJ IDEA, probably don't understand custom configuration and hence are not able to pull dependencies correctly. This happened to me with IntelliJ IDEA.
Option 2
Use the predefined compile and runtime configurations. Then, in the build.gradle of the war sub-project, filter out the jars of your choice. For example,
war {
Set exclusions = ['slf4j-api-1.7.7.jar', 'javax.servlet-api-3.1.0.jar',
'logback-classic-1.1.2.jar', 'logback-core-1.1.2.jar'] as Set
classpath = classpath.filter { file ->
!exclusions.contains( file.name )
}
}
With gradle 2.12, it is possible to use compileOnly when using java plugin.
https://docs.gradle.org/2.12/release-notes#support-for-declaring-compile-time-only-dependencies-with-java-plugin
How do I configure build.gradle to depend on LWJGL?
I'm new to Gradle, and how to configure library dependencies is clear as mud to me.
It's my understanding is that one can specify library dependencies for Gradle to download rather than checking them in to source control, but any sort of help with configuring things would be appreciated.
(I don't know any Ivy or Maven.)
I think what you want is to have lwjgl in your build classpath and resolve it automatically right?
try this snippet:
plugins {
id "java"
}
repositories{
maven {
url = "http://adterrasperaspera.com/lwjgl"
}
}
dependencies{
implementation "org.lwjgl:lwjgl:2.6"
implementation "org.lwjgl:lwjgl-util:2.6"
}
This snippet above defines a maven repository which contains the lwjgl libs and defines two compile dependencies to your project.
regards,
René