Delete/Remove file from war with Gradle - 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')
}

Related

Jenkins Job - Creating a zip file with war file, appspec.yml and scripts folder

I have created a build with Jenkins for a spring boot application and it is creating a war file.
Now I want to create a second job which should create a zip file with the war file created and appsepc.yml file and a folder "scripts" folder which contains some shell script that the appspec.yml file uses. Can anyone let me know how to do this?
The job name is "Package" so the following is the structure where the different files are.
.jenkins\workspace\Package\target\cpproject.war
.jenkins\workspace\Package\appspec.yml
.jenkins\workspace\Package\scripts\after_install.sh
.jenkins\workspace\Package\scripts\before_install.sh
.jenkins\workspace\Package\scripts\start_server.sh
.jenkins\workspace\Package\scripts\stop_server.sh
Thank you.
See the Maven Assembly Plugin:
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files.
Currently it can create distributions in the following formats:
zip
...

Why does un-jar then jar again break my Spring Boot web app?

I built a Spring Boot web app (using Eclipse/STS) that has an embedded tomcat server.
I need to do some (obfuscation) stuff to classes inside the WAR file and the zip it back up again, but before I try that I'm unzipping and then zipping up again to make sure that will work.
But when I unzip it then zip it back up it won't run anymore ...
Steps to reproduce:
Run the application like so ... java -jar myapp.war ... it works fine. Kill the application.
"Un-zip" the war like so ... jar -xf myapp.war ... all the contents are extracted.
Delete the original war file ... rm myapp.war
"Re-zip" the contents into a war like so ... jar -cfv myapp.war .
Run the new war file ... java -jar myapp.war ... I get an error ...
no main manifest attribute, in myapp.war
When I extract the contents of the new war file, I see that my original /META-INF/MANIFEST.MF file has been replaced by:
Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
I'm probably doing something wrong on step (4) above. Any ideas? Is there a "Spring Boot"-specific thing I'm suppose to do?
Looks like I figured it out ... I have to use the m option and specify the manifest file like this ...
jar -cfm myapp.war ./META-INF/MANIFEST.MF .
I had incorrectly assumed it would just include the existing manifest file from that directory.

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

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).

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

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