I have the following structure:
src/main/java/com/company/SomeJavaFile.java
src/main/java/com/company/template_file.ftl
When I create a build using gradle, the *.ftl files dont get included in the war file.
How can I make gradle include them in the war file?
Another solution would be to put your *.ftl files into src/main/resources directory instead.
Ok, found it. You simply add the following line to your build file:
gradle.build file (add the following line):
sourceSets.main.resources.srcDir 'src/main/java'
Got the answer from the following discussion thread:
http://gradle.1045684.n5.nabble.com/Copy-non-java-files-into-the-target-directory-td1432058.html
Why not put them in src/main/webapp where, if you used them, *.jsp files would go?
Related
I am currently working with a project (using gradle) that needs a properties file in the same package to the class that consumes it (I can't change this config). I have added the file in my package, but when I execute the gradle build command omits the file, and only adds the .class file. The app is deployed as a war file.
I have tried editing the war task, but I can't find the way to make the properties file to be added:
war {
from('src/main/com/foo/bar') {
include 'b.properties'
into 'WEB-INF/classes/com/foo/bar'
}
}
The war task does execute (I have added a prinln and it executes). Also, I have tried changing the paths, but no results. Also replacing from('path') to from (file('path')), but still doesn't seem to add the file anywhere.
How should I achieve this? Thanks!
Is there anything stopping you from following Gradle's convention for resources?
Eg put your property file at src/main/resources/com/foo/bar/b.properties
If you do this you won't need any task customisations
Searching in vain for examples on this humble task, none worked, I need to learn how to use gradle to copy one specific file from the main directory of a gradle project (containing subprojects) and into the /build/libs folder of one of the subprojects. Structure is this:
mainProject folder
file.txt
Subproject folder
build folder
libs folder
(I want to copy file.txt here)
This copy process shall be in the build.gradle (for the Subproject) file. I am using Android Studio and the subproject is a pure java application.
Thanks for any help.
You need to implement a custom task of type Copy to copy the file to the target directory. To access the file in the root project, you may use the rootProject property of the subproject.
task copyFileFromRootProject(type: Copy) {
from "${rootProject.projectDir}/file.txt"
into 'build/libs'
}
Gradle v2.9.x (old I know, but that's another project)
I tried the solution suggested in gradle: how to access the gradle.properties from another project to no avail.
I want to define artifact versions on a top-level gradle.properties file like so
toplevel/gradle.properties file:
artifact1_version='1.0.0'
Then I want to be able to use the above variable in artifact1_version variable like so
toplevel/artifact1/gradle.properties file:
version="$artifact1_version"
Is this possible to do in Gradle? If so how? I come from the Maven world so sorry if this is obvious.
The trick is put the variables in the build.gradle files, like this.
toplevel/build.gradle file:
ext.artifact1_version='1.0.0'
Then in the subprojects, do
toplevel/artifact1/build.gradle file:
version="$parent.artifact1_version"
In the Gradle samples (included with version 2.2.1) there is a java/multiproject project.
The settings.gradle file defines the following projects:
include "shared", "api", "services:webservice", "services:shared"
Note that services is not itself a project, merely a directory which contains the webservice and shared projects.
When I run the command gradle build from the root directory, I notice that after gradle successfully builds it creates inside the /services directory a /build directory containing /lib and a /tmp directories.
Inside of /services/build/lib is a jar: services-1.0.jar which contains very little; specifically just a META-INF/MANIFEST.MF file containing:
Manifest-Version: 1.0
provider: gradle
So what is causing Gradle to build a jar for this non-project? And how can I prevent this behavior in my similarly structured multiproject project?
/services isn't a project, I don't want to create anything inside /build folder at all. Yes I could just delete it, but I would like to avoid the unnecessary work of building this jar/running any tasks on this non-project in the first place.
To be honest I've no reasonable idea why gradle builds this folder. I guess that because it's a kind of a transient folder. However it can be excluded by adding the following piece of code to main build.gradle script:
project(':services').jar { onlyIf { false } }
Desired effect (services.jar elimination) can be also obtained with the following settings.gradle content:
include "shared", "api", "services/webservice", "services/shared"
File instead of project paths are included.
My guess would be that this is a combination of the next 2 gradle rules:
When you're including subprojects in the build.settings file using the include keyword according to Gradle Documentation here:
the inclusion of the path 'services:hotels:api' will result in
creating 3 projects: 'services', 'services:hotels' and
'services:hotels:api'.
In simple words, this means that the inclusion of services::webservice will also build the services project
The bulid.gradle file in your root that applies the 'java' plugin. According to Gradle Documentation here every configuration defined in the root.gradle takes effect for all sub projects. This means that it will also hold as the default configuration for the services project. As the 'java' plugin was applied a jar will be created, but as there is no src/main folder under the services directory nothing will be compiled and the jar will include only a META-INF/MANIFEST.MF file.
How can I add META-INF/context.xml into the war? I didn't find any config entry in config/warble.rb.
Unfortunately Nick's method doesn't work. The file is actually copied to WEB-INF/META-INF/context.xml.
I finally figure out a way to copy context.xml to META-INF:
create META-INF/context.xml under your rails app root folder
uncomment and change the following line in config/warble.rb
config.public_html = FileList["public/**/*", "doc/**/*", "META-INF/context.xml" ]
Basically treat META-INF as public_html, and it will be copied to webapps/youapp/META-INF.
You'll have to add one yourself. You can either create a META-INF/context.xml directory and file in your project and add META-INF to config.dirs in config/warble.rb or you can add a "pathmap" to rename the context.xml file into the META-INF directory in the war file.
config.pathmaps.application += ["%{context.xml,META-INF/context.xml}p"]
A better way of tackling this might be to use the following in your warble.rb file.
config.script_files << 'path_to_file/context.xml'
See documentation towards bottom of https://github.com/jruby/warbler/blob/master/lib/warbler/config.rb
# These file will be placed in the META-INF directory of the jar or war that warbler
# produces. They are primarily used as launchers by the runnable feature.
attr_accessor :script_files