just started with the codenarc plugin for grails - seems to be great!
But now I try to configure a dynamic report title like this:
"code quality report for ${appName}"
Unfortunately, it seems that the appName property is not available at the right time - I just get a null value.
Any ideas?
PS: using Grails 2.0.4 with ":codenarc:0.19"
appName is by default bound in BuildConfig.groovy but is not available to the configuration closure for codenarc. I am yet to become cognizant of an absolute reasoning behind that behavior, unless anyone here throws some light on it. But you can very well read application.properties directly to get the appName as below:
codenarc.reports = {
def props = new Properties()
new File("application.properties").withInputStream {props.load(it)}
MyHtmlReport('html') {
outputFile = "${props['app.name']} - CodeNarc-Report.html"
title = "${props['app.name']} - Sample Report"
}
}
Related
I'm currently spending some time setting up a generic configuration using Jenkins AD-SecurityRealm (ActiveDirectorySecurityRealm) Plugin (v2.6) and stuck over a nasty issue: It seems that my approach to (auto) set up a valid AD-connection (following the corresponding documentation) is not working at all. Every time I'll re-init my Jenkins instance an incomplete config.xml will provide - the "bindName" property (XML-node) is always missing. This property is required by the ad-server I'll use and so I've to override the config manually to solve this issue.
I haven't the vaguest idea why this still happens.
my groovy code (excerpt)
String _domain = 'my-primary-ad-server-running.acme.org'
String _site = 'jenkins.acme.org'
String _bindName = 'ad-bind-user'
String _bindPassword = 'ad-bind-password-super-secret-123'
String _server = 'my-primary-ad-server-running.acme.org'
def hudsonActiveDirectoryRealm = new ActiveDirectorySecurityRealm(_domain, _site, _bindName, _bindPassword, _server)
def instance = Jenkins.getInstance()
instance.setSecurityRealm(hudsonActiveDirectoryRealm)
instance.save()
my config.xml result (excerpt)
<securityRealm class="hudson.plugins.active_directory.ActiveDirectorySecurityRealm" plugin="active-directory#2.6">
<domains>
<hudson.plugins.active__directory.ActiveDirectoryDomain>
<name>my-primary-ad-server-running.acme.org</name>
<servers>my-primary-ad-server-running.acme.org:3268</servers>
<bindPassword>{###-fancy-crypted-super-password-nobody-can-decrypt-anymore-###}</bindPassword>
</hudson.plugins.active__directory.ActiveDirectoryDomain>
</domains>
<startTls>true</startTls>
<groupLookupStrategy>AUTO</groupLookupStrategy>
<removeIrrelevantGroups>false</removeIrrelevantGroups>
<tlsConfiguration>TRUST_ALL_CERTIFICATES</tlsConfiguration>
</securityRealm>
my config.xml required (excerpt)
<securityRealm class="hudson.plugins.active_directory.ActiveDirectorySecurityRealm" plugin="active-directory#2.6">
<domains>
<hudson.plugins.active__directory.ActiveDirectoryDomain>
<name>my-primary-ad-server-running.acme.org</name>
<servers>my-primary-ad-server-running.acme.org:3268</servers>
<bindName>ad-bind-user</bindName>
<bindPassword>{###-fancy-crypted-super-password-nobody-can-decrypt-anymore-###}</bindPassword>
</hudson.plugins.active__directory.ActiveDirectoryDomain>
</domains>
<startTls>true</startTls>
<groupLookupStrategy>AUTO</groupLookupStrategy>
<removeIrrelevantGroups>false</removeIrrelevantGroups>
<tlsConfiguration>TRUST_ALL_CERTIFICATES</tlsConfiguration>
</securityRealm>
Thanks #kosta.
Following script also works using active-directory 2.10 and jenkins 2.150.1
This also include site information.
import hudson.plugins.active_directory.ActiveDirectoryDomain
import hudson.plugins.active_directory.ActiveDirectorySecurityRealm
import hudson.plugins.active_directory.GroupLookupStrategy
String _domain = 'dev.test.com'
String _site = 'HQ'
String _bindName = 'dev\jenkins'
String _bindPassword = 'test'
String _server = 'dev.test.com:2328'
def hudsonActiveDirectoryRealm = new ActiveDirectorySecurityRealm(_domain, _site, _bindName, _bindPassword, _server)
hudsonActiveDirectoryRealm.getDomains().each({
it.bindName = hudsonActiveDirectoryRealm.bindName
it.bindPassword = hudsonActiveDirectoryRealm.bindPassword
it.site = hudsonActiveDirectoryRealm.site
})
def instance = Jenkins.getInstance()
instance.setSecurityRealm(hudsonActiveDirectoryRealm)
instance.save()
Check this screenshot: Configure Global Security
If you look at the source code for ActiveDirectorySecurityRealm you will see that the bindName is marked as transient, thus it won't be persisted as part of the config XML.
The only solution to get the desired config.xml is to force the config.xml by providing a custom static one and not use the init script.
I was able to solve this issue by adding the following code at the end (Tested on 2.6 and 2.8). You also need to make sure that your credentials are valid because the plugin is doing an initial connectivity check https://issues.jenkins-ci.org/browse/JENKINS-48513
hudsonActiveDirectoryRealm.getDomains().each({
it.bindName = hudsonActiveDirectoryRealm.bindName
it.bindPassword = hudsonActiveDirectoryRealm.bindPassword
})
instance.setSecurityRealm(hudsonActiveDirectoryRealm)
instance.save()
I'm running a test with Grails 2.3.8 and an external configuration, yet the value doesn't seem to be coming through. I've tried this in older versions and am not seeing the same error. Did something change or am I missing something that I fat fingered? I am using an absolute path and the file definitely exists because Grails sees the key.
Config.groovy
reliable.string = "This string is working"
grails.config.locations = ["file:/home/howes/Project/project/test-config.groovy"]
/home/howes/Project/project/test-config.groovy
test.externalstring = "this is a test"
To test it I made a controller and just called grailsApplication to pull out the values. The first time I load the page I get a blank map, and when I refresh I see the key but no value. To make sure I am pulling everything correctly I am outputting the test-config.groovy item and one from the basic Config.groovy.
TestContoller.groovy
class TestController {
def grailsApplication
def index() {
println grailsApplication.config.test
println grailsApplication.config.reliable
return [ext:grailsApplication.config.test.externalstring, ext2:grailsApplication.config.reliable.string]
}
}
Console Output (First Load)
[:]
[string:This string is working]
Console Output (Page Refresh)
[externalstring:[:]]
[string:This string is working]
What am I missing here?
I was trying to follow the step-by-step instructions in Grails In Action (http://www.manning.com/gsmith2/GiA2E_meap_ch01.pdf), and the scaffolding explained in section 1.5.1, Pg. 21-23 does not seem to be working for me.
I added the static scaffold = true in the QuoteController.groovy as suggested. Then did grails run-app, and when I head to localhost:8080/qotd/quote/list I get a 404 error (instead of the Figure 1.11 in the pdf) as follows:
HTTP Status 404 - /qotd/quote/list
type Status report
message /qotd/quote/list
description The requested resource is not available.
Apache Tomcat/7.0.42
Here is the QuoteController.groovy:
package qotd
class QuoteController {
static scaffold = true
def index() {
redirect(action: "home")
}
def home() {
render "Real Programmers do not eat Quiche"
}
def random() {
def allQuotes = Quote.list()
def randomQuote
def n = allQuotes.size()
if (n > 0){
def randomIdx = new Random().nextInt(n)
randomQuote = allQuotes[randomIdx]
} else{
String str = "Real Programmers Don't Eat Quiche" + n
randomQuote = new Quote(author: "Anonymous",
content: str)
}
[quote: randomQuote]
}
}
However, going to localhost:8080/qotd/quote/create works fine (matches with Figure 1.12 in the pdf), and I am able to create a new quote.
The versions I am using are:
App version: 0.1
Grails version: 2.3.1
Groovy version: 2.1.8
JVM version: 1.7.0_45
Is this a bug in Grails or I am missing something?
I am new to Groovy and Grails, and any help would be highly appreciated.
Thank you!
The list action has been removed for some reason. Use index instead.
There are more changes now with version 2.4.2.
The following url explains how scaffolding has been moved to the plugin model:
http://grails.org/doc/latest/guide/scaffolding.html
"As of Grails 2.3, the scaffolding feature has been moved to a plugin. By default this is configured for installation in new applications, but if you are upgrading from a previous version of Grails you will need to add the following configuration to your BuildConfig.groovy file..."
So, inside the plugins { } section add this line:
compile ":scaffolding:2.0.0"
Also, use the action 'create' to force data into your database if it's still empty.
For example:
localhost:8080/myapp/mycont/create
Then try to see if you can load it with:
localhost:8080/myapp/mycont/show/1
Replace:
myapp --> with your application name (used in 'grails create-app')
mycont --> your controller name (used in 'grails create-controller')
I'm creating a Jenkins plugin which is a post-build action. I want the plugin to read the value of the "Root POM" field in the job configuration page. I've been looking through the Javadocs for hudson.model.AbstractBuild and trying getBuildVariables(), getEnvironment() etc. but I don't see anything relevant.
I guess as a last resort I could configure my plugin to prompt the user for the root pom, but the problem is that management wants a plugin that can be deployed automatically on every build without any action on the user's part.
Do you mean you want a plugin to read the configuration of another plugin (the maven one)? If so, I believe you should use something like
Jenkins.getInstance().getDescriptor(RequiredDesc.class);
Your required class might be hudson/maven/Maven3Builder depending on what you are trying to do.
Discussion
Update
I was wrong. This seems to work for me:
if (build instanceof hudson.maven.MavenModuleSetBuild) {
try {
hudson.maven.MavenModuleSetBuild b = (hudson.maven.MavenModuleSetBuild) build;
hudson.EnvVars envVars = b.getEnvironment(listener);
String rootPOM = b.getProject().getRootPOM(envVars);
listener.getLogger().println("rootPOM: " + rootPOM);
} catch (Exception e) {
listener.getLogger().println("ERROR: " + e.getMessage());
}
}
I'm developing my first Grails plugin. It has to access a webservice. The Plugin will obviously need the webservice url. What is the best way to configure this without hardcoding it into the Groovy classes? It would be nice with different config for different environments.
You might want to Keep It Simple(tm). You may define the URL directly in Config.groovy -including per-environment settings- and access it from your plugin as needed using grailsApplication.config (in most cases) or a ConfigurationHolder.config object (See further details in the manual).
As an added bonus that setting may also be defined in standard Java property files or on other configuration files specified in grails.config.locations.
e.g. in Config.groovy
// This will be the default value...
myPlugin.url=http://somewhe.re/test/endpoint
environments {
production {
// ...except when running in production mode
myPlugin.url=http://somewhe.re/for-real/endpoint
}
}
later, in a service provided by your plugin
import org.codehaus.groovy.grails.commons.ConfigurationHolder
class MyPluginService {
def url = ConfigurationHolder.config.myPlugin.url
// ...
}
If its only a small (read: one item) config option, it might just be easier to slurp in a properties file. If there are some number of configuration options, and some of them should be dynamic, i would suggest doing what the Acegi Security plugin does - add a file to /grails-app/conf/plugin_name_config.groovy perhaps.
added bonus is that the user can execute groovy code to compute their configuration options (much better over using properties files), as well as being able to do different environments with ease.
check out http://groovy.codehaus.org/ConfigSlurper , which is what grails internally use to slurp configs like config.groovy.
//e.g. in /grails-app/conf/MyWebServicePluginConfig.groovy
somePluginName {
production {
property1 = "some string"
}
test {
property1 = "another"
}
}
//in your myWebServicePlugin.groovy file, perhaps in the doWithSpring closure
GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader())
ConfigObject config
try {
config = new ConfigSlurper().parse(classLoader.loadClass('MyWebServicePluginConfig'))
} catch (Exception e) {/*??handle or what? use default here?*/}
assert config.test.property1.equals("another") == true