Thursday, January 20, 2005

Changing your Creator package names

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.

In Creator, your project name is used as the java package path,and as the web application context name.

What if your project has grown and you want to change the name, eitherbecause you don't think webapplication24 is very professionalsounding in your context name (which shows up in the web app URL), or becauseyou don't like to see myfirstsimpletest as the package name on all your now complex page beans?

Unfortunately, there's no support in the IDE to do this yet. Which meansyou have to do it by hand.It's actually pretty straightforward, and there are no surprises; you justneed to change all references in the project from the old name to the newname. However, if you make any mistakes, it is difficult to track downthe problem. Pages will refuse to open, etc., because under the coversthe IDE may fail to locate the page bean for a JSP since it uses thepackage path to do the mapping, for example. Therefore, be careful,and I don't take any responsibility for any damage to your files, ormood, as a result of trying the below steps. Worked for me, your mileagemay vary.

First thing's first: Make a backup of your project. If things go wrong,you'll want to be able to start over - but starting over with therename operation, not starting over creating your project from scratch!

And while I have the red paint out:

Don't open your project in the IDE until you are completelydone! If there are inconsistencies in the project, the IDE willattempt to "fix" it, and suddenly you end up with multiplegetApplicationBean methods,the project file referring to both the old and the new package name, etc.

When you create a project, Creator takes your project name andcreates a "package name" out of it. This package name is usedin all the Java beans in your project, it's used as the deploymentcontext root, etc. The package name is derived by lowercasingyour project name, and removing all non-identifier characters from it, suchas punctuation, whitespace, etc. Therefore, "My Project" becomes"myproject". I'll use "myproject" as my sample old name in thefollowing code fragments, and "New Name" as the new Project name(which means "newname" will be the new package name and context root).

Exit the IDE.

Rename the project directory - from MyProject to New Name.

Now comes the most labor intensive portion of the project transformation:you'll need to change the java package name, which will involve updatingall the java files. You can either use an IDE which supports refactoringto do this (Creator doesn't yet, but NetBeans does, as does a number ofother IDEs). If you use an IDE be careful though to make sure it doesn'tperform any other changes to your code.

For each Java file, update three things: the package declaration(first statement in the file) from package myproject;to package newname;, and update the return typesand casts in the getApplicationBean1 andgetSessionBean1 methods (4 lines to change). (Theseare not present in all files.)

If you've done all the above steps, do a final search throughyour code to make sure that all references to "My Project"and "myproject" have indeed been updated. Now try loading yourproject. It should both load and correctly deploy.In my case I had introduceda typo in one of the source files, so I got the new error liston project load (which was added in patch 6, so make sure youhave the latest bits).

You may have noticed in the last step that there is a separate entryin project.prj for thecontextPath. If all you care about is changing the contextpath which shows up in the project's URL when deployed, youMAY be able to just change this entry in the project file andleave all your code with the old package name. I have not tried that - soI have no idea if it works. If anyone tries it, let me know and I'llreport your findings.