Search in sources :

Example 91 with Path

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

the class SCRDescriptorTask method setDestdir.

public void setDestdir(File outputDirectory) {
    this.destdir = outputDirectory;
    if (destdir != null) {
        Path dst = new Path(getProject());
        dst.setLocation(destdir);
        createClasspath().add(dst);
    }
}
Also used : Path(org.apache.tools.ant.types.Path)

Example 92 with Path

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

the class IPojoTask method execute.

/**
 * Execute the Ant Task.
 *
 * @see org.apache.tools.ant.Task#execute()
 */
public void execute() {
    if (m_input == null && m_directory == null) {
        throw new BuildException("Neither input bundle nor directory specified");
    }
    if (m_input != null && !m_input.exists()) {
        throw new BuildException("The input bundle " + m_input.getAbsolutePath() + " does not exist");
    }
    if (m_directory != null && !m_directory.exists()) {
        throw new BuildException("The input directory " + m_directory.getAbsolutePath() + " does not exist");
    }
    if (m_directory != null && !m_directory.isDirectory()) {
        throw new BuildException("The input directory " + m_directory.getAbsolutePath() + " is not a directory");
    }
    if (m_input != null) {
        log("Input bundle file : " + m_input.getAbsolutePath());
    } else {
        log("Input directory : " + m_directory.getAbsolutePath());
    }
    if (m_manifest != null) {
        if (m_input != null) {
            throw new BuildException("The manifest location cannot be used when manipulating an existing bundle");
        }
        if (!m_manifest.exists()) {
            throw new BuildException("The manifest file " + m_manifest.getAbsolutePath() + " does not exist");
        }
    }
    // Get metadata file
    if (m_metadata == null) {
        m_metadata = new File("./metadata.xml");
        if (!m_metadata.exists()) {
            // Verify if annotations are ignored
            if (m_ignoreAnnotations) {
                log("No metadata file found & annotations ignored : nothing to do");
                return;
            } else {
                log("No metadata file found - trying to use only annotations");
                m_metadata = null;
            }
        } else {
            log("Metadata file : " + m_metadata.getAbsolutePath());
        }
    } else {
        // Metadata file is specified, check existence
        if (!m_metadata.exists()) {
            throw new BuildException("No metadata file found - the file " + m_metadata.getAbsolutePath() + " does not exist");
        } else {
            if (m_metadata.isDirectory()) {
                log("Metadata directory : " + m_metadata.getAbsolutePath());
            } else {
                log("Metadata file : " + m_metadata.getAbsolutePath());
            }
        }
    }
    initializeSaxDriver();
    log("Start manipulation");
    if (m_input != null) {
        // Prepare output file
        if (m_output == null) {
            m_output = new File("./_out.jar");
        }
        if (m_output.exists()) {
            boolean r = m_output.delete();
            if (!r) {
                throw new BuildException("The file " + m_output.getAbsolutePath() + " cannot be deleted");
            }
        }
    }
    AntReporter reporter = new AntReporter(getProject());
    Pojoization pojo = new Pojoization(reporter);
    if (m_ignoreAnnotations) {
        pojo.disableAnnotationProcessing();
    }
    if (!m_ignoreLocalXSD) {
        pojo.setUseLocalXSD();
    }
    Path classpath = getClasspath();
    classpath.addJavaRuntime();
    // Adding the input jar or directory
    if (m_classpath == null) {
        m_classpath = createClasspath();
    }
    Path element = m_classpath.createPath();
    if (m_input != null) {
        element.setLocation(m_input.getAbsoluteFile());
    } else if (m_directory != null) {
        element.setLocation(m_directory.getAbsoluteFile());
    }
    m_classpath.add(element);
    ClassLoader loader = getProject().createClassLoader(getClasspath());
    if (m_input != null) {
        pojo.pojoization(m_input, m_output, m_metadata, loader);
    } else {
        pojo.directoryPojoization(m_directory, m_metadata, m_manifest, loader);
    }
    for (int i = 0; i < reporter.getWarnings().size(); i++) {
        log((String) reporter.getWarnings().get(i), Project.MSG_WARN);
    }
    if (reporter.getErrors().size() > 0) {
        throw new BuildException((String) reporter.getErrors().get(0));
    }
    if (m_input != null) {
        String out;
        if (m_output.getName().equals("_out.jar")) {
            if (m_input.delete()) {
                if (!m_output.renameTo(m_input)) {
                    log("Cannot rename the output jar to " + m_input.getAbsolutePath(), Project.MSG_WARN);
                }
            } else {
                log("Cannot delete the input file : " + m_input.getAbsolutePath(), Project.MSG_WARN);
            }
            out = m_input.getAbsolutePath();
        } else {
            out = m_output.getAbsolutePath();
        }
        log("Bundle manipulation - SUCCESS");
        log("Output file : " + out);
    } else {
        log("Manipulation - SUCCESS");
        log("Output files : " + m_directory.getAbsolutePath());
        if (m_manifest != null) {
            log("Manifest : " + m_manifest.getAbsolutePath());
        }
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 93 with Path

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

the class Javadoc method doJava14.

// Do java1.4 arguments
private void doJava14(final Commandline toExecute) {
    for (final Object element : tags) {
        if (element instanceof TagArgument) {
            final TagArgument ta = (TagArgument) element;
            final File tagDir = ta.getDir(getProject());
            if (tagDir == null) {
                // The tag element is not used as a fileset,
                // but specifies the tag directly.
                toExecute.createArgument().setValue("-tag");
                toExecute.createArgument().setValue(ta.getParameter());
            } else {
                // The tag element is used as a
                // fileset. Parse all the files and create
                // -tag arguments.
                final DirectoryScanner tagDefScanner = ta.getDirectoryScanner(getProject());
                for (String file : tagDefScanner.getIncludedFiles()) {
                    final File tagDefFile = new File(tagDir, file);
                    try (final BufferedReader in = new BufferedReader(new FileReader(tagDefFile))) {
                        String line;
                        while ((line = in.readLine()) != null) {
                            toExecute.createArgument().setValue("-tag");
                            toExecute.createArgument().setValue(line);
                        }
                    } catch (final IOException ioe) {
                        throw new BuildException("Couldn't read tag file from " + tagDefFile.getAbsolutePath(), ioe);
                    }
                }
            }
        } else {
            final ExtensionInfo tagletInfo = (ExtensionInfo) element;
            toExecute.createArgument().setValue("-taglet");
            toExecute.createArgument().setValue(tagletInfo.getName());
            if (tagletInfo.getPath() != null) {
                final Path tagletPath = tagletInfo.getPath().concatSystemClasspath("ignore");
                if (!tagletPath.isEmpty()) {
                    toExecute.createArgument().setValue("-tagletpath");
                    toExecute.createArgument().setPath(tagletPath);
                }
            }
        }
    }
    final String sourceArg = source != null ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE);
    if (sourceArg != null) {
        toExecute.createArgument().setValue("-source");
        toExecute.createArgument().setValue(sourceArg);
    }
    if (linksource && doclet == null) {
        toExecute.createArgument().setValue("-linksource");
    }
    if (noqualifier != null && doclet == null) {
        toExecute.createArgument().setValue("-noqualifier");
        toExecute.createArgument().setValue(noqualifier);
    }
}
Also used : Path(org.apache.tools.ant.types.Path) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 94 with Path

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

the class Javadoc method doDoclet.

private void doDoclet(final Commandline toExecute) {
    if (doclet != null) {
        if (doclet.getName() == null) {
            throw new BuildException("The doclet name must be specified.", getLocation());
        }
        toExecute.createArgument().setValue("-doclet");
        toExecute.createArgument().setValue(doclet.getName());
        if (doclet.getPath() != null) {
            final Path docletPath = doclet.getPath().concatSystemClasspath("ignore");
            if (docletPath.size() != 0) {
                toExecute.createArgument().setValue("-docletpath");
                toExecute.createArgument().setPath(docletPath);
            }
        }
        for (final DocletParam param : Collections.list(doclet.getParams())) {
            if (param.getName() == null) {
                throw new BuildException("Doclet parameters must have a name");
            }
            toExecute.createArgument().setValue(param.getName());
            if (param.getValue() != null) {
                toExecute.createArgument().setValue(param.getValue());
            }
        }
    }
}
Also used : Path(org.apache.tools.ant.types.Path) BuildException(org.apache.tools.ant.BuildException)

Example 95 with Path

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

the class Javadoc method parsePackages.

/**
 * Add the directories matched by the nested dirsets to the Vector
 * and the base directories of the dirsets to the Path.  It also
 * handles the packages and excludepackages attributes and
 * elements.
 *
 * @since 1.5
 */
private void parsePackages(final List<String> pn, final Path sp) {
    final Set<String> addedPackages = new HashSet<>();
    final List<DirSet> dirSets = new ArrayList<>(packageSets);
    // and nested excludepackage elements
    if (sourcePath != null) {
        final PatternSet ps = new PatternSet();
        ps.setProject(getProject());
        if (packageNames.isEmpty()) {
            ps.createInclude().setName("**");
        } else {
            packageNames.stream().map(PackageName::getName).map(s -> s.replace('.', '/').replaceFirst("\\*$", "**")).forEach(pkg -> ps.createInclude().setName(pkg));
        }
        excludePackageNames.stream().map(PackageName::getName).map(s -> s.replace('.', '/').replaceFirst("\\*$", "**")).forEach(pkg -> ps.createExclude().setName(pkg));
        for (String pathElement : sourcePath.list()) {
            final File dir = new File(pathElement);
            if (dir.isDirectory()) {
                final DirSet ds = new DirSet();
                ds.setProject(getProject());
                ds.setDefaultexcludes(useDefaultExcludes);
                ds.setDir(dir);
                ds.createPatternSet().addConfiguredPatternset(ps);
                dirSets.add(ds);
            } else {
                log("Skipping " + pathElement + " since it is no directory.", Project.MSG_WARN);
            }
        }
    }
    for (DirSet ds : dirSets) {
        final File baseDir = ds.getDir(getProject());
        log("scanning " + baseDir + " for packages.", Project.MSG_DEBUG);
        final DirectoryScanner dsc = ds.getDirectoryScanner(getProject());
        boolean containsPackages = false;
        for (String dir : dsc.getIncludedDirectories()) {
            // are there any java files in this directory?
            final File pd = new File(baseDir, dir);
            final String[] files = pd.list((directory, name) -> name.endsWith(".java") || (includeNoSourcePackages && name.equals("package.html")));
            if (files.length > 0) {
                if ("".equals(dir)) {
                    log(baseDir + " contains source files in the default package, you must specify them as source files not packages.", Project.MSG_WARN);
                } else {
                    containsPackages = true;
                    final String packageName = dir.replace(File.separatorChar, '.');
                    if (!addedPackages.contains(packageName)) {
                        addedPackages.add(packageName);
                        pn.add(packageName);
                    }
                }
            }
        }
        if (containsPackages) {
            // We don't need to care for duplicates here,
            // Path.list does it for us.
            sp.createPathElement().setLocation(baseDir);
        } else {
            log(baseDir + " doesn\'t contain any packages, dropping it.", Project.MSG_VERBOSE);
        }
    }
}
Also used : Commandline(org.apache.tools.ant.types.Commandline) DirSet(org.apache.tools.ant.types.DirSet) Enumeration(java.util.Enumeration) URL(java.net.URL) Task(org.apache.tools.ant.Task) JavaEnvUtils(org.apache.tools.ant.util.JavaEnvUtils) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) ArrayList(java.util.ArrayList) Path(org.apache.tools.ant.types.Path) HashSet(java.util.HashSet) Vector(java.util.Vector) Locale(java.util.Locale) StringTokenizer(java.util.StringTokenizer) FileSet(org.apache.tools.ant.types.FileSet) OutputStreamWriter(java.io.OutputStreamWriter) Project(org.apache.tools.ant.Project) ProjectComponent(org.apache.tools.ant.ProjectComponent) FileUtils(org.apache.tools.ant.util.FileUtils) Resource(org.apache.tools.ant.types.Resource) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Set(java.util.Set) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) EnumeratedAttribute(org.apache.tools.ant.types.EnumeratedAttribute) PatternSet(org.apache.tools.ant.types.PatternSet) FileProvider(org.apache.tools.ant.types.resources.FileProvider) StringUtils(org.apache.tools.ant.util.StringUtils) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Collections(java.util.Collections) MagicNames(org.apache.tools.ant.MagicNames) InputStream(java.io.InputStream) Reference(org.apache.tools.ant.types.Reference) ArrayList(java.util.ArrayList) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) PatternSet(org.apache.tools.ant.types.PatternSet) File(java.io.File) HashSet(java.util.HashSet)

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