Tôi chắc chắn không có cách nào để gọi chung một tác vụ cho từng tệp mà không cần tạo custom task hoặc sử dụng ant-contrib của riêng bạn. Tuy nhiên có nhiều cách để có kiến để tự động lấy ant-contrib (hoặc một lọ tùy chỉnh) để bạn có thể đạt được kết quả mong muốn. Đây là phương pháp được sử dụng bởi TIBant, trong đó có một yêu cầu tương tự (để nó dễ dàng cho bất cứ ai để đóng góp vào sự phát triển),
Bước 1: Tải Apache Ivy
Apache Ivy có thể được sử dụng để lấy phụ thuộc như ant-contrib. Tôi sử dụng các tính chất và mục tiêu kiến sau đây để tải về và tải Ivy
<property name="ivy.install.version" value="2.2.0" />
<property name="ivy.jar.dir" location="${user.home}/.ivy2/jars" />
<property name="ivy.jar.file" location="${ivy.jar.dir}/ivy-${ivy.install.version}.jar" />
<target name="-download-ivy" unless="ivy.downloaded">
<mkdir dir="${ivy.jar.dir}" />
<!-- download Ivy from web site so that it can be used even without any special installation -->
<echo message="installing ivy..." />
<get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}"
usetimestamp="true"
verbose="true" />
</target>
<target name="-check-ivy-downloaded">
<condition property="ivy.downloaded">
<and>
<available file="${ivy.jar.file}" />
<available file="${ivy.jar.dir}/jsch-0.1.44-1.jar" />
</and>
</condition>
</target>
<target name="-load-ivy" depends="-check-ivy-downloaded,-download-ivy" unless="ivy.loaded">
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar" />
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path" />
<property name="ivy.loaded" value="true" />
</target>
Bước 2: Thêm ant-contrib phụ thuộc
Ivy sử dụng một "tập tin ivy" để xác định phụ thuộc.Bạn có thể thêm ant-contrib như sau
<dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" transitive="false"/>
một file ivy đầy đủ có thể trông như
<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
<info
organisation="org.my"
module="mymodule"
status="release"/>
<dependencies>
<dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" transitive="false"/>
</dependencies>
</ivy-module>
Bạn có thể cũng ra tập tin này động từ kịch bản kiến của bạn sử dụng echoxml
Bước 3: Truy xuất các phụ thuộc
Sử dụng Ivy để tải xuống ant-contrib
(hoặc wha phụ thuộc tever bạn đã xác định)
<target name="retrieve" description="retrieve dependancies with ivy" depends="-load-ivy">
<ivy:retrieve />
<ivy:artifactproperty name="[module].[artifact]" value="lib/[artifact]-[revision].[ext]" />
</target>
Bước 4: Tải ant-contrib
<target name="-load-ant-contrib" depends="retrieve" unless="ant.contrib.loaded">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${ant-contrib.ant-contrib}" />
</classpath>
</taskdef>
<property name="ant.contrib.loaded" value="true" />
</target>
Bước 5: Tạo mục tiêu của bạn mà lặp trên các tập tin sử dụng for
<target name="mytarget" depends="-load-ant-contrib">
<for param="file">
<fileset dir="somedir" includes="..." />
<sequential>
<!-- do stuff with @{file} -->
</sequential>
</for>
</target>
Nếu bạn chỉ cần tải ant-contrib và không muốn t o sử dụng Ivy để quản lý các phụ thuộc khác, sau đó bạn có thể bỏ qua hầu hết các điều trên và chỉ sử dụng get để tải xuống ant-contrib
giống như cách tải xuống ở trên Ivy.
Cảm ơn câu trả lời của bạn !!! –
Nó không hoàn toàn trả lời câu hỏi của tôi, nhưng nó cung cấp một giải pháp tốt đẹp/thay thế cho vấn đề của tôi! Thx cho rằng –