如何将一批maven化的示例转换成Eclipse能够理解的格式?

2024-03-28 18:21:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一些目前使用一种格式(不是Mavenized——实际上是ANT格式)的示例,并试图找到一种自动化的方法,将它们移植到Eclipse能够理解的内容中。虽然我有几年的Java和其他奇怪的语言,但用Java做这件事听起来就像用大锤敲打钉子。在

我一直在研究诸如Ruby、Python、Perl等脚本语言。我没有任何经验,但很乐意学习。在

我如何以一种格式(目录或带有文件的目录)的示例为例,并将其重新构造为类似于Eclipse项目的东西?例如,我想要一个具有以下结构的树

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

把它转换成

^{pr2}$

另外,我需要创建Eclipse需要的某些隐藏文件的能力。其中一个是.project文件,如下所示:

<?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>

如何使用这些脚本语言之一创建文本文件?在

提前谢谢。在


Tags: 文件nameorgcoreproject示例格式xml
3条回答

在您进行任何“咀嚼”之前,我会先让您的maven示例在eclipse中运行。Eclipse有几个插件用于处理基于maven的项目。在

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

Eclipse可以理解maven化的项目。您有两种选择:

  • 使用Maven Eclipse Plugin(一个Maven插件)并在一个Maven化的示例上运行mvn eclipse:eclipse,生成.project和{},然后将其作为现有项目导入Eclipse。

  • 使用m2eclipse插件(一个Eclipse插件)直接将一个现有的Maven项目导入到您的工作区中。

这两种方法都是排他的,使用其中一种。现在,人们倾向于选择提供完整Maven集成的m2eclipse插件。在

这应该可以满足您的需要(在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'))

这应该适用于多个操作系统。这本可以写得短些,但对新手来说会更混乱。不尝试捕捉异常。例如,操作系统.mkdirs如果新目录已经存在,可能会失败。在

在运行之前更改base_path和{}。在

相关问题 更多 >