Grails default package name - grails

I am new to Grails and I like it very much. I want to place my classes in packages like org.company.project.module.model. Its quite painful for to me to repeat create-domain-class <package>.<class_name>. Is there something like "package templates" or can I somehow "enter" (like grails cd org.comopany...) and then just write Class names (grails Person will be generated in ./ location)? Is that possible or should I use copy paste design pattern?
Thanks in advance for any help.

If I understood your question, you are looking for default package name for your domain classes. In your config.groovy file there is a line saying:
grails.project.groupId = appName
if you give it an appName, Grails will use that as the default package name when it generates the artifacts.
grails.project.groupId = 'com.example.yourpackagename'
If you now create a domain class by default it will locate it under com/example/yourpackagename.
UPDATE
It is not required to use Grails commands like create-domain-class or other commands to create artifacts. These are all just classes that you can manually create. Just create a file and duplicate it in the same package.
UPDATE
Grails interactive mode (when you type grails) for some of the commands by pressing tab it will type ahead the unique portion of the package name.
UPDATE for Grails 3.0
The setting has moved into conf/application.yml:
grails:
profile: web
codegen:
defaultPackage: com.example.yourpackagename

Related

Dynamic Controller Plugin for grails

I'am new to grails, I wanted to make use of Dynamic Controller Plugin (http://grails.org/plugin/dynamic-controller) in my project.
I am using grails version 3.2.11
I've added the dependency as directed on the page. It downloads the dependency in the form of zip, I can see it in External libraries. But when I am trying to import two classes (as directed on http://burtbeckwith.com/blog/?p=1041 Linking to existing Controller Actions
approach)
import com.burtbeckwith.grails.plugins.dynamiccontroller.ControllerClosureSource
import com.burtbeckwith.grails.plugins.dynamiccontroller.DynamicControllerManager
it gives " unable to resolve class" error. Please suggest what am I doing wrong here. Thanks!
You're trying to install a Grails 2 plugin in a Grails 3+ app, but that's not possible since they're not compatible. Grails 2 plugins must be upgraded and reworked to be used in Grails 3, and there's no plan to do so for this plugin.
I would say take a look at the URL Mappings & Embedded variables in the grails documentation.
https://docs.grails.org/3.2.11/guide/single.html#embeddedVariables
For example:
static mappings = {
"/blog/$topic"(controller: "blog")
}
which gives you a feeling like you are dynamically declaring actions.
And the topic variable is accessible through GrailsParameterMap params object # controller.
With this you can construct url like:
www.mysite.com/blog/football
www.mysite.com/blog/tvshow
www.mysite.com/blog/etc
Edit: you can also take a look at Dynamic Controller and Action Names [https://docs.grails.org/3.2.11/guide/single.html#_dynamic_controller_and_action_names]

Grails disable view generation of a domain class variable

I am working on a multi-tenant architecture based plugin, where I am adding a tenantId variable on few domain classes. Now this variable gets assigned its value automatically at object creation time via some code in Domain class itself and the User need not assign it manually.
Now the problem is that I need to provide this functionality to other developers and who actually generate the GSP views using grails generate-views com.something.someClass.
By doing this the generated view also has field for selection of tenant. So is there any domain class constraint or any setting that I can apply to prevent this variable from being automatically included in the view?
P.S. - Any such setting will be anytime better than manual removal of field from view each time.
Thanks.
Try follow steps:
1- run this command to copies the the templates used by Grails during code generation
grails install-templates
2- then open _form file (found in src/templates/scaffolding folder)
3- add tenantId in excludedProps variable like grails did with version field
excludedProps = Event.allEvents.toList() << 'version' << ... << 'tenantId'
Note- I haven't tried this.

iReports and grails. set javabeans data source and connection

I have some problems to configure iReports, I´m using iReport4.8.0 (I tried iReport4.7.1 before and the same problem happened) and grails ggts 3.0.0.
I have a domain class called Consulta in a package called consultas, I want to list them and create a report using ireports, I´ve followed the steps I found in a book: Beginning Groovy
and Grails From Novice to Professional CHAPTER 9 it says that create a report from groovy clases is very easy (in theory it is) you just have to create a jar with the classes compiled and place it in the reports lib directory, then select "javabeans set data source option from data connections where the only field to fill is the name, it says to set the others blank, when I test it an error pop-up appears saying ClassNotFoundError, I'm sure there are some missing steps but i don't know which.
in my consulta controller I have this code:
def report ={
def consultas = Consulta.list()
chain(controller: "jasper", action: "index", model: [data: consultas], params:params)
}
and I think jassper plugins are OK.

Grails Events: Copying directories around at Build time to get a clean 'multi-tenant' deployment effect

I'm working on a sort of 'multi-tenant' grails app that will be used as a 'platform' upon which quick sites will be developed.
A 'site' will include a layout, images, and page gsps.
Right now, these are spread across the project in their normal locations, eg:
/grails-app/views/layout
/grails-app/views/<site>
/web-app/images/
Ideally, they'd all be in one place, centralized by site, like
/sites/<site>/layout
/sites/<site>/pages/
/sites/<site>/imagtes
My current thinking is this could be accomplished with a Build.groovy script and doing some ant trickery at build time.
But I'm not sure if it's possible to do this copying-by-convention - ie I don't know the directories that are present until it runs. (I'm also no ant guru)
Any ideas/suggestions? Thanks!
I have used the following script, named _Events.groovy and located in a scripts folder under your grails application, to copy files before my build:
includeTargets << grailsScript("_GrailsEvents")
eventSetClasspath = { msg ->
println "Custom Configuration"
ant.copy(todir:classesDirPath) {
fileset(dir:"${basedir}/config")
}
}
And I suppose you could use something very similar.
Relevant grails documentation is here

