I have a Copy task set as follow:
task copyToLib( type: Copy ) {
into "$buildDir/myapp/lib"
from configurations.runtime
// We only want jars files to go in lib folder
exclude "*.exe"
exclude "*.bat"
exclude "*.cmd"
exclude "*.dll"
// We exclude some lib
exclude group: "org.slf4j", name: "slf4j-api", version: "1.6.2"
}
And i'm getting the following error:
Could not find method exclude() for arguments [{group=org.slf4j, name=slf4j-api, version=1.6.2}] on task ':copyToLib' of type org.gradle.api.tasks.Copy
I have the feeling that it's only a syntax issue, any hint?
Exclude by group: exclude group: org.slf4j
Exclude by module: exclude module: slf4j-api
Exclude by file name: exclude { it.file.name.contains('slf4j-api') }
Exclude a file: exclude "slf4j-api.jar"
You can exclude by group and module but it needs to go into configurations exclude like this. Then it's gonna restrict the configuration before copying.
task copyToLib( type: Copy ) {
into "$buildDir/myapp/lib"
from configurations.runtime {
exclude group: 'org.slf4j'
}
// We only want jars files to go in lib folder
exclude "*.exe"
exclude "*.bat"
exclude "*.cmd"
exclude "*.dll"
}
And remember to make sure that the directory exists $buildDir/myapp/lib
And maybe instead of excluding all other files just include jars?
Maybe write a method to help you
static String generateExcludeJar(Map<String, String> map) {
"$map.name-${map.version}.jar"
// All jar names are like name-version.jar when downloaded by Gradle.
}
exclude generateExcludeJar(group: "org.slf4j", name: "slf4j-api", version: "1.6.2")
For Gradle 7.4 with Groovy:
task copyLibs(type: Copy){
from configurations.externalLib{
into '<dest-dir-name>'
exclude('slf*.jar', '<other jars if needed>')
}
}
FYI:
configurations {
externalLib.extendsFrom(implementation)
}
Related
I'm trying to exclude some modules from my build.gradle file but it(code1 and code2) still downloads the excluded files.
code 1:
compile (group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.7') {
exclude group: 'com.amazonaws', module: 'aws-java-sdk-machinelearning'
}
code 2:
compile (group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.7') {
exclude module: 'aws-java-sdk-machinelearning'
}
when I tried using the following code,
configurations {
compile.exclude module: 'aws-java-sdk-machinelearning'
}
it excludes the files but I don't want to use this method to exclude files
I second/confirm with #Opal that code1 works fine in Gradle 2.13.
What is likely happening is that you have some other (maybe non-aws) dependency, that may be transitively using aws-java-sdk which then brings in the machine-learning dependency. Which is why, it works fine when you do a global exclude, but not when you do a local exclude on just aws-java-sdk.
Try running gradlew dependencies --configuration=compile to get a tree of dependencies, including transitives, to check which dependency might be bringing in aws-java-sdk-machinelearning
In my build.gradle I have the following snipped:
dependencies {
compile project(':utils')
compile (project(':cache')) {
// excludings because of dropwizard conflicts
exclude group: 'org.glassfish.jersey.core'
exclude group: 'javax.ws.rs'
exclude group: 'com.codahale.metrics'
}
but in fact the artifacts are not excluded I still end up with different jersey and metrics in my classpath. Ony if I put this in my cache/build.gradle it will compile and run.
configurations {
all*.exclude group: 'com.codahale.metrics'
all*.exclude group: 'org.glassfish.jersey.core'
}
But then my cache project is broken because of missing dependencies which is not what I wanted.
I've had issues with this in the past, what worked was when I included module: explicitly along with the group. like so:
exclude group: 'commons-math3', module: 'commons-math3'
If you use configurations to exclude, you don't have to do it on all*, you can do it on just local project compile as :
compile.exclude group: 'commons-math3', module: 'commons-math3'
I'm starting with Gradle and I was wondering how do I include a single dependency (TeamSpeak API in my case) into my JAR so that it could be available at the runtime.
Here is a part of my build.gradle :
apply plugin: 'java'
compileJava {
sourceCompatibility = '1.8'
options.encoding = 'UTF-8'
}
jar {
manifest {
attributes 'Class-Path': '.......'
}
from {
* What should I put here ? *
}
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.7.Final'
compile group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
// Many other dependencies, all available at runtime...
// This one isn't. So I need to include it into my JAR :
compile group: 'com.github.theholywaffle', name: 'teamspeak3-api', version: '+'
}
Thanks for your help :)
The easiest way is to start with a separate configuration for the dependencies you want to include. I know you only asked about a single jar but this solution will work if you add more dependencies to your new configuration. Maven has a well known name for this sort of thing called provided, so that is what we will use.
configurations {
provided
// Make compile extend from our provided configuration so that things added to bundled end up on the compile classpath
compile.extendsFrom(provided)
}
dependencies {
provided group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
}
jar {
// Include all of the jars from the bundled configuration in our jar
from configurations.provided.asFileTree.files.collect { zipTree(it) }
}
Using provided as the name of the configuration is also important because when the jar gets published, any dependencies you have in the providedconfiguration will show up as provided in the POM.xml that gets published with the JAR. Maven dependency resolvers will not pull down provided dependencies and users of your jar will not end up with duplicate copies of classes on the classpath. See Maven Dependency Scopes
I'm using the Microsoft JDBC Driver 4.1 to connect to SQL Server. In Eclipse I have added the .jar file to the class path of my project ( C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc41.jar).
I've added this .jar to a folder called lib in my project and am trying to have this .jar added as part of my .jar:
repositories {
mavenCentral()
}
dependencies {
compile files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
However, sqljdbc41.jar is not included in my .jar. Am I missing something?
You have to create a (application) distribution to package your libraries and application jar in a folder: http://gradle.org/docs/current/userguide/application_plugin.html
Alternatively you can use a plugin like https://github.com/johnrengelman/shadow to create a self-contained uberjar.
Thanks chromanoid. I was able to solve the problem two ways. First I placed the sqljdbc41.jar in a new folder called lib in my project. Solution 1 was to create an application distribution:
apply plugin:'application'
apply plugin: 'java'
mainClassName = "main.java.application.Main"
repositories {
mavenCentral()
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
You can then create a distribution using the distZip task. Solution 2 was to create a fat jar.
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
dependsOn configurations.runtime
from {
(configurations.runtime - configurations.provided).collect {
it.isDirectory() ? it : zipTree(it)
}
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
and use the build task.
I have a simple build.gradle that pulls in a few java dependencies. On my mac, gradle keeps saying that its unable to resolve dependencies for any jar. This only started happening yesterday, rebooting doesn't solve this. I have tried running the project on linux and things work just as expected. Any idea whats up with my mac thats causing this?
Cause 2: java.lang.InternalError
[...]
at javax.xml.parsers.SecuritySupport$4.run(SecuritySupport.java:92)
at java.security.AccessController.doPrivileged(Native Method)
at javax.xml.parsers.SecuritySupport.getResourceAsStream(SecuritySupport.java:87)
at javax.xml.parsers.FactoryFinder.findJarServiceProvider(FactoryFinder.java:288)
at javax.xml.parsers.FactoryFinder.find(FactoryFinder.java:255)
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:121)
at org.apache.ivy.util.XMLHelper.getDocBuilder(XMLHelper.java:208)
at org.apache.ivy.util.XMLHelper.parseToDom(XMLHelper.java:193)
at org.apache.ivy.plugins.parser.m2.PomReader.<init>(PomReader.java:95)
at org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.GradlePomModuleDescriptorParser.parseDescriptor(GradlePomModuleDescriptorParser.java:91)
[...]
Caused by: java.util.zip.ZipException: error in opening zip file
When I run on linux, things are working fine.
Here is my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.7'
}
apply plugin: 'scala'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.10.3'
compile('com.twitter:finagle-core_2.10:6.9.0') {
exclude group: 'junit', module: 'junit'
exclude group: 'org.mockito', module: 'mockito-all'
exclude group: 'org.scala-tools.testing', module: 'specs_2.10'
exclude group: 'org.scalatest', module: 'scalatest_2.10'
}
compile('com.twitter:finagle-http_2.10:6.9.0') {
exclude group: 'junit', module: 'junit'
exclude group: 'org.mockito', module: 'mockito-all'
exclude group: 'org.scala-tools.testing', module: 'specs_2.10'
exclude group: 'org.scalatest', module: 'scalatest_2.10'
}
compile 'com.fasterxml.jackson.module:jackson-module-scala_2.10:2.3.0'
}
Using Java 7 and Gradle 1.7
Try dropping maven and/or gradle cache for the dependencies that make problems or just altogether:
rm -rf ~/.gradle/
rm -rf ~/.m2/repository/
Please note that the later may lead to significant network traffic and build times next time the build is run!
Turns out my java got corrupted; reinstalling java fixed the issue