Ant路径转换

时间:2011-06-08 13:51:14

标签: ant path

下午好。

我正在运行ant来处理一些代码现在我在属性中有路径“com / source / project”但我需要将“com.source.project”传递给我的java代码,无论如何我可以将“/”转换为“”使用ant命令

感谢

2 个答案:

答案 0 :(得分:3)

PropertyRegex任务适合您,但您需要安装ant-contrib

<project>

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="./ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>

<property name="path" value="com/source/project"/>
<echo message="Path=${path}"/>

<propertyregex property="java.package.name"
              input="${path}"
              regexp="/"
              replace="."
              global="true"
              defaultValue="${path}" />

<echo message="package=${java.package.name}"/>
</project>

答案 1 :(得分:0)

这是一个使用Ant Plugin Flaka的完整项目。我还必须用'。'替换$ {path.separator}。开始一些java类。请参阅以';'开头的评论

<project xmlns:fl="antlib:it.haefelinger.flaka">

  <fl:install-property-handler/>

  <property name="srcroot" value="path/to/srcrootdir"/>
  <property name="classroot" value="path/to/classrootdir"/>

  <!-- determine all main classes -->
  <fileset dir="${srcroot}" includes="**/*.java" id="mainclasses">
    <contains text="public static void main"/>
  </fileset>

  <!-- iterate over those main classes and
       call the corresponding classfile -->
  <fl:for var="file" in="split('${toString:mainclasses}', ';')">
    <fl:let>
      ; strip the .java Extension
      file = replace(file, '', '.java')
      ; replace fileseparator with '.'
      ; on Windows you have to use the following line
      ; replace(file, '\.', '${file.separator}${file.separator}')
      file = replace(file, '\.', '${file.separator}')
      </fl:let>
    <fl:echo>
      starting => #{file} in ${classroot}
    </fl:echo>
    <java classname="#{file}">
      <classpath>
       <!--
         when using a fileset you'll get a
         java.util.zip.ZipException because you're
         referencing classfiles and no jars
         therefore you have to use
         pathelement and location
       -->
       <pathelement location="${classroot}"/>
      </classpath>
    </java>
  </fl:for>

</project>