Changing resource target dir in gradle built jar - java

I have the following structure in the project:
Project_1 -> src->main->java->all_java_files
-> src->main->resources-> all_prop_files
I want to build the jar so that the property files will be put under 'main/resources' inside the jar. By default gradle puts them under root.
Here is my build file with my fix. But this causes sonarqube indexing issues.Is there an easy way to change output directory for the resource files?:
apply plugin: 'java'
dependencies {
compile libraries.log4j
compile libraries.apache_commons_net
compile libraries.apache_commons_lang2
compile libraries.apache_velocity
compile libraries.ehcache_15
compile libraries.apache_commons_validator
compile libraries.apache_xmlbeans
compile libraries.spring_framework
}
compileJava.classpath += was70ServerLibraryJars
sourceSets {
main {
resources{
srcDirs = ['src']
exclude 'main/java'
}
}
}
jar {
}

Why don't you move
src->main->resources-> all_prop_files
to
src->main->resources->main->resources-> all_prop_files
By convention everything in src/main/resources will be copied in the root of the JAR.

Related

Gradle (java): test task should use generated .jar and not the .class files in classpath

Gradle with apply plugin: 'java' in build.gradle. The file will create a .jar file and the test task is running junit tests:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
This is working. But to make sure the public API tests are working with the generated .jar file I want that the 'test' task is running the test with the generated .jar file from the build/libs folder in classpath and not with the generate .class files from folder build/classes in in classpath.
Not working because the sourceSets is set global:
tasks.withType(Test) {
sourceSets {
main {
java {
exclude '**'
}
}
}
}
Partly working: multiproject (test and jar separated in two gradle projects):
dependencies {
compile project(":jar_project")
testCompile 'junit:junit:4.12'
}
in this case jar_project.jar is used but package private test are also executed without an error.
Do somebody have an idea how to run the tests with the .jar as dependency and ignoring the .class files?
Thank you, pulp
The problem is that the test task does not depend on the jar task, which sort of makes sense because the jar is supposed to package the classes, so there should be no reason for tests to depend on that jar.
You can force the dependency by:
test {
dependsOn jar
doFirst {
classpath += jar.outputs.files
}
}
Now the jar will be on the test classpath

How do i fix my dependencies in my build.gradle file

I am new to gradle, i need to configure my build.gradle file . Am using selenium webdriver and i have list of .jar files. how do i include this jar files as dependencies in my build.gradle file?. i have this .jar in a folder called lib in my package. and i have
dependencies {
compile fileTree(dir: 'lib', includes: '*.jar')
}
but i keep having the error below:
FAILURE: Build failed with an exception.
Where:Build file '/home/ola/workspace/build.gradle' line: 20
What went wrong:
A problem occurred evaluating root project 'workspace'. Cannot cast object '*.jar' with class 'java.lang.String' to class 'java.lang.Iterable'
please can anyone point me to how to write dependencies for a webdriver project using gradle.This is the path to my lib folder: "/home/user/workspace/mainsite_automation/src/autoTest/lib/"
Thanks
You just need to specify the dependencies repository and the selenium webdriver dependencies so you will end up with a build.gradle similar to this:
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
mavenCentral()
}
sourceSets {
selenium
}
dependencies {
seleniumCompile 'junit:junit:4.11'
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.30.0'
}
task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
daemon = true
}
task selenium(type: Test, dependsOn: jettyDaemon) {
testClassesDir = sourceSets.selenium.output.classesDir
classpath = sourceSets.selenium.runtimeClasspath
}
for Eclipse, you can add selenium dependencies to the classpath adding this to your build.gradle:
eclipse {
classpath {
plusConfigurations += configurations.seleniumCompile
}
}
then you can use grade clean selenium to rebuild your project.
Sources:
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
http://www.dreamchain.com/gradle-selenium-webdriver-task/

Gradle- Importing a JAR file with absolute windows path?

How do I import a JAR file as a dependency through a full Windows URL? I'm trying to import JAR file on a network drive (labeled "H") and I cannot use Maven Central easily because of our silly firewall.
when I try to use compile files I still get compile errors due to the dependency classes not being found.
apply plugin: 'java'
dependencies {
compile files ("H/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar")
}
sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
}
jar {
from configurations.compile.collect { entry -> zipTree(entry) }
}
It looks like there is an error in your path. It should be corrected like this.
H:/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar
If you want to add more than one jar file as dependencies, here is how to do it(relative path is used in here)
dependencies {
compile fileTree(dir: '../../lib', include: '**/*.jar')
}

