ant basedir and eclipse .project file generation

独自空忆成欢 提交于 2019-12-22 05:31:33

问题


I have an ant target that echo's the content of an eclipse .project file, the idea here is to ensure that the project is easily importable into eclipse. The project name should match the directory in which its checkout. I have this so far

<target name="eclipse" description="creates an eclipse .project file">
    <echo file=".project">
        <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
        <projectDescription>
            <name>${basedir}</name>
            <comment></comment>
            <projects>
            </projects>
            <buildSpec>
                <buildCommand>
                    <name>org.eclipse.jdt.core.javabuilder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
            </buildSpec>
            <natures>
                <nature>org.eclipse.jdt.core.javanature</nature>
            </natures>
        </projectDescription>
        ]]>
    </echo>    
</target>

The problem is the ${basedir} evaluates to a full path

<name>/home/assure/projects/MyProject</name>

but eclipse only needs the final folder name

<name>MyProject</name>

Any ideas on the ant target i should use to strip this path?


回答1:


You could define a property first, based on the last directory of path "basename".
(see Ant basename Task)

<basename property="project.name" file="${basedir}"/>

That allows you to reuse that property in the rest of your script

<projectDescription>
    <name>${project.name}</name>



回答2:


I had found a workaround using the ant 'flattenmapper' to trim the basedir value

    <path id="basedir.path">
        <pathelement path="${basedir}" />
    </path>
    <pathconvert property="project.name" refid="basedir.path">
        <chainedmapper>
            <mapper type="flatten"/>
            <flattenmapper/>
        </chainedmapper>
    </pathconvert>
    <echo message="${project.name}" />

but 1 line always beats 10 lines, ;-)



来源:https://stackoverflow.com/questions/2454128/ant-basedir-and-eclipse-project-file-generation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!