简体   繁体   中英

Ant - No public execute() error, even though it is there

I have an Ant script that I use to compile a project, and recently I've been trying to implement Maven to said project to deal with libraries references. I managed to do a'okay and create a pom.xml script that copies the libraries into the lib folder and then invokes the build.xml of Ant to build the whole project.

But then I tried to make ant invoke a function from the main class of the project (by making it extend the Task class and implementing the execute() method), but for some reason the build.xml script says that said function doesn't exist. It finds the class, alright, but not the function. What's weirder is the fact that I tried it in a previous version that doesn't have Maven implemented, and it works perfectly fine.

This is the bit in the ant build.xml that sets the path and defines the task:

<path id="classes">
    <pathelement path="${basedir}/bin" />
    <fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<taskdef name="getVersionTask" 
    classname="us.inswitch.jreport.JReport" 
classpathref="classes"/>
<getVersionTask/>

And this is, in short, the class in question (with the unnecesary things removed):

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class JReport extends Task {

    public void execute() throws BuildException {
        getProject().setProperty("version", version);
        Date date;
        date=new Date();
        DateFormat format=DateFormat.getDateInstance();
        getProject().setProperty("date",format.format(date));
    }
}

In both project cases, the files are the same through and through.

I think that, somehow, one being a Maven project and the other not being one is affecting something. Even after copying the ant files in the directory instead of referencing them with Maven, the error is still there. If I try to run the build script alone on the Maven project, it crashes and lays out this message pile:

[javac] import org.apache.tools.ant.BuildException;
[javac]                            ^
[javac] C:\Users\user\Documents\workspace\jreport-maven\src\us\inswitch\jreport\JReport.java:9: package org.apache.tools.ant does not exist
[javac] import org.apache.tools.ant.Task;
[javac]                            ^
[javac] C:\Users\user\Documents\workspace\jreport-maven\src\us\inswitch\jreport\JReport.java:19: cannot find symbol
[javac] symbol: class Task
[javac] public class JReport extends Task {
[javac]                              ^
[javac] C:\Users\user\Documents\workspace\jreport-maven\src\us\inswitch\jreport\JReport.java:115: cannot find symbol
[javac] symbol  : class BuildException
[javac] location: class us.inswitch.jreport.JReport
[javac]     public void execute() throws BuildException {
[javac]                                  ^
[javac] C:\Users\user\Documents\workspace\jreport-maven\src\us\inswitch\jreport\JReport.java:116: cannot find symbol
[javac] symbol  : method getProject()
[javac] location: class us.inswitch.jreport.JReport
[javac]         getProject().setProperty("version", version);
[javac]         ^
[javac] C:\Users\user\Documents\workspace\jreport-maven\src\us\inswitch\jreport\JReport.java:120: cannot find symbol
[javac] symbol  : method getProject()
[javac] location: class us.inswitch.jreport.JReport
[javac]         getProject().setProperty("date",format.format(date));
[javac]         ^
[javac] 6 errors

Any ideas?

The error message is pretty clear:

[javac] ... package org.apache.tools.ant does not exist
[javac] import org.apache.tools.ant.Task;
[javac]                            ^

During your compile, you need the ant.jar file in your javac classpath.

If you want to use Maven repositories for dependency management, you may want to look into using Ivy . Ivy is a dependency manager that can be used by Ant and can (if so configured) use Maven repositories.

At my current location, our company produces a lot of jars that go into other projects. Originally, they tried downloading these jars into projects, and then using a overly complex system to fetch the jars that still didn't work. I was able to incorporate Ivy into the current build system.

Moving to Maven would have meant completely overhaul of all of the projects and not being 100% sure what we were producing was correct. Using Ivy allowed me to handle the dependency management issues with a minimal amount of changes. Plus, it allowed me to fix this issue as quickly as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM