Gradle 1.2: Exclude directory under resources sourceSets - gradle

I have development related directory src/main/resources/certs/test which is needed for one external library. This has some cert files which are not needed in production build.
At the moment I exclude everything under that directory with following block in build.gradle:
sourceSets {
main {
resources {
exclude '**/test/*'
}
}
}
This does it job well, but leaves ugly empty directory test lying there. What are my options to not include this directory in final war?
I've tried excluding '**/test', but it doesn't work at all.
I use war plugin and Gradle 1.2

Using Gradle 1.1, this works for me:
apply plugin: 'war'
sourceSets {
main {
resources {
exclude '**/test/*'
exclude 'certs/test'
}
}
}

I had a similar problem with production files in a JAR file (though mine were not test files). I solved it with the following:
jar {
exclude ("DIRECTORY-TO-EXCLUDE/**")
}
e.g.
jar {
exclude ("test/**")
}

A common projet layout is to put test files under the test source set, this way you don't have to exclude them from the main source set.
From the Gradle documentation, the default project layout is like this:
src/main/java Production Java source
src/main/resources Production resources
src/test/java Test Java source
src/test/resources Test resources

Related

Gradle include jar produced by another project in war

Currently I have two projects with gradle build.gradle. The first is going to create a fat jar file, which I would like to include in a war file. I thought compiling it would be enough, but it doesn't seem to be ending up in the /lib directory of my war file. Anyone have thoughts I am quite new to gradle.
dependencies {
compile project(':JarProject')
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
providedCompile 'org.apache.tomcat:tomcat-jsp-api:7.0.55'
}
war {
archiveName 'WarProject.war'
from 'JarProject/build/libs'
webXml = file('src/web.xml')
}
Does the second project war need to be in providedRuntime? Or should I publish the jar from the other project in the local maven repo and include it that way?
The War task essentially behaves like a CopyTask with regards to stuff it packs in the war, so the documentation on working with files is useful. In essence, I think you need something like (untested):
from fileTree('JarProject/build/libs') {
into("lib")
}
That being said, using mavenLocal() and publishing there also works, but it can lead to unexpected results when the war includes some old version from local, picking up the jar explicitly from the file system like above is better.
I think the elegant solution would be to use multi project builds and project level dependencies. You would have the two builds as separate projects of the same Gradle build and add the "jar project" as a regular compile dependency.
How have you declared the dependency? I assume you have a multi-project build with subprojects A and B, both using the War plugin. I made an experiment using Gradle 2.4 and if I declare B/build.gradle like this:
apply plugin: 'war'
dependencies {
compile project(':A')
}
then B.war contains WEB-INF/lib/A.jar. If you correctly follow conventions of Gradle War plugin (place web resources in A/src/main/webapp/ and code-related resources in A/src/main/resources/), then A.jar should contain what you want.
see this

Is it possible to use resource files from jar in gradle?

