Load different stylesheets when packaging a war file with Grails - maven

I want to package a Grails application for different brands. While generating a war file, I want to pass some custom parameter that refers to a certain brand and styles the application by loading the stylesheet for the brand.
I read online and one approach I found was with maven. I tried working with maven but I am stuck while initially compiling the application. The error is
Failed to execute goal org.grails:grails-maven-plugin:2.2.1:maven-compile
(default-maven-compile) on project foreAction: Forked Grails VM exited with error.
I am stuck as to what approach to take now. I searched for the above error and tried different solutions but nothing seems to work.
If there is a different way without using Maven I am willing to give it a shot.

You could always hook into the events using scripts/_Events.groovy and replace the appropraite CSS/assets. The documentation explains how to hook into build events.
Your code inside scripts/_Events.groovy might look something like this:
// This is called after the staging dir is prepared but before the war is packaged.
eventCreateWarStart = { name, stagingDir ->
// place your code here that copies your resources to the appropriate location in the staging directory.
Ant.copy(
file: System.getProperty('somePassedInFile'),
toFile: "${stagingDir}/assets/stylesheets/whatever.css",
overwrite: true
)
}
Then you can pass in the value of the source file from grails prod war like this:
grails prod war -DsomePassedInFile=/path/to/file.css
Hope this helps at least give you an idea of how you can accomplish this. (All written off the top of my head so beware of typos etc.)

Related

Maven: How to test that generated documents in target folder are properly generated?

I've got a Java/Maven project that uses RestDocs to generate documentation of our endpoints.
For the uninitiated of RestDocs, what it does is monitor tests during the build and use information from the tests to generate "snippets" that get used to create API documenation using asciidoc during the mvn package phase.
The problem I'm facing is that if the asciidoc (.adoc) file is referencing a non-existent snippet, the doc gets generated and will say something like:
"Unresolved directive in myDoc.adoc - include::{snippets}/error-example/response-fields.adoc[]"
Since this happens after the tests, I don't know how to test for something like this so that the build can stop and let the developer know that they need to update either the .adoc or the snippet.
Any guidance would be much appreciated.
Adding this to the configuration of the asciidoctor plugin fails the build when a snippet is not found that the .adoc is expecting:
<logHandler>
<outputToConsole>true</outputToConsole>
<failIf>
<containsText>include file not found</containsText>
</failIf>
</logHandler>

how to flip between local and remote repo in gradle with old maven plugin?

I am not using the new maven-publish plugin which looks awesome and would work perfectly for this use case because I keep reading how no one can get the signing of the jars to work (though I am open to an answer that explains that as well as I haven't found a blog on that saying it would work at this point).
Soooo, I would like easily to switch between publishing locally for testing and remotely. My current build.gradle file is
https://github.com/deanhiller/webpieces/blob/master/build.gradle
which has a commented out local repo and I keep flipping commenting on and off and would rather like to avoid that. ie. these repos...
//repository(url: "file://localhost/tmp/myRepo/")
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
I read a post on doing something here but this failed with cryptic closure errors...
Configuring multiple upload repositories in Gradle build
I put that in my subprojects section but that didn't work at all. Ideally, using the graph whenReady sounds the best as it would be cool to only use remote repo if I am doing the release target. I can't seem to get any of this to work though.
thanks,
Dean
EDIT: I was wrong, the below is only half working right now as when I ./gradlew jar, then I get this error... (so some targets work and others not...ouch)
Cannot cast object 'task ':uploadArchives'' with class 'org.gradle.api.DefaultTask_Decorated' to class 'org.gradle.api.tasks.Upload'
I did figure out a work around though it seems weird as targets in running tasks list are hidden now depending on if a variable is set which seems a little odd. I would think there is probably a better way of doing this so all tasks are still listed, but for now my build file works by just doing a
if(project.hasProperty('projVersion')) {
//task I want to be able to run
} else {
//other task I want to run when no projVersion
}
so it works...the tasks are just invisible now until I do ./gradlew -PprojVersion=X to view them.

Maven plugin: copy a file content into another file

I am looking for the appropriate plugin to copy a file content into another file.
My resource.xml has content like this:
<class>my.path.ResourceA</class>
<class>my.path.ResourceB</class>
<class>my.path.ResourceC</class>
and must be copied to destination.xml at the place of ${content}:
<aaa>some info</aaa>
${content}
What the proper maven plugin to do that task, please?
Thank you in advance.
Nic
In general with Maven, you must first thinkg WHAT you want to do. Might seem weird at first sight, but as Maven is an opinionated build tool, it will generally be not simple to do weird/workaround things ;-).
Here, you would need two things:
load that file into a "content" property (before resource filtering starts by binding to an early phase, obviously, see the next point)
just activate resource filtering and be done
Unfortunately, there's no well-known/standard plugin for the (1) able to load a file inside a property.
A possible way, before rewriting it through a dedicated plugin or so, would be by using antrun-maven-plugin (through LoadFile task?) or gmaven plugin to load that file into a property (during the initialize phase, for example, so that it happens before process-resources, see the documentation about lifecycle.)
Then, for (2), you simply have to activate filtering (see the standard documentation of the maven-resources-plugin).
The answer from Baptiste does not work with the antrun plugin, as the properties from the LoadFile task are Ant properties are not accessible in Maven for filtering.
Instead, one can use the readfiles-maven-plugin, which you can find here: https://github.com/chonton/readfiles-maven-plugin

AppHarbor Web.config transforms not being applied

I am developing an application in ServiceStack and am trying to sort out deployment on AppHarbor, however for some reason my web.config transforms are not being applied.
I had originally a Web.AppHarbor.config file and changed the Environment Setting to "AppHarbor" - once this failed to work after several updates, I gave up and changed the Environment setting to "Release" and copied the desired transformations into the Web.Release.Config file.
App gets deployed OK but config settings do not reflect the values in the transform file (I verify this by login on with twitter and seeing the callback url for Twitter Auth still tries to redirect me to localhost, which is one of the settings I change in my transform file)
I have also tried the transform tester tool and all works as expected.
Manually publishing the web application to a local folder correctly applies the transformations according to the selected configuration
Does anyone have this working? Is there something obvious I'm missing?
Thanks
It sounds like the Web.Release.config file is not included in the build output. You need to set the Build Action attribute to Content to include it in the build output.
You can confirm whether the file is included in the output by downloading the build from the log page.
I stumbled across this post because I was seeing the same lack of action myself. Upon closer inspection (about 15 times that is) of my Web.Release.config I realized one of the nodes in my config file was not marked xdt:Transform="Replace". Unsurprisingly it did nothing when deployed.

Property Replacement in Maven Site Content

I'm generating maven site content using the site plugin. I want to have a little table that shows my maven group id, artifact id, parent info, etc on the module. I don't see a plugin for it, so I was going to use the APT format and create a content page with a table for it. The documentation says I can use property replacement in the site descriptor via ${project.name} etc. This fails when i use it in both the descriptor and in my index.apt file. Has anybody seen this done or know how to do it?
The mvn site command generates a Project Summary page which has this information on it. Does that work for you?
For example, when I execute mvn site, get an output here target/site/project-summary.html where the page has a section like so:
Build Information
Field Value
GroupId com.a.b
ArtifactId myapp
Version 1.01.13-SNAPSHOT
Type jar
If you want to create pages which use placeholder like ${project.version} etc. you need to use index.apt.vm (velocity files) wher the replacement works. But for the information you have asked the solution which has been described (project-summary.html) is the right way cause it's automatically generated.

Resources