views:

43

answers:

1

I am looking for a library which would simplify the process of copying parts of the code from one project to another. Copying of certain parts of the code which belong to the same file is not required. I am aware that there exists some code generators which would enable me to build the generated code from scratch programatically, but I am looking for a library which would enable me to generate already hand-coded code elsewhere.

I would prefer an annotation-based tool which would enable me to "tag" the classes/files/packages I would like to copy. Something like this:

@CopyCode
class Foo {
  ...
}

File destinationDirectory;
...
new CodeCopier().copyAllAnnotatedFiles(destinationDirectory);

or at least something like:

Package package;
...
new CodeCopier().copyPackage(package, destinationDirectory);

Expected behavior: The source code of the file which contains the "tagged" code should be copied to the destination directory (which is another Java project), with all the directory/package structure maintained.

Is there any library which would be helpful here?

If not, what is your advice, how should I implement my own "code copier" and what libraries would come in handy?

+5  A: 

If you only want to copy the files - don't do it. Move them to a library and use that library with different projects.

If you have to modify the code while copying, I'd recommend FMPP. Here you can define a model from some custom properties and access the model values to insert them into the templates you want to copy. I use this technique for creating new projects that have the same structure and insert the project name and some other individual properties into the generated code.

tangens
+1 - this is 100% on target. I would add that if you are modifying the code only slightly - i.e. changing variable values, etc - I would recommend still creating a library and abstracting out the code to allow for those variables to be set or added in to the calls. Martin Fowler's Refactoring is a good reference for all of this.
aperkins
Thanks, I will see what FMPP can do here. I know that a library would be better in general, but the requirements are clear for my task so it has be the source code.
leden
Don't Repeat Yourself. http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
RonU
@RonU That's an invalid argument. If you have to do something on a new project that you've already done on another, would you re-implement it again differently just because you don't want to repeat yourself? The argument you present is just valid for repetitions inside the same project.
Rui Curado