What does Gradle do with the files, which are located in src/main/resources directory? - gradle

I'm trying to figure out, what Gradle does with the files, which are located in src/main/resources directory.

The processResources task, added by the java plugin to the project, copies them to the build/resources/main directory.
The content of this directory is bundled into the jar file created by the jar task that is also added by the java plugin, and the resources can thus be loaded, at runtime, by the ClassLoader.
See the documentation of the java plugin.

it might do nothing with them, but ignore them - per default (with the Android plugin) that directory is called res, only the Java plugin would take the resources directory into account (the question does not indicate which plugin is used). otherwise it would run a processResources task on them; only res/raw is not being processed (copied 1:1).

Related

In Maven, how to compile a class outside the source directory into an arbitrary target directory?

I have a legacy app that I'm porting from Ant to Maven. My Maven build works fine for the main project, which I've moved into the standard Maven directory layout (*.java files in /src/main/java/) and it outputs the compiled classes into /target/classes/ as neat as you could wish. These are packaged in a .war file.
However, the project also has a class outside of the folder hierarchy, indeed outside of the web application, that contains scripts that run via cron job. Let's say it's /cronjobs/MyClass.java. I need that class to be compiled and output to /target/cronjobs/MyClass.class and zipped up as part of the resulting .war file, in its /cronjobs/ folder.
Can Maven do this? I know it's possible to change the default "src" directory and "target" directory, but I don't know if (or how) it's possible to run a separate, parallel compile step for just one class.
I can move the source file, of course, if it's easier to compile it with the other classes and then move it later (maybe with the WAR plugin?) but I definitely need the compiled MyClass.class file in the /cronjobs/ directory of the .war.
I'd split the project in 2 parts, webapp as war and cronjobs as jar. Maven knows about multi-module format and it is somewhat the best way to go forward and decouple the webapp from non-webapp code.

How Do You Specify Where The Gradle Wrapper Installs Gradle?

I have an off the shelf application that ships a version of gradle with it. It also has scripts that are hard coded to set GRADLE_HOME to this location.
I want to zip up this dir, put it in nexus and replace it with the gradle wrapper.
How do I configure the gradle wrapper to download this zip from nexus and extract it to a specific location in the project?
EDIT: In the gradle-wrapper.properties I have
distributionPath=wrapper/gradle
However, I end up with it being unzipped to
...\wrapper\gradle\gradle-2.3-bin\8gn7esgljqyucijpbynjk93oc\gradle-2.3
How do I get it to unzip to the path I specified and not to the subdirs?
The location to which Gradle gets unpacked is a combination of the distributionBase and distributionPath properties in gradle-wrapper.properties file. The location specified by distributionPath will always be considered as relative to distributionBase. The only available values for distributionPath are GRADLE_USER_HOME and PROJECT. Even when using PROJECT the wrapper will still generate the folder structure you see above.
If you want to control this more precisely I'd suggest not relying on the wrapper to do this and instead add a task to your build specifically for this purpose.

How do I add a directory in /target to the resulting JAR with Spring Boot?

I'm using Enunciate to generate REST documentation upon building a REST application. Enunicate outputs documentation to /target/docs. I would like to add the /docs directory to the resulting JAR file (and rename it) to be able to serve docs as static content.
How do I do this? I can't figure out how to get these static files (which are generated upon build) into the JAR.
I guess you can solve this by configuring the Maven plugin for enunciate and wiring it up to be run in the 'generate-resources' lifecycle phase.
Also, make sure you set the output-dir to a subdirectory of src/main/resources/static, as commented by Rob above.
I added this to my enunciate.xml to force the docs directory to be generated in a custom location which will be packaged with the .war file
<docs docsDir="target/<app_name>/docs"/>
and then maven will put the entire contents of target/ into the resulting war file package

Delete/Remove file from war with Gradle

I'm using gradle to build a Spring Boot application, and I would like to have the application.properties file removed from the war, because that will be loaded externally (this is running in a tomcat container, not embedded).
I've looked around StackOverflow and the Gradle docs to try to figure out what to do, but I don't know which phase to tie into, and if I exclude the file before or after the war is created. There also seem to be multiple ways of dealing with files.
I believe Maven uses packagingExcludes for the equivalent.
Although I was not able to prevent a file from being added to the war, I was able to remove a file after the war was created - thanks in part to a tip from this question: Is there a quick way to delete a file from a Jar / war without having to extract the jar and recreate it?
In my build.gradle file I appended the war command with an exec command so that I could run a command after the war file had been created. The command will remove the application.properties file from the war. This is what the task extension looks like:
war << {
exec {
workingDir 'build/libs'
commandLine 'zip', '-d', "${appName}-${appVersion}.war", 'WEB-INF/classes/application.properties'
}
}
In short, it changes the working directory to the location that gradle places the war, and then uses the zip command to remove a file from the war.
You also have the option that if you don’t care for these properties files to be copied from the src/main/resources to the build/resources/main folder (not just excluded when build/resources/main is copied to the War), you could use:
In build.gradle file;
processResources {
exclude('application.properties')
}

CMYKJPEGImageReaderSpi not loading

I was having an issue of reading CMYK JPEG images , and have used below url as reference for solving the issue.
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/
I have given the configuration org.monte.media.jpeg.CMYKJPEGImageReaderSpi in the file javax.imageio.spi.ImageReaderSpi under path /META-INF/services/javax.imageio.spi.ImageReaderSpi.
This works perfectly inside eclipse and the image reader is loaded successfully.
This file is not loading when deployed , i can find the folder and the file in the generated war file in my desired jar file inside lib folder, i guess i need to add it to java classpath.
Please help me to add to classpath or if there is any other issue with it.
You need to add this file as a static resource to your build lifecycle.
For Ant or Gradle you just need to write a simple copy task (Ant task, Gradle task), for Maven you can use Maven Resources Plugin.
After that your file should appears in your app package.

Resources