0

I'm in the process of looking at taking some examples that are currently in one format (NOT Mavenized - actually in ANT form) and trying to find an automated way to munge them into something that Eclipse can comprehend. Though I have several years of Java and other odd languages, doing this in Java sounds like using a sledgehammer to drive a nail.

I've been looking into scripting languages such as Ruby, Python, Perl, and so on. I have no experience with any of them, but would be happy to learn.

How can I take an example in one format (directory or directory with files) and restructure it into something approximating an Eclipse project? For example, I'd like to take a tree with the following structure

dir my_example
  - build.xml
  - deployment.xml
  - jboss-esb-unfiltered.xml
  - log4j.xml
  - readme.txt

and convert it to

dir1 my_example_eclipse
  - dir src
    - [empty]
  - dir esbcontent
    - dir META-INF
      - deployment.xml
      - jboss-esb-unfiltered.xml
    - log4j.xml
  - readme.txt

In addition, I need the ability to create certain hidden files that Eclipse needs. One of these is the .project file like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>helloworld</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.wst.common.project.facet.core.builder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.wst.validation.validationbuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
    <nature>org.eclipse.jdt.core.javanature</nature>
    <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
    <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
</natures>
</projectDescription>

How would I create a text file with one of these scripting languages?

Thanks in advance.

6
  • 1
    All three languages shine in this area. If you choose Python, google for examples using os.walk to traverse files and directories. Nov 11, 2010 at 20:41
  • 1
    In this case, perl > Python > Ruby. Nov 11, 2010 at 20:42
  • Also google for "maven eclipse", it looks like there may be some existing solutions. Nov 11, 2010 at 20:45
  • @Fitz Please provide a specific example of what you'd like. Fill in the blanks that I've marked FIXME. Questions of the form "What's the best ..." tend to get downvoted and closed quickly, so ask how to do what you want. We don't make anything less than the best suggestions here! :-)
    – Greg Bacon
    Nov 11, 2010 at 20:54
  • @Steven @Rafe @Greg - thanks for the feedback... @Greg - I'm updating the question now with additional details.
    – Fitz
    Nov 16, 2010 at 17:46

3 Answers 3

5

How can I convert a batch of Mavenized examples into a format that Eclipse understands?

Eclipse can understand Mavenized projects. You have two options:

  • use the Maven Eclipse Plugin (a Maven plugin) and run mvn eclipse:eclipse on a Mavenized example to generate the .project and .classpath and then import it as Existing Project into Eclipse.

  • use the m2eclipse plugin (an Eclipse plugin) to directly import an Existing Maven Project into your workspace.

Both approach are exclusive, use one or the the other. Nowadays, people tend to prefer the m2eclipse plugin that provides full Maven integration.

1
  • Well, it turns out I'm an idiot. These examples aren't Mavenized - they're ANTified. Is there a way to convert ant to Maven?
    – Fitz
    Nov 16, 2010 at 17:44
2

Before you do any "munging", I'd try getting your maven examples working in eclipse first. Eclipse has several plugins for working out of maven-based projects.

0

This should do what you need (in Python):

import os
import shutil

def maven_to_eclipse(maven_dir, eclipse_dir):
    # assumes mode of maven_dir will be the same as eclipse_dir
    new_mode = os.stat(maven_dir).st_mode
    if os.path.exists(eclipse_dir):
        # if new_dir doesn't exist
        # create it with same permissions as old_dir
        os.mkdirs(eclipse_dir, new_mode)

    # create directories under new_dir: src, ebscontent, ebscontent/META-INF
    # use os.path.join to work on multiple os 
    os.mkdir(os.path.join(eclipse_dir, 'src'), new_mode)
    os.mkdir(os.path.join(eclipse_dir, 'ebscontent'), new_mode)
    os.mkdir(os.path.join(eclipse_dir, 'ebscontent', 'META-INF'), new_mode)

    # cp old/deployment.xml new/ebsconent/META-INF/deployment.xml
    shutil.copy2(os.path.join(maven_dir, 'deployment.xml'),
        os.path.join(eclipse_dir, 'ebscontent', 'META-INF', 'deployment.xml'))

    # cp old/jboss-esb-unfiltered.xml new/ebsconent/META-INF/jboss-esb-unfiltered.xml
    shutil.copy2(os.path.join(maven_dir, 'jboss-esb-unfiltered.xml'),
        os.path.join(eclipse_dir, 'ebscontent', 'META-INF', 'jboss-esb-unfiltered.xml'))

    # cp old/log4j.xml new/ebsconent/log4j.xml
    shutil.copy2(os.path.join(maven_dir, 'log4j.xml'),
        os.path.join(eclipse_dir, 'ebscontent', 'log4j.xml'))

    # cp old/readme.txt new/readme.txt
    shutil.copy2(os.path.join(maven_dir, 'readme.txt'),
        os.path.join(eclipse_dir, 'readme.txt'))

if __name__ == '__main__':
    base_path = 'C:\\Path\\To\\Maven Dirs'
    maven_dirs = ('my_example', 'another_example', 'third_example')
    for maven_dir in maven_dirs:
        maven_to_eclipse(os.path.join(base_path, maven_dir), 
            os.path.join(base_bath, maven_dir + '_eclipse'))

This should work on multiple OSes. This could have been written shorter, but would be more confusing to a newbie. No attempt is made to catch exceptions. For example, os.mkdirs might fail if the new directory already exists.

Change base_path and maven_dirs before running.

4
  • That pretty much answers that, doesn't it. :)
    – Fitz
    Nov 16, 2010 at 20:35
  • @Fitz If it works for you, could you accept the answer? Otherwise, please post any problems and I'll be happy to troubleshoot. Nov 16, 2010 at 20:41
  • I accept the answer, but have one last problem to solve. Do you have an equally easy way to create a text file via python? I need to create some of the "hidden" files in Eclipse, starting with a .project file:
    – Fitz
    Nov 16, 2010 at 21:25
  • I've updated the initial question with an example of what a .project file looks like.
    – Fitz
    Nov 16, 2010 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.