Using Ant's classpath in Eclipse

后端 未结 2 1680
执笔经年
执笔经年 2021-01-05 15:16

I have an Ant build.xml file that works just fine on the command line: it compiles, builds the JAR, and I am able to execute the main method from the JAR just f

相关标签:
2条回答
  • 2021-01-05 15:35

    An alternative to perl is to use an embedded groovy task:

    <project name="demo" default="eclipse-files">
    
        <property name="src.dir"     location="src"/>
        <property name="classes.dir" location="build/classes"/>
    
        <path id="dep.runtime">
           <fileset dir="${lib}"       includes="**/*.jar"/>
           <fileset dir="${hive-util}" includes="**/*.jar"/>
           <fileset dir="${hpdb}"      includes="**/*.jar"/>
           <fileset dir="${static}"    includes="**/*.jar"/>
        </path>
    
        <target name="bootstrap">
            <mkdir dir="${user.home}/.ant/lib"/>
            <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.4/groovy-all-2.1.4.jar"/>
        </target>
    
        <target name="eclipse-files">
            <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
            <groovy>
                import groovy.xml.MarkupBuilder
    
                project.log "Creating .classpath"
    
                new File(".classpath").withWriter { writer ->
                    def xml = new MarkupBuilder(writer)
    
                    xml.classpath() {
                        classpathentry(kind:"src",    path:properties["src.dir"])
                        classpathentry(kind:"output", path:properties["classes.dir"])
                        classpathentry(kind:"con",    path:"org.eclipse.jdt.launching.JRE_CONTAINER")
    
                        project.references."dep.runtime".each {
                            classpathentry(kind:"lib", path:it)
                        }
                    }
                }
            </groovy>
        </target>
    
        <target name="clean">
            <delete file=".classpath"/>
        </target>
    
    </project>
    

    Notes:

    • The bootstrap target will download the 3rd party groovy jar (No dependency on perl)
    • Groovy can access the "dep.runtime" ANT path directly and iterate over its contents
    • Groovy has excellent support for writing XML files.

    The following answer is similar and additionally generates the Eclipse .project file.

    • Using Apache Ivy with netbeans
    0 讨论(0)
  • 2021-01-05 15:57

    I came up with the following workaround, inspired by the link provided by @leeand00.

    First, I wrote a simple Perl script (called genClasspath.pl) that generates the .classpath file that Eclipse uses.

    #!/usr/bin/perl
    use strict;
    
    if (@ARGV != 2) {
      print STDERR "Usage: $0 OUTFILE CLASSPATHSTRING\n";
      print STDERR "e.g., $0 .classpath path1:path2:path3\n";
      exit 1;
    }
    
    my $OUTFILE         = $ARGV[0];
    my $CLASSPATHSTRING = $ARGV[1];
    
    open my $out_fh, '>', $OUTFILE or die "Couldn't open output file: $!";
    
    print $out_fh q{<?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="output" path="build"/>
    };
    
    my @libs = split(":", $CLASSPATHSTRING);
    foreach my $thisLib (@libs){
        print $out_fh "    <classpathentry kind=\"lib\" path=\"$thisLib\"/>\n";
    }
    print $out_fh "</classpath>\n";
    

    Then, I have my build.xml file call this script with the content of dep.runtime:

    <target name="compile" depends="init">
        <javac srcdir="${src}" destdir="${build}" debug="on" includeantruntime="false">
            <classpath refid="dep.runtime" />
        </javac>
    
        <property name="myclasspath" refid="dep.runtime"/>
    
        <exec dir="." executable="../../scripts/genClasspath.pl" os="Linux">
            <arg value=".classpath"/>
            <arg value="${myclasspath}"/>
        </exec>
    
    </target>
    

    The only catch is that I need to run Ant on the command line at least once before I open the project in Eclipse. But when I do, Eclipse is able to compile and execute my project just fine, since the classpath is exactly the same as Ant's.

    0 讨论(0)
提交回复
热议问题