I want to use resource files from two jar files. Is it possible ?
build.gradle
sourceSets {
main {
resources {
srcDir 'src/resources'
files('libs/myOwnFile-1.jar:src/resources')
//add another resource jar file
}
}
}
This means , I want to include resource files (mainly xml files) also from libs/myOwnFile-1.jar file. The jar is creating by other team but it's another project which cannot be linked with this project. But I know the path of resources (src/resources)
Is it possible to use src/resources under jar file ?
If an external JAR contains resources you want to use, consider just adding that JAR as a dependency to your project. That way, you can also use these resources in your own code.
For example, the following code will add all JARs in the libs folder as dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The sourceSets property on the other hand is typically for sources/resources that are part of the project itself (and aren't part of another JAR).

You can't change configuration 'providedRuntime' because it is already resolved

I am using Gradle 2.0 with Groovy 2.3.3.
When I run the build below I get the error > You can't change configuration 'providedRuntime' because it is already resolved!
Other posts and release notes suggest that it is to do with +=, however, I'm not using that operator so I am a bit confused.
apply plugin: 'war'
repositories {
mavenCentral()
}
//We don't want transitive dependencies added as we are matching a third-party build
configurations.all {
transitive = false
}
war {
archiveName='gradle.war'
from(configurations.providedRuntime.files) {
into "app-jars"
}
classpath fileTree('webinf-libs') // adds a file-set to the WEB-INF/lib dir.
}
dependencies {
providedRuntime group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.2'
}
Changing war configuration to:
war {
archiveName='gradle.war'
from(configurations.providedRuntime) {
into "app-jars"
}
classpath fileTree('webinf-libs') // adds a file-set to the WEB-INF/lib dir.
}
will solve the issue.
The problem occurred because files was called in war configuration block and then dependencies were added to providedRuntime. Since calling files resolves the configuration (and war block is evaluated at configuration phase) it can't be modified later on.
You can also change order of dependencies and war and it will be the same.

Gradle: How can I access files from one subproject in another subproject via configuration?

I have a gradle build with many subprojects. One subproject generates a WAR and needs to include xhtml files from ui-module subprojects that will be included in the WAR. At the moment I search for the ui-modules programatically and copy the files. But I was wondering if I could solve it with a separate configuration.
I want to generate zip files with the xhtml files into the configuration uiModule as artifacts. Later the artifacts should be unzipped into the war. My build.gradle for the ui-modules looks like this
configurations {
uiModule
}
task zipXhtmlFiles(type: Zip) {
from 'xhtmlResources'
}
artifacts {
uiModule zipXhtmlFiles
}
The war should access the xhtmlFiles like this
apply plugin: 'war'
dependencies {
uiModule project('someUiModuleProject')
}
war {
//unzipping will be done later
from configurations.uiModule.allArtifacts
}
However, when I create the war, the zipXhtmlFiles is not executed and when I print the arifacts they don't contain the zip files. Can this be solved without writing an extra task class? Am I getting something wrong?

Gradle multiproject gives "Could not find property 'sourceSets' on project" error

I had quite good gradle configuration, that built everything just fine. But one of the projects of my multi-project build derived from the rest of them so much, that I would gladly move it to another git repo and configure submodules to handle it.
First, I moved Project and its resources to subfolder Libraries/MovedProject. After altering some lines in gradle configurations it worked fine. But then I decided to write a new build.gradle just for this project, and move all configurations there from the main one.
And this is where everything stopped working. When I try to call any task it always ends
with Could not find property 'sourceSets' on project ':Libraries/MovedProject'. Line which is responsible for it is:
dependencies {
...
if (noEclipseTask) {
testCompile project(':Libraries/MovedLibrary').sourceSets.test.output
}
}
which I use for running tests in which I use classes from other projects. If I remove that line, the build fails only when it reaches compileTestJava task of projects that make use of MovedProject. If I remove that line and call gradle :Libraries/MovedLibrary:properties I can see :
...
sourceCompatibility: 1.7
sourceSets: [source set main, source set test]
standardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager#1e263938
...
while gradle :Libraries/MovedLibrary:build builds correctly.
Currently I've got everything set up as following:
directories:
/SomeMainProject1
/SomeMainProject2
/SomeMainProject3
/Libraries
/MovedProject
build.gradle
dependencies.gradle
project.gradle
tasks.gradle
/Builder
dependencies.gradle
project.gradle
tasks.gradle
build.gradle
settings.gradle
settings.gradle
include Libraries/MovedProject,
SomeMainProject1,
SomeMainProject2,
SomeMainProject3
sourceSets for MovedProject are defined in Libraries/MovedProject/project.gradle:
sourceSets {
main {
java {
srcDir 'src'
srcDir 'resources'
}
resources { srcDir 'resources' }
}
test { java {
srcDir 'test/unit'
} }
}
dependencies that makes use of sourceSets.test.output are stored in Builder/dependancies.gradle, and set for each project that needs MovedProject to run tests:
project(':SomeMainProject1') {
dependencies {
...
if (noEclipseTask) {
testCompile project(':Libraries/net.jsdpu').sourceSets.test.output
}
}
}
What would be the easiest way to get rid of that error and make gradle build projects with current directory structure? I would like to understand why gradle cannot see that property.
The line in question is problematic because it makes the assumption that project :Libraries/MovedLibrary is evaluated (not executed) before the current project, which may not be the case. And if it's not, the source sets of the other project will not have been configured yet. (There won't even be a sourceSets property because the java-base plugin hasn't been applied yet.)
In general, it's best not to reach out into project models of other projects, especially if they aren't children of the current project. In the case of project A using project B's test code, the recommended solution is to have project B expose a test Jar (via an artifacts {} block) that is then consumed by project A.
If you want to keep things as they are, you may be able to work around the problem by using gradle.projectsEvaluated {} or project.evaluationDependsOn(). See the Gradle Build Language Reference for more information.
I had a similar error happen to me in a multimodule project, but for me the cause was as simple as I had forgotten to apply the java-library plugin within the configurations, I only had maven-publish plugin in use.
Once I added the plugin, sourceSets was found normally:
configure(subprojects) {
apply plugin: 'maven-publish'
apply plugin: 'java-library'
....

Resources