Gradle to pull down dependencies into local lib dir

I have the following list of JAR dependencies (the following is my entire build.gradle file):
apply plugin: 'java'
dependencies {
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.3'
compile 'com.wordnik:swagger-annotations:1.3.4'
compile 'javax.validation:validation-api:1.0.0.GA'
compile 'io.dropwizard:dropwizard-core:0.7.0'
compile 'io.dropwizard:dropwizard-client:0.7.0'
compile 'com.wordnik:swagger-jaxrs_2.10:1.3.4'
compile 'org.eclipse.jetty:jetty-servlets:8.1.14.v20131031'
}
I would like to run Gradle and have it pull all of these JARs (and their transitive deps) from Maven and place the JARs in a local lib directory.
When I run this I get a BUILD SUCCESSFUL message, but I don't see a lib directory under my main TestGradleroot` dir. I was expecting to see a directory that would contain all of these JARs as well as their transitive dependencies.
Try to use separate 'compile' for each library.
This is how it looks on mine android project:
dependencies {
compile 'com.google.android.support:wearable:+'
compile 'com.google.android.gms:play-services-wearable:+'
}

Gradle: How to exclude JAR from a WAR?

I have a multi-project Gradle build structure, where child project depends on a JAR, which I don't want to be in WAR file. I tried "exclude" but it does not work.
The main project script:
apply plugin: 'war'
war {
dependencies {
runtime (project(':childProject')) {
exclude group: 'javax.servlet.jsp', module: 'jsp-api'
}
}
}
The childProject script:
apply plugin: 'java'
dependencies {
compile 'javax.servlet.jsp:jsp-api'
}
I'm doing it this way.
war {
rootSpec.exclude("**/some-*.jar")
}
From the Gradle documentation
The War plugin adds two dependency configurations: providedCompile and
providedRuntime. Those configurations have the same scope as the
respective compile and runtime configurations, except that they are
not added to the WAR archive.
So, in other words, adding an entry to providedCompile or providedRuntime will cause that dependency to be excluded from the war file.
use providedCompile if you have source that relies on some classes for compiling
use providedRuntime if you use it for testing and not compiling.
http://www.gradle.org/docs/current/userguide/war_plugin.html
Example
providedCompile "javax.servlet:servlet-api:2.5"
I realised that providedCompile sometimes introduced dependencies issues for me, e.g. some classes are not found. I then figured out a more flexible solution.
We can just configure required dependencies as 'compile' and then exclude specific jars from the war file by using the following war configuration:
war {
classpath = classpath.filter { file ->
println file.name
(
!file.name.startsWith('gwt-dev') &&
!file.name.startsWith('gwt-user') &&
!file.name.startsWith('guava-gwt') &&
!file.name.startsWith('gwtbootstrap3') &&
!file.name.startsWith('gwtbootstrap3-extras') &&
!file.name.startsWith('servlet-api')
)
}
}
So far, I found this is the cleanest solution for me. I hope it helps.
I had the same problem but i found a generic solution based on the one of Jake W.
In your child-project, without the war plugin, you add your own providedCompile and providedRuntime like this:
configurations {
providedCompile
providedRuntime.extendsFrom providedCompile
}
plugins.withId('java') {
configurations {
compile.extendsFrom providedCompile
runtime.extendsFrom providedRuntime
}
}
In the project with your war file you copy this anywhere you want:
configurations.runtime.allDependencies.withType(ProjectDependency) { ProjectDependency dep ->
Project proj = dep.dependencyProject
evaluationDependsOn(proj.path)
Configuration cfg = proj.configurations.findByName('providedRuntime')
if (cfg != null){
war {
classpath -= cfg
}
}
}
The default behavior of the War task is to copy the content of src/main/webapp to the root of the archive. Your webapp directory may of course contain a WEB-INF sub-directory, which may contain all the dependencies of the runtime [1] configuration to WEB-INF/lib.
So to avoid load of other jar files or to decrease war file size, you may have to exclude jars during packaging. So, try adding rootSpec.exclude("/*.jar")** to exclude jars in war file like below.
war {
archiveName = "newproject.war"
rootSpec.exclude("**/*.jar")
destinationDir = buildDir
}

Categories