Search in sources :

Example 46 with Path

use of org.apache.tools.ant.types.Path in project ant by apache.

the class JasperC method execute.

/**
 * Our execute method.
 * @return true if successful
 * @throws BuildException on error
 */
@Override
public boolean execute() throws BuildException {
    getJspc().log("Using jasper compiler", Project.MSG_VERBOSE);
    CommandlineJava cmd = setupJasperCommand();
    try {
        // Create an instance of the compiler, redirecting output to
        // the project log
        Java java = new Java(owner);
        Path p = getClasspath();
        if (getJspc().getClasspath() != null) {
            getProject().log("using user supplied classpath: " + p, Project.MSG_DEBUG);
        } else {
            getProject().log("using system classpath: " + p, Project.MSG_DEBUG);
        }
        java.setClasspath(p);
        java.setDir(getProject().getBaseDir());
        java.setClassname("org.apache.jasper.JspC");
        // this is really irritating; we need a way to set stuff
        for (String arg : cmd.getJavaCommand().getArguments()) {
            java.createArg().setValue(arg);
        }
        java.setFailonerror(getJspc().getFailonerror());
        // we are forking here to be sure that if JspC calls
        // System.exit() it doesn't halt the build
        java.setFork(true);
        java.setTaskName("jasperc");
        java.execute();
        return true;
    } catch (Exception ex) {
        if (ex instanceof BuildException) {
            throw (BuildException) ex;
        }
        throw new BuildException("Error running jsp compiler: ", ex, getJspc().getLocation());
    } finally {
        getJspc().deleteEmptyJavaFiles();
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Java(org.apache.tools.ant.taskdefs.Java) CommandlineJava(org.apache.tools.ant.types.CommandlineJava) CommandlineJava(org.apache.tools.ant.types.CommandlineJava) BuildException(org.apache.tools.ant.BuildException) BuildException(org.apache.tools.ant.BuildException)

Example 47 with Path

use of org.apache.tools.ant.types.Path in project ant by apache.

the class BorlandDeploymentTool method verifyBorlandJarV4.

/**
 * Verify the produced jar file by invoking the Borland verify tool
 * @param sourceJar java.io.File representing the produced jar file
 */
private void verifyBorlandJarV4(File sourceJar) {
    Java javaTask = null;
    log("verify BAS " + sourceJar, Project.MSG_INFO);
    try {
        String args = verifyArgs;
        args += " " + sourceJar.getPath();
        javaTask = new Java(getTask());
        javaTask.setTaskName("verify");
        javaTask.setClassname(VERIFY);
        Commandline.Argument arguments = javaTask.createArg();
        arguments.setLine(args);
        Path classpath = getCombinedClasspath();
        if (classpath != null) {
            javaTask.setClasspath(classpath);
            javaTask.setFork(true);
        }
        log("Calling " + VERIFY + " for " + sourceJar.toString(), Project.MSG_VERBOSE);
        javaTask.execute();
    } catch (Exception e) {
        // TO DO : delete the file if it is not a valid file.
        String msg = "Exception while calling " + VERIFY + " Details: " + e.toString();
        throw new BuildException(msg, e);
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Java(org.apache.tools.ant.taskdefs.Java) Commandline(org.apache.tools.ant.types.Commandline) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 48 with Path

use of org.apache.tools.ant.types.Path in project ant by apache.

the class Javah method execute.

/**
 * Execute the task
 *
 * @throws BuildException is there is a problem in the task execution.
 */
@Override
public void execute() throws BuildException {
    // first off, make sure that we've got a srcdir
    final Set<Settings> settings = EnumSet.noneOf(Settings.class);
    if (cls != null) {
        settings.add(Settings.cls);
    }
    if (!classes.isEmpty()) {
        settings.add(Settings.classes);
    }
    if (!files.isEmpty()) {
        settings.add(Settings.files);
    }
    if (settings.size() > 1) {
        throw new BuildException("Exactly one of " + Settings.values() + " attributes is required", getLocation());
    }
    if (destDir != null) {
        if (!destDir.isDirectory()) {
            throw new BuildException("destination directory \"" + destDir + "\" does not exist or is not a directory", getLocation());
        }
        if (outputFile != null) {
            throw new BuildException("destdir and outputFile are mutually " + "exclusive", getLocation());
        }
    }
    if (classpath == null) {
        classpath = new Path(getProject()).concatSystemClasspath("last");
    } else {
        classpath = classpath.concatSystemClasspath("ignore");
    }
    JavahAdapter ad = nestedAdapter != null ? nestedAdapter : JavahAdapterFactory.getAdapter(facade.getImplementation(), this, createImplementationClasspath());
    if (!ad.compile(this)) {
        throw new BuildException("compilation failed");
    }
}
Also used : Path(org.apache.tools.ant.types.Path) JavahAdapter(org.apache.tools.ant.taskdefs.optional.javah.JavahAdapter) BuildException(org.apache.tools.ant.BuildException)

Example 49 with Path

use of org.apache.tools.ant.types.Path in project ant by apache.

the class SignJar method execute.

/**
 * sign the jar(s)
 *
 * @throws BuildException on errors
 */
@Override
public void execute() throws BuildException {
    // validation logic
    final boolean hasJar = jar != null;
    final boolean hasSignedJar = signedjar != null;
    final boolean hasDestDir = destDir != null;
    final boolean hasMapper = mapper != null;
    if (!hasJar && !hasResources()) {
        throw new BuildException(ERROR_NO_SOURCE);
    }
    if (null == alias) {
        throw new BuildException(ERROR_NO_ALIAS);
    }
    if (null == storepass) {
        throw new BuildException(ERROR_NO_STOREPASS);
    }
    if (hasDestDir && hasSignedJar) {
        throw new BuildException(ERROR_TODIR_AND_SIGNEDJAR);
    }
    if (hasResources() && hasSignedJar) {
        throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS);
    }
    // we can change implementation details later
    if (!hasDestDir && hasMapper) {
        throw new BuildException(ERROR_MAPPER_WITHOUT_DEST);
    }
    beginExecution();
    try {
        // special case single jar handling with signedjar attribute set
        if (hasJar && hasSignedJar) {
            // single jar processing
            signOneJar(jar, signedjar);
            // return here.
            return;
        }
        // the rest of the method treats single jar like
        // a nested path with one file
        Path sources = createUnifiedSourcePath();
        // set up our mapping policy
        FileNameMapper destMapper = hasMapper ? mapper : new IdentityMapper();
        // deal with the paths
        for (Resource r : sources) {
            FileResource fr = ResourceUtils.asFileResource(r.as(FileProvider.class));
            // calculate our destination directory; it is either the destDir
            // attribute, or the base dir of the fileset (for in situ updates)
            File toDir = hasDestDir ? destDir : fr.getBaseDir();
            // determine the destination filename via the mapper
            String[] destFilenames = destMapper.mapFileName(fr.getName());
            if (destFilenames == null || destFilenames.length != 1) {
                // we only like simple mappers.
                throw new BuildException(ERROR_BAD_MAP + fr.getFile());
            }
            File destFile = new File(toDir, destFilenames[0]);
            signOneJar(fr.getFile(), destFile);
        }
    } finally {
        endExecution();
    }
}
Also used : Path(org.apache.tools.ant.types.Path) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File)

Example 50 with Path

use of org.apache.tools.ant.types.Path in project ant by apache.

the class Depend method determineDependencies.

/**
 * Determine the dependencies between classes. Class dependencies are
 * determined by examining the class references in a class file to other
 * classes.
 *
 * This method sets up the following fields
 * <ul>
 *   <li>affectedClassMap - the list of classes each class affects</li>
 *   <li>classFileInfoMap - information about each class</li>
 *   <li>classpathDependencies - the list of jars and classes from the
 *                             classpath that each class depends upon.</li>
 * </ul>
 *
 * If required, the dependencies are written to the cache.
 *
 * @exception IOException if either the dependencies cache or the class
 *      files cannot be read or written
 */
private void determineDependencies() throws IOException {
    affectedClassMap = new HashMap<>();
    classFileInfoMap = new HashMap<>();
    boolean cacheDirty = false;
    Map<String, List<String>> dependencyMap = new HashMap<>();
    File cacheFile = null;
    boolean cacheFileExists = true;
    long cacheLastModified = Long.MAX_VALUE;
    // read the dependency cache from the disk
    if (cache != null) {
        cacheFile = new File(cache, CACHE_FILE_NAME);
        cacheFileExists = cacheFile.exists();
        cacheLastModified = cacheFile.lastModified();
        if (cacheFileExists) {
            dependencyMap = readCachedDependencies(cacheFile);
        }
    }
    for (ClassFileInfo info : getClassFiles()) {
        log("Adding class info for " + info.className, Project.MSG_DEBUG);
        classFileInfoMap.put(info.className, info);
        List<String> dependencyList = null;
        if (cache != null) {
            // not out of date
            if (cacheFileExists && cacheLastModified > info.absoluteFile.lastModified()) {
                // depFile exists and is newer than the class file
                // need to get dependency list from the map.
                dependencyList = dependencyMap.get(info.className);
            }
        }
        if (dependencyList == null) {
            // not cached - so need to read directly from the class file
            DependencyAnalyzer analyzer = new AntAnalyzer();
            analyzer.addRootClass(info.className);
            analyzer.addClassPath(destPath);
            analyzer.setClosure(false);
            dependencyList = new ArrayList<>();
            Enumeration<String> depEnum = analyzer.getClassDependencies();
            while (depEnum.hasMoreElements()) {
                String o = depEnum.nextElement();
                dependencyList.add(o);
                log("Class " + info.className + " depends on " + o, Project.MSG_DEBUG);
            }
            cacheDirty = true;
            dependencyMap.put(info.className, dependencyList);
        }
        // one of those, add this class into their affected classes list
        for (String dependentClass : dependencyList) {
            affectedClassMap.computeIfAbsent(dependentClass, k -> new HashMap<>()).put(info.className, info);
            log(dependentClass + " affects " + info.className, Project.MSG_DEBUG);
        }
    }
    classpathDependencies = null;
    Path checkPath = getCheckClassPath();
    if (checkPath != null) {
        // now determine which jars each class depends upon
        classpathDependencies = new HashMap<>();
        try (AntClassLoader loader = getProject().createClassLoader(checkPath)) {
            Map<String, Object> classpathFileCache = new HashMap<>();
            Object nullFileMarker = new Object();
            for (Map.Entry<String, List<String>> e : dependencyMap.entrySet()) {
                String className = e.getKey();
                log("Determining classpath dependencies for " + className, Project.MSG_DEBUG);
                List<String> dependencyList = e.getValue();
                Set<File> dependencies = new HashSet<>();
                classpathDependencies.put(className, dependencies);
                for (String dependency : dependencyList) {
                    log("Looking for " + dependency, Project.MSG_DEBUG);
                    Object classpathFileObject = classpathFileCache.get(dependency);
                    if (classpathFileObject == null) {
                        classpathFileObject = nullFileMarker;
                        if (!dependency.startsWith("java.") && !dependency.startsWith("javax.")) {
                            URL classURL = loader.getResource(dependency.replace('.', '/') + ".class");
                            log("URL is " + classURL, Project.MSG_DEBUG);
                            if (classURL != null) {
                                if ("jar".equals(classURL.getProtocol())) {
                                    String jarFilePath = classURL.getFile();
                                    int classMarker = jarFilePath.indexOf('!');
                                    jarFilePath = jarFilePath.substring(0, classMarker);
                                    if (jarFilePath.startsWith("file:")) {
                                        classpathFileObject = new File(FileUtils.getFileUtils().fromURI(jarFilePath));
                                    } else {
                                        throw new IOException("Bizarre nested path in jar: protocol: " + jarFilePath);
                                    }
                                } else if ("file".equals(classURL.getProtocol())) {
                                    classpathFileObject = new File(FileUtils.getFileUtils().fromURI(classURL.toExternalForm()));
                                }
                                log("Class " + className + " depends on " + classpathFileObject + " due to " + dependency, Project.MSG_DEBUG);
                            }
                        } else {
                            log("Ignoring base classlib dependency " + dependency, Project.MSG_DEBUG);
                        }
                        classpathFileCache.put(dependency, classpathFileObject);
                    }
                    if (classpathFileObject != nullFileMarker) {
                        // we need to add this jar to the list for this class.
                        File jarFile = (File) classpathFileObject;
                        log("Adding a classpath dependency on " + jarFile, Project.MSG_DEBUG);
                        dependencies.add(jarFile);
                    }
                }
            }
        }
    } else {
        log("No classpath to check", Project.MSG_DEBUG);
    }
    // write the dependency cache to the disk
    if (cache != null && cacheDirty) {
        writeCachedDependencies(dependencyMap);
    }
}
Also used : AntClassLoader(org.apache.tools.ant.AntClassLoader) DefaultRmicAdapter(org.apache.tools.ant.taskdefs.rmic.DefaultRmicAdapter) Enumeration(java.util.Enumeration) URL(java.net.URL) HashMap(java.util.HashMap) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) ArrayList(java.util.ArrayList) Path(org.apache.tools.ant.types.Path) HashSet(java.util.HashSet) Map(java.util.Map) Project(org.apache.tools.ant.Project) LinkedHashSet(java.util.LinkedHashSet) FileUtils(org.apache.tools.ant.util.FileUtils) WLRmic(org.apache.tools.ant.taskdefs.rmic.WLRmic) Resource(org.apache.tools.ant.types.Resource) BufferedWriter(java.io.BufferedWriter) Predicate(java.util.function.Predicate) FileWriter(java.io.FileWriter) Set(java.util.Set) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) DependencyAnalyzer(org.apache.tools.ant.util.depend.DependencyAnalyzer) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) MatchingTask(org.apache.tools.ant.taskdefs.MatchingTask) File(java.io.File) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Reference(org.apache.tools.ant.types.Reference) HashMap(java.util.HashMap) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Path(org.apache.tools.ant.types.Path) IOException(java.io.IOException) DependencyAnalyzer(org.apache.tools.ant.util.depend.DependencyAnalyzer) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Path (org.apache.tools.ant.types.Path)175 File (java.io.File)81 BuildException (org.apache.tools.ant.BuildException)57 Test (org.junit.Test)49 Project (org.apache.tools.ant.Project)28 IOException (java.io.IOException)27 Commandline (org.apache.tools.ant.types.Commandline)21 ArrayList (java.util.ArrayList)15 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)12 AntClassLoader (org.apache.tools.ant.AntClassLoader)9 GroovyClassLoader (groovy.lang.GroovyClassLoader)8 URL (java.net.URL)8 StringTokenizer (java.util.StringTokenizer)8 Reference (org.apache.tools.ant.types.Reference)8 Java (org.apache.tools.ant.taskdefs.Java)7 FileSet (org.apache.tools.ant.types.FileSet)7 Resource (org.apache.tools.ant.types.Resource)7 FileWriter (java.io.FileWriter)6 List (java.util.List)6 Execute (org.apache.tools.ant.taskdefs.Execute)6