8

Ant ビルド スクリプトから多くのscpおよびsshexecその他のリモート コマンドを実行しています。jsch.jarantlibディレクトリにない場合、これらのコマンドは機能しません。機能させるために、JAR を ant ディレクトリにコピーしましたlibが、スクリプトを実行したい他の人が同じことをしなければならないため、これは良い解決策ではありません。Teamcity から ant ターゲットを実行するには、lib ファイルのパスを明示的に設定する必要があります。

Ant ビルド XML 自体で JAR のパスを指定する方法はありますか?

4

3 に答える 3

5

回答ありがとうございます。クラスローダータスクで動作させることができました。これが私がしたことです。

<project basedir="." >
  <property environment="env"/>

  <taskdef resource="net/jtools/classloadertask/antlib.xml">
    <classpath>
      <fileset dir="${basedir}/lib" includes="ant-classloader*.jar"/>
    </classpath>
  </taskdef>

  <!--Add JSCH jar to the classpath-->
  <classloader loader="system">
    <classpath>
      <fileset dir="${basedir}/lib" includes="jsch*.jar"/>
      </classpath>
  </classloader>

  <target name="Test">
      <scp todir="user1:pass1@server1:/tmp" trust="true" >
        <fileset dir="dir1">
          <include name="test.txt" />
        </fileset>
      </scp>
   </target>
</project>

ここでわかるように、"Test" ターゲットに依存するターゲットを指定する必要はありませんでした。これは、jsch.jar をシステム クラスローダーに追加するクラスローダーを使用します。

于 2011-02-25T11:06:30.223 に答える
2

考えられる回避策の 1 つは、-libコマンド ライン オプションを使用して、追加の jar を探す場所を ant に指示することです。antおそらく、このオプション セットで呼び出すラッパー スクリプトを作成できます。

別の方法は、ant-jsch.jarファイル (これはタスクを定義する ant に付属の jar であり、個別にダウンロードする必要がある jsch.jar ファイルではありません) を ant lib ディレクトリから移動し、taskdef別の ssh タスク用にを作成することです。次に、このタスクのクラスパスをjsch.jarおよび に設定しant-jsch.jarます。

<taskdef name="sshexec"
  classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
  <classpath>
    <pathelement location="jsch-0.1.44.jar"/>
    <pathelement location="ant-jsch.jar" />
  </classpath>
</taskdef>

ただし、lib ディレクトリに変更を加える必要があるため、これが役立つかどうかはわかりません。

私が知る限り、一般的に、ビルド ファイル自体の組み込みタスクに必要な追加の jar を指定することは、現時点では不可能です。たとえば、junit など、いくつかの特殊なケースがあります。

于 2011-02-24T16:35:38.050 に答える
1

To ensure your build is more cross platform I'd suggest using dependency management. The ivy plug-in can automatically install the version of your build's plugin at build-time.

This approach means the last jar you'll ever need to install into your ANT lib is ivy-2.2.0.jar :-)

First declare your project's dependencies in the file ivy.xml

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>
    <configurations>
        <conf name="anttask" description="Jars implementing ANT tasks"/>
    </configurations>
    <dependencies>
        <dependency org="com.jcraft" name="jsch" rev="0.1.42" conf="anttask->default"/>
    </dependencies>
</ivy-module>

Within your build.xml run ivy and use it to populate a custom classpath based on the ivy configuration:

<target name='init' description='Resolve project dependencies and set classpaths'>
    <ivy:resolve/>
    <ivy:cachepath pathid="anttask.path" conf="anttask"/>
</target>

Finally, elsewhere in your build declare your ANT tasks using the class path now automatically populated by ivy.

<target name='dosomething' depends="init">
    <taskdef name="sshexec" 
             classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
             classpathref="anttask.path"/>
    ..
    ..
</target>

This approach works for all ANT plug-ins, most of which are available in the central Maven repository. The second benefit is that it's easy to upgrade the plug-in versions across all builds.

于 2011-02-24T18:16:22.710 に答える