Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, September 25, 2009

Subclipse handles file renaming (refactoring) of Java classes properly

I started playing with CollabNet desktop and particularly Subclipse within Eclipse. Some things I liked.

Refactoring

Subclipse DOES handle refactoring of classes. That is, if you rename a class, it will create subversion events to delete the old class and add the new class. For example, renaming A.java to B.java shows a "Add B.java with copy-from path of A.java." This means that it preserves the version history over time.

To refactor without the subclipse plugin, I had to do the following:
  • Use "Rename" to rename the class. This fixes all the code and simply renames the existing file as the new file. (OldClass --> NewClass)
  • Do a SVN "Update" to get the old file back in the directory (OldClass.java)
  • Delete that OldClass.java file and rename the NewClass.java back to OldClass.java. SVN should show you that the file has been locally modified.
  • Do a SVN Rename to rename OldClass.java back to NewClass.java.
  • SVN "Check for Modifications" (status) should show you a "delete" of OldClass.java and a "add" of NewClass.java with a copy-from path of OldClass.java
Definately a hassle. It's nice that Subclipse makes it all work properly.

I also noticed that Subclipse and Tortoise SVN seem to "play nice" with checked out versions of code. That is, you can alternate back and forth between using Subclipse and Tortoise to operate on files.

Tuesday, September 22, 2009

HOWTO Include Subversion version identifiers in your Java source code with Eclipse

It's often useful to include version markers in your source code files, especially when it's possible they will be distributed outside the bounds of a version control system. In my case, I'm using Subversion and Eclipse to write code.

Subversion provides keyword substitution for special keywords that are managed by Subversion during checkin an checkout. (See: http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html). These include Date, HeadURL, Revision, Author, and Id (a combination of the others). You can embed these in your text files and SVN will automatically replace them with appropriate values each time. To enable this, you need to do the following:

svn propset svn:keywords "Id HeadURL Revision Author Date" foo.java

Then, you embed a keyword like "$Id" in your file and next time SVN updates your file, it will replace the $Id string with something like $Id: foo.java 148 2006-07-28 21:30:43Z sally $.

To make this easier, you might want to change the [auto-props] section of your config file to automatically set this property any time you create a new java file:

*.java: svn:keywords="
Id HeadURL Revision Author Date";svn:eol-style:native

Typically, this is embedded in a comment block. To make this easy, you can include it in your Eclipse file templates so things get pre-populated when you generate new files.

For general purpose insertion in all your Java source files, use Window > Preferences > Java > Code Style > Templates, then edit the "Files" entry and add the following to the template:

/*
*
* $$Id$$
*/

That will automatically insert the "$Id$" tag into any new source file you create.

Another reference:
http://wiki.collectionspace.org/display/collectionspace/Java+Source+Files+-+Beginning+Comments+Block+Template

For more advanced usage, you can embed a string in a variable that gets compiled into the object code and can be used to identify versions of binaries.

For Java, insert a piece of code that looks like this:

// version identifier automatically filled by svn:keywords "Id"
public static final String __class_id = "$Id$";

This will expand when the files are checked in to be representative of the version of code that was committed.

This can automatically be inserted in new classes with code templates, again. Window > Preferences > Java > Code Style > Templates > Code > Class body. (Make sure you use $$ for the dollar signs when you create the template.)

Pitfall: Make sure you have cleanly committed/updated files when you do this. Don't make the mistake of building your modified code that is checked out, then distributing it. Once you commit this code, the version number will bump up and potentially be different.


HOWTO specify default JVM arguments when using Java JAR manifest

A couple of times I've wanted to be able to specify a set of "default" JVM and program properties using the manifest file of a JAR file so that I could invoke it easily with "java -jar MyProgram." In my case, I wanted to set the JVM system property "https.proxyHost" so that all my web traffic would go through a local proxy.

Unfortunately, there doesn't appear to be a way to specify program arguments in a Java JAR manifest file.

To get around it, the solution is to use properties. I found a useful post here (http://www.velocityreviews.com/forums/t129370-vm-arguments-in-manifest.html) that suggests doing something like this:

static {
System.setProperty("https.proxyHost", proxyHost)
}

This solution actually worked quite well. I ended up writing a "JVMProxyHelper.setProxy(host,port)" routine that I simply invoked from the main() of the program I was running. If I get really fancy, I'll go back and make it read the property out of a file and register it that way, but this works for my quick need.

HOWTO Create Ant build.xml file from Eclipse Java project

I searched around and couldn't find an easy answer for this, but accidentally found it in Eclipse while doing something else. Basically, I created a simple Java program in Eclipse. I wanted to be able to build the program from the command line with ant, but didn't want to hand create the build.xml file when I knew that Eclipse should be able to generate a basic one for me. I kept looking in the "Project" menu and "New" menu for a way to do it.

Answer: It's in the "Export" menu. If you do Export > General > Ant build file, everything works fine.

Beware: it automatically over-writes any build.xml file that may exist with no warning. Don't make modifications unless you figure out a way to preserve them when things change.

Similar task: if you want to produce a runnable JAR file from your project, you can use Export > Java > Runnable Jar to do so. This can also create a custom Ant build file that automatically packs all the Jar stuff you need. NOTE that one of the things this does is re-pack any dependent Jar files you have into one big jar so you can run it with "java -jar."

If you have additional rules in the file that you would like to include in the generated build.xml, you can make Eclipse automatically import these files.

In the same directory, create "build-custom.xml" with the following lines:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse.ant.import?>
<project default="all" name="Create runnable JARs for everything">
<target name="all" depends="build">
... more stuff ...
</target>
</project>

Then export the build.xml file from Eclipse. It will now include a line
<import file="build-custom.xml">
near the beginning. All rules you define in your custom XML file will appear
as part of the build.xml file. You could now execute "ant all" to invoke your "all" rule from the main command line.

NOTE: I think your filename must start with the word "build....xml" in order for Eclipse to find it.