one-off grails script for populating database

Update: as of Grails 1.3.6 one has access to the full domain from Gant scripts.
From the Grails 1.3.6 release notes:
You can now run one or more Groovy scripts from the commandline using the run-script command, e.g.
grails run-script [path-to-script-1] [path-to-script-2]...[path-to-script-n]
This works around the issue in Gant scripts where you can't conveniently access application classes since they're not available in the classpath when the scripts start.
Hi all,
I am new to using Grails (in a real project) and I have a one-off script I need to execute that reads a file and then populates my database.
I wanted the script to run in the context of my grails app, so I used the create-script command. I now understand that makes it a 'Gant' script. The reason for doing so was that I thought it would allow me easy access to all the grails domain good-ness, so that i would be able to do something like this easily:
Car car = new Car(model: 'bar', brand: 'Ford')
car.save()
Here, Car is one of my domain classes and the strings 'bar' and 'Ford' I have retrieved from my file.
The start of my script looks like this:
import com.foo.Car
grailsHome = Ant.project.properties."environment.GRAILS_HOME"
includeTargets << new File ( "${grailsHome}/scripts/Bootstrap.groovy" )
target(main: "a script for storing cars") {
depends(bootstrap, classpath) // code dealing with the file with cars follows
Surprisingly, groovy gives me a java.lang.NoClassDefFoundError: com.foo.Car when I execute the script with the command grails LoadCars
Am I taking the wrong approach, or is there something more simple I am doing wrong?
Any help is appreciated
i know the scripts are useful, and I will probably get hate mail for even suggesting it, but I have just incorporating this kinda of stuff directly into my application in the past.
I have a flag set in my configuration which indicates if the data should be bootstrapped, if so, the bootstrap code looks for a comma delimited file at startup and calls a service method to load up the data.
I've updated the grails run-script Gant script (referred to by Jared above) to work with grails 1.3.5. I'd been meaning to do it for a while, but this question nudged me into finally getting around to it).
Just download the script described in the post, save it in your grails "scripts" directory and you can then run your own groovy script to bootstrap data with:
grails run-script script-path/boostrapMyDataIntoApp.groovy
I've had to do this and you have to create a special script to allow you to access GORM from a standard grails script. See this question for more info. I'm not sure what the current status of the script is under grails 1.3 but the author of the script posted in the comments.
Hans, there are several choices here, assuming you are not out to polish the GANT scripting chops 8^)
So assume that you are doing some integration-mode TDD, correct?
Have you looked into the db-stuff plugin? Actually that one leverages the open source package (extension of the JUnit project) called dbUnit, which is also an outstanding choice, for both Java and Groovy projects.
*db-stuff <0.3.0> -- db schema managment and data import/export. Generate generic schema files and import or export base/seed/test data into your database.
I have traditionally done this as well in the BootStrap depending on the environment - and I try to never let those domain assumptions / constraints get too far out of synch. with my schema.
Here's the canon I'm talking about :
class BootStrap {
def init = { servletContext ->
if (GrailsUtil.environment.equals( GrailsApplication.ENV_DEVELOPMENT )) {
log.info( "Loading sample data for 2010 models..." );
new Car( manufacturer: new Manufacturer( name: "Toyota" ), model: "Prius" )
new Car( manufacturer: new Manufacturer( name: "GM" ), model: "Volt" )
//...

Resources