Intellij Class '' already exists - grails

I need to customize the spring security LogoutController a little bit so I have copied the controller from the plugin to the same package location in my grails-app/controllers
The controller is used instead of the original one because the plugins are compiled first -> the original will be overriden.
Now my problem is that intellij 12.1.7 complains that the Class 'LogoutController' already exists in package 'grails.plugin.security' ... ofcourse :( but thats no error !
I could not find anything to hide that error in the validation setup.

Grails controllers do not take package into account: only 1 LoginContoller will be used regardless of the package: (default url mapping is /$controller/$action?/$id? no place for package there).
The same goes for Domain classes.
To override the LoginController from ANY plugin in your app you just need to define a LoginController in the app, no matter the package.
However, if you define it in the exact same package of the plugin you're creating a conflict that would need to be resolved by class loader precedence. That should be the same (app first) but I wouldn't take that for granted. Hence the error IntelliJ is showing.

Related

Grails can't reference a domain class called Config in plugin

I created a Grails plugin and created a domain class in it called Config:
package com.company.common
class Config {
String defaultTimeZone
String defaultLanguage
}
In another Grails project, I'm including the plugin and I have this line in Bootstrap.groovy:
import com.company.common.Config
Every time I try to run the project though, I get this error:
C:\Code\grails-app\conf\BootStrap.groovy: 4: unable to resolve class com.company.common.Config
It works if I have the Config domain class in the same Grails project. It also seems to work if I rename it to something like ProjectConfig in the plugin project.
Does anyone know why I can't reference a domain class called Config in another project?
P.S. All the other domain classes in the plugin project can be accessed from the main Grails project, only the Config class gives me problems.
Grails plugins have specific files that are removed when packaging the plugin (so you can use them for testing). You can find the list here Grails Plugins #Creating and Installing Plugins. For convenience, here is the list:
grails-app/conf/BootStrap.groovy
grails-app/conf/BuildConfig.groovy (although it is used to generate dependencies.groovy)
grails-app/conf/Config.groovy
grails-app/conf/DataSource.groovy (and any other *DataSource.groovy)
grails-app/conf/UrlMappings.groovy
grails-app/conf/spring/resources.groovy
Everything within /web-app/WEB-INF
Everything within /web-app/plugins/**
Everything within /test/**
SCM management files within **/.svn/** and **/CVS/**
While this list does not include Config.groovy files in domain class folders, it might be caught by this. Would be worth checking if the Config.groovy file gets carried over to the packaged plugin, or if you can live with renaming the file, that would work as well.

Getting "unable to resolve class" exception when importing application domain objects into a plugin in grails

I am working with the Grails Authentication plugin and trying to add a domain class from the plugin into my GORM. I am able to use plugin objects in my application when importing them with, "import com.grailsrocks.authentication.AuthenticationUser", but getting "unable to resolve class" exception when trying to access my application objects from the plugin (I am trying to use the domain class "User" and my import command is "import blap.User" - package name is blap). Both import commands work from the shell, and the import statement is not triggering an error in STS.
I am new to grails, so I'm probably doing something very wrong. But, at this point I am running out of ideas, so any help would be greatly appreciated. Thanks!
Vitaly
While I haven't used the Grails Authentication plugin before, I don't think you should be modifying the plugin classes. In general, you should extend the plugin class you want to modify in your application and use your application class instead.
Actually, from reading the docs, it looks like you should use the event handling to modify the plugin behavior.
From the plugin docs:
The default AuthenticationUser domain class is minimal. If you want to change constraints or add fields (you may consider using a separate class instead for extra user data) you just redefine the onNewUserObject event and return your own instance of a domain class or similar wrapper around another authentication database such as LDAP

grails-doc creates copies of my classes in default package

In my grails 1.3.7 project, I have put all of my classes in com.mycompany.myapp, as you do. So this goes for services, controllers, domain classes. I have a filter that goes in its own package. My app works fine.
However, when I run grails doc, grails decides to create two pages for every class:
one in its right comp.mycompany.myapp package that has all the right Groovy Doc
the other takes all the above classes and pretends as if those also live in the default package.
So, target/docs contains two directories: 'DefaultPackage' and 'com', with DefaultPackage holding a copy of everything that lives under com/
Consequently, my groovy doc looks messy because there is two copies for each class.
How can I solve this?
It has been documented as a bug at GRAILS-6605. There is no workaround listed there for the bug.
I too faced the same issue and so created a plugin "Grails Runtime Docs" ( http://grails.org/plugin/grails-runtime-docs ) that solves this issue and generates both Java and groovy docs properly only 1 copy per class. It's grails aware and categorizes the classes into Controllers, Commands, Domains, Services and Tag Libraries. The groovy documentation is actually generated from runtime so as to include the dynamic methods also, adding "Dynamic Method Summary" & "Dynamic Method Detail" in the generated html docs, that provide their source information. Hope you find it useful.

Getting Grails not to create packages from hyphenated app names

If I create a Grails app called a-b-c-d, doing a grails create-domain-class User will result in Grails creating a class User in the sub-directory grails-app/domain/a/b/c/d, giving it the package a.b.c.d. How do I prevent Grails from creating these package names?
You should definitely use packages, but you can customize the default package by changing the value of grails.project.groupId in Config.groovy. The default value is appName which is your application name, but you can change it to any value package, e.g. 'com.foo.bar'.
In addition you can specify the package when running a create script, and if you do want to create classes in the default package, you can use this syntax:
grails create-service .Person
and it won't use a package.
I have no idea, this sounds like a bug. There are two obvious workarounds
change your app to have a name without dashes
don't use the Grails commands create-domain-class, create-controller-class, etc. I never use these commands because they don't actually do anything other than creating the class (and a corresponding empty test class). Personally, I find it easier just to create the class myself than to run the Grails command
You can specify the package when you call the cli command...
grails create-domain-class your.package.name.User

Grails using a package to hold domain classes

Grails question: Confused about using a package to hold domain classes.
I'm using Netbeans on Mac to check out Grails.
When I create domain classes without using a package holding it, I can just go to localhost:8080/gTunes and the expected .gsp page is rendered properly.
However when I use a package (com.g2one.gtunes) to hold a domain class (Song), I don't see the .gsp page when I go to localhost:8080/gTunes. Instead I see a Directory view of folders/files such as META-INF, WEB-INF, etc. In order to see the expected .gsp page, I have to type in the specific URL localhost:8080/gTunes/index.gsp
In my research I've seen people talk about adding the following line but I can't figure out where to add it.
<%#page import="path.to.domains.*"%>
Any help is appreciated.
Thank you.
You need to add that import to the top of your GSP pages that use the domain class that you put in a package. Don't forget you'll also need an import in your controllers unless that are in the same package.
Add the specified line as the very first line in the GSP you are trying to render (show).
Figured out why I way I was having this issue. I'm going through Definitive Guide to Grails 2nd Edt on mac with NetBeans. I ran into this problem when I
created 'gtunes' project at /Users/name/NetBeans/gtunes
deleted 'gtunes' project and /Users/name/NetBeans/gtunes
created another project with same name at /Users/name/NetBeans/gtunes. I type in some code and run it and I run into the error I posted.
To get around this error,
I create 'gtunes' project located at /Users/name/NetBeans/gtunes
I right click on 'gtunes' project in NetBeans, issue 'Clean' command. Than I see some files are removed from ~/.grails/... in Output window
I right click on Album.groovy (possibly Song.groovy also) and issue 'Generate Views', NOT 'Generate All'
Run the project and it works!
Thanks all for comments and hopefully my mistake will help others avoid same mistake.

Resources